???

P1443 马的遍历

@[rjh1012](/user/1111860) 您看一下? ``` #include <bits/stdc++.h> using namespace std; int flag[405][405]; int hor[1000][1000]={{0},{0}}; int m, n, x, y; int dx[] = {-2, -2, 2, 2, -1, -1, 1, 1}; int dy[] = {1, -1, 1, -1, 2, -2, 2, -2}; int bfs() { /*memset(flag, 0, sizeof(flag)); int row, col, r, c, qsize, step = 0; queue<int> qrow, qcol; qrow.push(x); qcol.push(y); flag[x][y] = 1; while (!qrow.empty()) { ++step; qsize = qrow.size(); while (qsize--) { row = qrow.front(); col = qcol.front(); qrow.pop(); qcol.pop(); for (int i = 0; i < 8; ++i) { r = row + dx[i]; c = col + dy[i]; if (r < 1 || c < 1 || r > n || c > m) continue; if (flag[r][c]) continue; if (r == fr && c == fc) { return step; } qrow.push(r); qcol.push(c); flag[r][c] = 1; } } } return -1; }/*/ queue<int> x1; queue<int> y1; x1.push(x); y1.push(y); while(!x1.empty()){ for(int i=0;i<8;i++){ int xx=x1.front()+dx[i]; int yy=y1.front()+dy[i]; if(xx>=1 && xx<=n && yy>=1 && yy<=m && !flag[xx][yy]) { flag[xx][yy]=1; hor[xx][yy]=hor[x1.front()][y1.front()]+1; x1.push(xx); y1.push(yy); } } x1.pop(); y1.pop(); } } int main() { cin >> n >> m >> x >> y; memset(hor,-1,sizeof(hor)); hor[x][y]=0; flag[x][y]=1;/* for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (i == x && j == y) cout << 0 << ' '; else cout << bfs(i, j) << ' '; } cout << endl; }/*/ bfs(); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ printf("%d ",hor[i][j]); } cout << "\n"; } return 0; } ```
by rwhy @ 2024-02-03 21:38:24


|