90,wa on #9
查看原帖
90,wa on #9
1218883
yanghb666楼主2024/8/7 09:49
#include <bits/stdc++.h>
#define int long long
using namespace std;
int t[4][2] = {1, 0, -1, 0, 0, 1, 0, -1}, s[1010][1010][2], n, m, d, r;
char mp[1100][1100];
struct yy {
    int x, y, s, flag;
};
void bfs() {
    queue<yy> q;
    q.push({1, 1, 0, 0});
    memset(s, 0x3f3f3f, sizeof(s));
    s[1][1][0] = 0;
    while (!q.empty()) {
        yy zz = q.front();
        q.pop();
        for (int i = 0; i < 4; i++) {
            int x1 = zz.x + t[i][0], y1 = zz.y + t[i][1];
            if (x1 >= 1 && x1 <= n && y1 >= 1 && y1 <= m && mp[x1][y1] != '#') {
                if (zz.flag == 0 && (zz.x + d >= 1 && zz.x + d <= n && zz.y + r >= 1 && zz.y + r <= m) && mp[zz.x + d][zz.y + r] != '#' && zz.s + 1 < s[zz.x + d][zz.y + r][1]) q.push({zz.x + d, zz.y + r, zz.s + 1, 1}), s[zz.x + d][zz.y + r][1] = zz.s + 1;
                if (zz.s + 1 < s[x1][y1][zz.flag]) q.push({x1, y1, zz.s + 1, zz.flag}), s[x1][y1][zz.flag] = zz.s + 1;
            }
        }
    }
}
signed main() {
    cin >> n >> m >> d >> r;
    for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> mp[i][j];
    bfs();
    if (s[n][m][0] == s[n][m][1] == 0x3f3f3f3f) cout << -1;
    else cout << min(s[n][m][0], s[n][m][1]);
    return 0;
}
2024/8/7 09:49
加载中...