求助求助,BFS20分,样例通过

P1443 马的遍历

```cpp #include <iostream> #include <queue> using namespace std; struct Node{ int x; int y; int step; }; int board[401][401]={0}; int dx[] = {-1, 1, -2, -2, -1, 1, 2, 2}; int dy[] = {2, 2, 1, -1, -2, -2, 1, -1}; bool vis[401][401]={0}; int main() { queue<Node> nodes; int n, m, x, y; scanf("%d%d%d%d", &n, &m, &x, &y); int xx=x, yy=y; vis[x][y] = 1; board[x][y] = -1; for(int i=0;i<8;i++){ if(xx+dx[i]>0&&xx+dx[i]<=n&&yy+dy[i]>0&&yy+dy[i]<=m){//this nodes.push((Node){xx+dx[i], yy+dy[i], 1}); vis[xx+dx[i]][yy+dy[i]] = 1; board[xx+dx[i]][yy+dy[i]] = 1; } } while(!nodes.empty()){ Node temp = nodes.front(); nodes.pop(); xx = temp.x; yy = temp.y; for(int i=0;i<8;i++){ if(xx+dx[i]>0&&xx+dx[i]<=n&&yy+dy[i]>0&&yy+dy[i]<=m&&!vis[xx+dx[i]][yy+dy[i]]){//this nodes.push((Node){xx+dx[i], yy+dy[i], temp.step+1}); vis[xx+dx[i]][yy+dy[i]] = 1; board[xx+dx[i]][yy+dy[i]] = temp.step+1; } } } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(board[i][j]==-1){ board[i][j] = 0; } else if(board[i][j]==0){ board[i][j] = -1; } } } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++) printf("%d ", board[i][j]); printf("\n"); } return 0; } ```
by Tangjiale @ 2023-01-08 22:28:26


55行应该是 ``` printf("%-5d",board[i][j]); ```
by Tx1234567 @ 2023-01-24 16:21:33


|