求条70分代码

P1649 [USACO07OCT] Obstacle Course S

Bismuth_ @ 2024-10-14 17:25:44

rt,代码判太多-1

n#include <bits/stdc++.h>
using namespace std;
char a[45][45];

int dirx[] = {0, 0, 1, -1}, diry[] = {1, -1, 0, 0}, n, m, cnt, sx, sy, ex, ey, ans = INT_MAX;
bool vis[45][45], fl;

struct point {
    int x, y, step, way;
};

void bfs() {
    queue<point>q;
    for (int i = 0; i < 4; i++)
        if (a[sx + dirx[i]][sy + diry[i]] == '.')
            q.push({sx + dirx[i], sy + diry[i], 0, i});
    while (!q.empty()) {
        point now = q.front();
        q.pop();
        int x = now.x, y = now.y, step = now.step, way = now.way;
        //cout << x << " " << y << " " << step << " " << way << '\n';
        if (x == ex && y == ey) {
            ans = min(ans, step);
            fl = 1;
            continue;
        }
        vis[x][y] = true;
        for (int i = 0; i < 4; i++) {
            if (x + dirx[i] < 1 || x + dirx[i] > n)
                continue;
            if (y + diry[i] < 1 || y + diry[i] > m)
                continue;
            if (vis[x + dirx[i]][y + diry[i]])
                continue;
            if (a[x + dirx[i]][y + diry[i]] == 'x')
                continue;
            q.push({x + dirx[i], y + diry[i], step + (int)(i != way), i});
        }
    }
}

int main() {
    cin >> n;
    m = n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> a[i][j];
            if (a[i][j] == 'A')
                sx = i, sy = j;
            if (a[i][j] == 'B')
                ex = i, ey = j;
        }
    }
    //cout<<sx<<" "<<
    bfs();
    if (!fl)
        cout << -1;
    else
        cout << ans;
    return 0;
}

|