1 solutions
-
-1
#include <bits/stdc++.h> using namespace std; const int N = 410; int mp[N][N]; int n, m, sx, sy; int dx[] = {-1, 1, 2, 2, 1, -1, -2, -2}; int dy[] = {-2, -2, -1, 1, 2, 2, 1, -1}; struct node { int x, y, t; }; queue<node> q; int main() { cin >> n >> m >> sx >> sy; // memset 给数组全部赋值, // 这里给 mp 数组全部赋值为 -1 // memset 只能赋值 0 , -1 ,极大值(127) memset(mp, -1, sizeof(mp)); mp[sx][sy] = 0; q.push({sx, sy, 0}); // 开始广搜 while (!q.empty()) { node tmp = q.front(); q.pop(); // 弹出 int x = tmp.x; int y = tmp.y; int t = tmp.t; // 拓展其他点 for (int i = 0; i < 8; i++) { int nx = x + dx[i]; int ny = y + dy[i]; // 1. 越界判断 if (nx < 1 || nx > n || ny < 1 || ny > m) continue; // 2. 访问判断 if (mp[nx][ny] != -1) continue; // 到(x,y) 的时间是t ,到(nx,ny)的时间是 t+1 mp[nx][ny] = t + 1; q.push({nx, ny, t + 1}); } } // 输出所有的时间,就是 mp for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cout << mp[i][j] << " "; } cout << endl; } return 0; }
Information
- ID
- 5556
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- 3
- Tags
- # Submissions
- 60
- Accepted
- 24
- Uploaded By