P2130 狂奔的Wzf
P2130 狂奔的Wzf
题意简述
从
题目分析
注意点
这一题与其他迷宫题不同之处在于可以走
其中
有了前缀和,我们则可以判断点
其他则就同普通的迷宫一般了,具体看代码。
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1000;
struct node {
int x, y, t;
} ;
int n, m, h[N][N], l[N][N];
int d[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int p[10] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512};
bool vis[N][N];
char c[N][N];
int main() {
cin >> n >> m;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) {
cin >> c[i][j];
h[i][j] = h[i-1][j]+(c[i][j] == 'X');
l[i][j] = l[i][j-1]+(c[i][j] == 'X');
}
queue <node> q;
q.push((node){1, 1, 0}), vis[1][1] = 1;
while (!q.empty()) {
node now = q.front(); q.pop();
if (c[now.x][now.y] == '#') {
cout << now.t;
return 0;
}
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 10; ++j) {
int x = now.x+d[i][0]*p[j], y = now.y+d[i][1]*p[j];
if (x < 1 || x > n || y < 1 || y > m || vis[x][y]) continue;
if (c[x][y] == 'X') break;
if (d[i][0] && h[x][y]-h[now.x][now.y]) break;
if (d[i][1] && l[x][y]-l[now.x][now.y]) break;
q.push((node){x, y, now.t+1}), vis[x][y] = 1;
}
}
cout << -1;
return 0;
}
record。