题目:1101. 献给阿尔吉侬的花束
思路:bfs
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int >PII;
const int N=1e5+10;
const int mod=100000007;
int fx[4]={1,-1,0,0},fy[4]={0,0,1,-1};
int t;
int r,c;
char a[205][205];
bool sta[205][205];
int main(){
cin>>t;
while(t--){
memset(sta,0,sizeof sta);
cin>>r>>c;
queue<pair<PII,int >> q;
int s1=0,s2=0,e1=0,e2=0;
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
//scanf("%c",&a[i][j]);
cin>>a[i][j];
if(a[i][j]=='S') {
PII t={i,j};
q.push({t,0});
s1=i,s2=j;
}
else if(a[i][j]=='E'){
e1=i,e2=j;
}
}
}
//for(int i=0;i<r;i++){
// for(int j=0;j<c;j++){
// cout<<a[i][j];
// }
// cout<<endl;
//}
//cout<<s1<<s2<<endl;
// cout<<e1<<e2<<endl;
bool flag=0;
while(q.size()){
PII t=q.front().first;
int cc=q.front().second;
int x=t.first,y=t.second;
//cout<<endl<<x<<y;
q.pop();
if(x==e1&&y==e2){
printf("%d\n",cc);
flag=1;
break;
}
for(int i=0;i<4;i++){
int xx=x+fx[i];
int yy=y+fy[i];
//cout<<endl<<xx<<yy;
if(xx<0||xx>=r||yy<0||yy>=c) continue;
if(!sta[xx][yy]&&a[xx][yy]!='#'){
q.push({{xx,yy},cc+1});
sta[xx][yy]=1;
}
}
}
if(flag==0){
puts("oop!");
}
}
return 0;
}