悬 1 关 求调
查看原帖
悬 1 关 求调
1123057
Solune楼主2025/1/20 23:10

WA on #8, 有调必关

#include <iostream>
#include <queue>
#include <cstring>
#include <climits>
using namespace std;

int n, sx, sy, fx, fy, ans = INT_MAX;
int xy[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1};
char c[110][110];
int dis[110][110];
struct node{
    int x, y, d, s;
};
queue<node> q;

void bfs(){
    while(!q.empty()){
        int x = q.front().x, y = q.front().y, d = q.front().d, s = q.front().s;
        q.pop();
        for(int i = 0; i < 4; ++i){
            int nx = x + xy[i][0], ny = y + xy[i][1], nd = i, ns; 
            if(nx >= 1 && nx <= n && ny >= 1 && ny <= n && c[nx][ny] != 'x'){
                if(d == -1 || d == nd) ns = s;
                else ns = s + 1;
                if(dis[nx][ny] == 0 || ns < dis[nx][ny]){
                    dis[nx][ny] = ns;
                    if(nx == fx && ny == fy){
                        ans = min(ans, ns);
                        continue;
                    }
                    q.push({nx, ny, nd, ns});
                }
            }
        }
    }
}

int main(){
    cin >> n;
    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= n; ++j){
            cin >> c[i][j];
            dis[i][j] = INT_MAX;
            if(c[i][j] == 'A') sx = i, sy = j, dis[i][j] = 0;
            else if(c[i][j] == 'B') fx = i, fy = j, dis[i][j] = 0;
            else if(c[i][j] == '.') dis[i][j] = 0;
        }
    }
    q.push({sx, sy, -1, 0});
    bfs();
    cout << ans;
    return 0;
}
2025/1/20 23:10
加载中...