#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <string>
#include <map>
#include <stack>
#include <queue>
using namespace std;
#define maxn 105
#define inf 0x3f3f3f3f
typedef long long LL;
int n,m;
char mp[maxn][maxn];
bool vis[maxn][maxn];
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
struct node{
int x,y;
int step;
node(){
}
node(int x,int y,int step):x(x),y(y),step(step){}
};
bool check(int x,int y){
if(x>=0&&y>=0&&x<=n&&y<=m&&!vis[x][y]&&mp[x][y]=='*')return true;
return false;
}
int bfs(){
queue<node>q;
q.push(node(0,0,0));
node tmp;
bool flag=0;
while(!q.empty()){
tmp=q.front();
q.pop();
if(tmp.x==n-1&&tmp.y==m-1){
flag=1;
break;
}
for(int i=0;i<4;i++){
int dx=tmp.x+dir[i][0];
int dy=tmp.y+dir[i][1];
if(check(dx,dy)){
vis[dx][dy]=1;
q.push(node(dx,dy,tmp.step+1));
}
}
}
if(flag)
return tmp.step;
return -1;
}
int main()
{
//freopen("in.txt","r",stdin);
while(cin>>n>>m){
for(int i=0;i<n;i++){
cin>>mp[i];
}
memset(vis,0,sizeof vis);
int ans=bfs();
if(ans==-1)cout<<"defeat"<<endl;
else cout<<"victory "<<ans<<endl;
}
}
迷宫问题之最短路径bfs
最新推荐文章于 2024-03-15 16:02:56 发布