蓝桥杯-献给阿尔吉侬的花束
作者:互联网
链接:https://www.acwing.com/problem/content/1103/
代码:
#include<bits/stdc++.h>
using namespace std;
using PII=pair<int,int>;
int dx[]={0,-1,0,1};
int dy[]={1,0,-1,0};
char a[205][205];
int dis[205][205];
int n,m;
PII be,ed;
int bfs(){
queue<PII>q;
memset(dis,0,sizeof(dis));
q.push(be);
dis[be.first][be.second]=1;
while(q.size()){
int x=q.front().first;
int y=q.front().second;
q.pop();
for(int i=0;i<4;i++){
int l=x+dx[i];
int r=y+dy[i];
if(l<1||l>n||r<1||r>m||a[l][r]=='#')continue;
if(dis[l][r])continue;
else {
q.push({l,r});
dis[l][r]=dis[x][y]+1;
if(a[l][r]=='E')return dis[l][r];
}
}
}
return -1;
}
int main (){
int t;
cin>>t;
while(t--){
cin>>n>>m;
for(int i=1;i<=n;i++)
cin>>(a[i]+1);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
if(a[i][j]=='S')be={i,j};
else if(a[i][j]=='E')ed={i,j};
}
int ans=bfs();
if(ans==-1)cout<<"oop!"<<endl;
else cout<<ans-1<<endl;
}
return 0;
}
标签:205,int,花束,蓝桥,second,front,return,阿尔吉侬,dis 来源: https://www.cnblogs.com/abestxun/p/14661418.html