AcWing 3179. 穿越雷区
原题链接
简单
作者:
liaoyu
,
2022-02-24 21:29:52
,
所有人可见
,
阅读 184
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
typedef pair<int,int> PII;
char a[105][105];
int n;
int sx,sy,ex,ey;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int dist[105][105];
bool st[105][105];
int bfs(){
queue<PII> q;
q.push({sx,sy});
dist[sx][sy] = 0;
while(!q.empty()){
auto t = q.front();
q.pop();
int x = t.first,y = t.second;
// cout << "(" << x << "," << y << ")" << endl;
if(x==ex && y==ey) return dist[x][y];
if(st[x][y]) continue;
st[x][y] = 1;
for(int i=0;i<4;i++){
int tx = x + dx[i];
int ty = y + dy[i];
if(tx>0 && tx<=n && ty>0 && ty<=n && !st[tx][ty] && ((a[x][y]-'0')*(a[tx][ty]-'0')==2) || a[x][y]=='A' || a[tx][ty]=='B'){
q.push({tx,ty});
dist[tx][ty] = dist[x][y]+1;
}
}
}
return -1;
}
int main()
{
cin >> n;
char ch;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
cin >> ch;
if(ch=='+'){
ch = '1';
}else if(ch=='-'){
ch = '2';
}else if(ch=='A'){
sx = i,sy = j;
}else if(ch=='B'){
ex = i,ey = j;
}
a[i][j] = ch;
}
cout << bfs() << endl;
return 0;
}