WA 0 求调
查看原帖
WA 0 求调
1293918
CaoGenJY楼主2024/12/5 11:46
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
#include <iomanip>
#include <cstdio>

using namespace std;

const int N = 410;
int stx, sty, n, m;
int dx[] = {-2, -1, 1, 2, 2, 1, -1, -2};
int dy[] = {1, 2, 2, 1, -1, -2, -2, -1};

struct node{
    int x, y;
    int step;
};

void bfs(int xx, int yy){
    int st[N][N];
    memset(st, 0, sizeof(st));
    queue <node> q;
    node t;
    t.x = stx, t.y = sty, t.step = 0;
    q.push(t);
    while(!q.empty()){
        node u = q.front();
        st[u.x][u.y] = 1;
        q.pop();
        if(u.x == xx && u.y == yy){
            printf("%-5d", u.step);
            return;
        }
        for(int i = 0; i < 8; i ++ ){
            int tx = u.x + dx[i];
            int ty = u.y + dy[i];
            if(st[tx][ty] || tx <= 0 || tx > n || ty <= 0 || ty > n) continue;
            node n;
            n.x = tx;
            n.y = ty;
            n.step = u.step + 1;
            q.push(n);
        }
    }
    printf("%-5d", -1);
}


int main(){
    cin >> n >> m >> stx >> sty;
    for(int i = 1; i <= n; i ++ ){
        for(int j = 1; j <= m; j ++ ){
            bfs(i, j);
        }
        cout << endl;
    }
}
2024/12/5 11:46
加载中...