蒟蒻求助 bfs悬关!!!
  • 板块P1443 马的遍历
  • 楼主Noldo
  • 当前回复2
  • 已保存回复2
  • 发布时间2024/3/23 17:01
  • 上次更新2024/3/23 19:20:42
查看原帖
蒟蒻求助 bfs悬关!!!
1179393
Noldo楼主2024/3/23 17:01
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<cstring>
#include<queue>
using namespace std;
const int N = 405;
typedef pair<int, int> PII;
queue<PII>q;
int n, m, sx, sy;
int bx[] = { -2,-1,1,2,2 ,1 ,-1 };
int by[] = { 1 ,2 ,2,1,-1,-2,-2 };
int steep[N][N];
void bfs(int x, int y) {
    steep[x][y] = 0;
    q.push({ x,y });
    while (!q.empty()) {
        auto t = q.front();
        q.pop();
        for (int i = 0; i < 8; i++) {
            int a = t.first + bx[i];
            int b = t.second + by[i];
            if (a<1 || b<1 || a>n || b>m||steep[a][b]!=-1)continue;
            steep[a][b] = steep[t.first][t.second] + 1;
            q.push({ a,b });
        }
    }
}
int main() {
    cin >> n >> m >> sx >> sy;
    memset(steep,-1, sizeof steep);
    bfs(sx, sy);
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            printf("%-5d", steep[i][j]);
        }
        cout << endl;
    }
    return 0;
}
2024/3/23 17:01
加载中...