90分求助
查看原帖
90分求助
748102
dy_dyy楼主2024/1/20 11:30
#include <bits/stdc++.h>
using namespace std;
int x1,x2,xx[] = {-2,-1,1,2,-2,2,-2,2,-2,-1,1,2},yy[]= {-2,-2,-2,-2,-1,-1,1,1,2,2,2,2};
bool v[25][25];
int ans[25][25];
struct node{
    int x,y;
};
queue<node> dui; 

void bfs(int cx,int cy){
    dui.push({cx,cy});
    while(!dui.empty()){
        node nn = dui.front();
        dui.pop();
        for(int i = 0;i < 12;i++){
            int pp = nn.x + xx[i];
            int qq = nn.y + yy[i];
            if(pp < 1 || pp > 20 || qq < 1 || pp > 20 || v[pp][qq] == 1){
                continue;
            }
            dui.push({pp,qq});
            v[pp][qq] = 1;
            ans[pp][qq] = ans[nn.x][nn.y] + 1;
        }
    }
}
int main() {
    for(int f = 0;f < 2;f++){
        memset(ans,0,sizeof(ans));
        memset(v,0,sizeof(v));
        cin >> x1 >> x2;
        v[x1][x2] = 1;
        bfs(x1,x2);
        cout << ans[1][1] << endl;
        
    }
    return 0;
}
2024/1/20 11:30
加载中...