help,我现在晕晕的,谁来救救我?
查看原帖
help,我现在晕晕的,谁来救救我?
694295
yuanbingtao楼主2022/7/6 14:13
#include <iostream>
#include <queue>

using namespace std;

const char f[] = { 'E', 'W', 'S', 'N' };

int n, m, sx, sy, ex, ey, ans;
char c;
int a[55][55];

struct infn {
    int x, y, sum;
    char c;
};

queue<infn> q;

int main() {
    cin >> n >> m;
    for (int i = 0; i <= m; i++) {
        a[0][i] = a[n][i] = 1;
    }
    for (int i = 0; i <= n; i++) {
        a[i][0] = a[i][m] = 1;
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1, x; j <= m; j++) {
            cin >> x;
            if (x == 1) {
                a[i][j] = 1, a[i][j - 1] = 1, a[i - 1][j - 1] = 1, a[i - 1][j] = 1;
            }
        }
    }
    cin >> sx >> sy >> ex >> ey >> c;
    q.push({ sx, sx, 0, c });
    while (!q.empty()) {
        infn u = q.front();
        if (u.x == ex && u.y == ey) {
            cout << u.sum << endl;
            return 0;
        }
        for (int i = 0; i < 4; i++) {
            if (f[i] == u.c) {
                for (int j = 1; j <= 3; j++) {
                    switch (u.c) {
                    case 'E':
                        if (a[u.x - j][u.y] == 0) {
                            q.push({ u.x - j, u.y, u.sum + 1, 'E' });
                        }
                        break;
                    case 'W':
                        if (a[u.x + j][u.y] == 0) {
                            q.push({ u.x + j, u.y, u.sum + 1, 'W' });
                        }
                        break;
                    case 'N':
                        if (a[u.x][u.y - j] == 0) {
                            q.push({ u.x, u.y - j, u.sum + 1, 'N' });
                        }
                        break;
                    case 'S':
                        if (a[u.x][u.y + j] == 0) {
                            q.push({ u.x, u.y + j, u.sum + 1, 'S' });
                        }
                        break;
                    }
                }
            }
            else {
                q.push({ u.x, u.y, u.sum + 1, f[i] });
            }
        }
    }
    cout << -1 << endl;
    return 0;
}

地图打好啦,但后面就

2022/7/6 14:13
加载中...