#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxN = 1e3 + 7;
int n, m;
char mp[maxN][maxN];
bool vis[maxN][maxN][4];
struct node {
int x, y, s, d;
node(int a = 0, int b = 0, int c = 0, int _d = 0) {
x = a; y = b; s = c; d = _d;
}
};
queue<node> Q;
const int dir[4][2] = {
-1, 0,
0, -1,
0, 1,
1, 0
};
bool In_map(int x, int y) { return x >= 1 && y >= 1 && x <= n && y <= m; }
void bfs() {
while(!Q.empty()) Q.pop();
int sta = 0;
if(mp[1][1] == '@') sta = 1;
if(mp[1][1] == '#') sta = 2;
Q.push(node(1, 1, sta, 0));
vis[1][1][sta] = true;
while(!Q.empty()) {
node now = Q.front();
Q.pop();
int x = now.x, y = now.y;
sta = now.s;
if(sta == 3) {
printf("%d\n", now.d);
return;
}
for(int i = 0, xx, yy; i < 4; i ++) {
xx = x + dir[i][0];
yy = y + dir[i][1];
sta = now.s;
if(!In_map(xx, yy) || mp[xx][yy] == '*') continue;
if(mp[xx][yy] == '@') sta |= 1;
if(mp[xx][yy] == '#') sta |= 2;
if(vis[xx][yy][sta]) continue;
vis[xx][yy][sta] = true;
Q.push(node(xx, yy, sta, now.d + 1));
}
}
}
int main() {
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i ++) scanf("%s", mp[i] + 1);
bfs();
return 0;
}
记不住就发一下
最新推荐文章于 2024-10-02 15:23:19 发布