3 条题解
-
1
#include <iostream> #include <queue> #include <iomanip> using namespace std; struct node { int x; int y; int step; }; int n, m, sx, sy, ex, ey; char s[405][405]; bool vis[405][405]; int dis[405][405]; int dx[8] = {-2, -2, -1, -1, 1, 1, 2, 2}; int dy[8] = {-1, 1, -2, 2, -2, 2, -1, 1}; void bfs() { queue<node> q; q.push({sx, sy, 0}); vis[sx][sy]=true; dis[sx][sy]=0; while (!q.empty()) { node now = q.front(); q.pop(); for (int i = 0; i <= 7; i++) { int nx = now.x + dx[i], ny = now.y + dy[i]; if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && vis[nx][ny] == false) { vis[nx][ny] = true; q.push({nx, ny, now.step + 1}); dis[nx][ny]=dis[now.x][now.y]+1; } } } } int main() { cin >> n >> m >> sx >> sy; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { dis[i][j]=-1; } } bfs(); for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { cout<<left; cout<<setw(5)<<dis[i][j]; } cout<<endl; } return 0; } -
-3
#include <queue> #include <cstring> using namespace std; int N,M,sn,sm; int a[500][500]; int dx[] = {-1,-2,-2,-1,1,2,2,1}; int dy[] = {-2,-1,1,2,2,1,-1,-2}; bool vis[500][500] = {false}; struct Node { int x; int y; int step; }; queue<Node>q; bool ia(int x,int y) { if (x >= 1 && x <= N && y >= 1 && y <= M) { return true; } return false; } void bfs(int x,int y) { q.push((Node){x,y,0}); vis[x][y] = true; a[x][y] = 0; while (!q.empty()) { Node temp = q.front(); q.pop(); for (int i = 0;i < 8;i++) { int nx = temp.x + dx[i]; int ny = temp.y + dy[i]; if (ia(nx,ny) == true && vis[nx][ny] == false) { q.push((Node){nx,ny,temp.step + 1}); a[nx][ny] = temp.step + 1; vis[nx][ny] = true; } } } } int main() { memset(a,-1,sizeof a); cin >> N >> M >> sn >> sm; bfs(sn,sm); for (int i = 1;i <= N;i++) { for (int j = 1;j <= M;j++) { printf("%-5d",a[i][j]); } cout << endl; } return 0; } -
-3
/***************************************** ±¸×¢£º ******************************************/ #include <queue> #include <set> #include <math.h> #include <stack> #include <stdio.h> #include <iostream> #include <vector> #include <iomanip> #include <string.h> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; #define LL long long const int N = 1e4 + 10; const int INF = 0x3f3f3f3f; struct node { int x, y; node(int xx, int yy) { x = xx, y = yy; } }; int n, m, x, y; queue<node> q; int a[N][N]; int vis[N][N]; int dx[] = {-2, -2, -1, 1, 2, 2, 1, -1}; int dy[] = {-1, 1, 2, 2, 1, -1, -2, -2}; void bfs() { q.push(node(x, y)); a[x][y] = 0; vis[x][y] = 1; while (!q.empty()) { int x = q.front().x; int y = q.front().y; q.pop(); for (int i = 0; i < 8; i++) { int xx = x + dx[i]; int yy = y + dy[i]; if (xx >= 1 && yy >= 1 && xx <= n && yy <= m && !vis[xx][yy]) { a[xx][yy] = a[x][y] + 1; q.push(node(xx, yy)); vis[xx][yy] = 1; } } } } int main() { cin >> n >> m >> x >> y; memset(a, -1, sizeof a); bfs(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { printf("%-5d", a[i][j]); } cout << endl; } return 0; }
- 1
信息
- ID
- 2495
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 7
- 标签
- 递交数
- 738
- 已通过
- 150
- 上传者