30分蒟蒻求助

P1443 马的遍历

3 4 6 7 8 9MLE 5TLE
by lz081101 @ 2023-07-07 21:48:05


参考一下: ```c #include<bits/stdc++.h> #define st first #define nd second using namespace std; using pii = pair<int, int>; int n, m, ans, xm, ym, d[405][405]; const int dx[] = {1, 2, -1, 2, 1, -2, -1, -2}; const int dy[] = {2, 1, 2, -1, -2, 1, -2, -1}; bool cheak(int x, int y) { return 1 <= x && x <= n && 1 <= y && y <= m && d[x][y] == -1; } queue<pii> q; int main() { scanf("%d%d%d%d", &n, &m, &xm, &ym); q.push({xm, ym}); memset(d, -1, sizeof d); d[xm][ym] = 0; while(!q.empty()) { pii u = q.front(); q.pop(); int x = u.st, y = u.nd; for(int i = 0;i < 8;i++) { int nx = x + dx[i], ny = y + dy[i]; if(!cheak(nx, ny)) continue; d[nx][ny] = d[x][y] + 1; q.push({nx, ny}); } } for(int i = 1;i <= n;i++) { for(int j = 1;j <= m; j++) printf("%-5d", d[i][j]); printf("\n"); } return 0; } ```
by __zhy__ @ 2023-07-07 22:30:15


@[lz081101](/user/1034276)
by __zhy__ @ 2023-07-07 22:31:36


@[zhenghaoyi](/user/716006) 谢谢已经过了
by lz081101 @ 2023-07-08 12:55:44


|