题目意思

存在格子规定方向,存在格子随意走动,不存在不能走的格子,只需要输出可以到达终点的某一条路径即可。

解题思路

BFS,直接对地图剖解,把方向字母用数字代替)其实也可以不换后面明显一点就是了,后面通过BFS记录当前走的方向,走到终点。
再一次方向走图,记录每个点是如何过来的。
再输出答案就行了!

#pragma GCC target("avx,sse2,sse3,sse4,popcnt")
#pragma GCC optimize("O2,O3,Ofast,inline,unroll-all-loops,-ffast-math")
#include <bits/stdc++.h>
using namespace std;
#define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define all(vv) (vv).begin(), (vv).end()
#define endl "\n"
#define pai pair<int, int>
#define fi first
#define se second
typedef long long ll; typedef unsigned long long ull; typedef long double ld;
const ll MOD = 1e9 + 7;
inline ll read() { ll s = 0, w = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') w = -1; for (; isdigit(ch); ch = getchar())    s = (s << 1) + (s << 3) + (ch ^ 48); return s * w; }
inline void write(ll x) { if (!x) { putchar('0'); return; } char F[40]; ll tmp = x > 0 ? x : -x; if (x < 0)putchar('-');    int cnt = 0;    while (tmp > 0) { F[cnt++] = tmp % 10 + '0';        tmp /= 10; }    while (cnt > 0)putchar(F[--cnt]); }
inline ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll qpow(ll a, ll b) { ll ans = 1;    while (b) { if (b & 1)    ans *= a;        b >>= 1;        a *= a; }    return ans; }    ll qpow(ll a, ll b, ll mod) { ll ans = 1; while (b) { if (b & 1)(ans *= a) %= mod; b >>= 1; (a *= a) %= mod; }return ans % mod; }
inline int lowbit(int x) { return x & (-x); }

const int N = 1000 + 7;
int mp[N][N];
int vis[N][N];
int p[N][N];
int n, m, sx, sy, tx, ty;
int dx[] = { 0,-1,1,0,0 };
int dy[] = { 0,0,0,-1,1 };

int main() {
    js;
    cin >> n >> m >> sx >> sy >> tx >> ty;
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j) {
            char ch;    cin >> ch;
            if (ch == '.')    mp[i][j] == 0;
            else if (ch == 'w')    mp[i][j] = 1;
            else if (ch == 's')    mp[i][j] = 2;
            else if (ch == 'a')    mp[i][j] = 3;
            else if (ch == 'd')    mp[i][j] = 4;
        }
    queue<pai> q;
    q.push({ sx,sy });
    vis[sx][sy] = 1;
    while (q.size()) {
        pai now = q.front(); q.pop();
        int x = now.first, y = now.second;
        if (x == tx and y == ty)    break;
        if (mp[x][y]) {
            int xx = x + dx[mp[x][y]], yy = y + dy[mp[x][y]];
            if (xx >= 1 and xx <= n and yy >= 1 and yy <= m and !vis[xx][yy])
                q.push({ xx,yy }), vis[xx][yy] = 1, p[xx][yy] = mp[x][y];
        }
        else {
            for (int i = 1; i <= 4; ++i) {
                int xx = x + dx[i], yy = y + dy[i];
                if (xx >= 1 and xx <= n and yy >= 1 and yy <= m and !vis[xx][yy])
                    q.push({ xx,yy }), vis[xx][yy] = 1, p[xx][yy] = i;
            }
        }
    }
    int x = tx, y = ty;
    while (x != sx or y != sy) { //反向走回起点并记录路程
        int xx = x - dx[p[x][y]], yy = y - dy[p[x][y]];
        mp[xx][yy] = p[x][y];
        x = xx, y = yy;
    }
    cout << n << ' ' << m << ' ' << sx << ' ' << sy << ' ' << tx << ' ' << ty << endl;
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j)
            if (!mp[i][j] or mp[i][j] == 1)    cout << 'w'; //终点没走过,随便输出一个
            else if (mp[i][j] == 2)    cout << 's';
            else if (mp[i][j] == 3)    cout << 'a';
            else if (mp[i][j] == 4)    cout << 'd';
        cout << endl;
    }
    return 0;
}