RedHat V5! Orz! #include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int Maxn = 25;
int vis[Maxn][Maxn];
char Map[Maxn][Maxn];
int fx[4] = {0,0,1,-1};
int fy[4] = {1,-1,0,0};
struct Point
{
int x;
int y;
int step;
};
int n,m;
int bfs(int x1,int y1,int x2,int y2)
{
memset(vis,0,sizeof(vis));
Point Start,End,Temp;
queue<Point>q;
vis[x1][y1] = 1;
Start.x = x1;
Start.y = y1;
Start.step = 0;
q.push(Start);
while(!q.empty())
{
Temp = q.front();
if(Temp.x == x2 && Temp .y == y2)
{
return Temp.step;
break;
}
q.pop();
for(int i=0;i<4;i++)
{
End.x = fx[i] + Temp.x;
End.y = fy[i] + Temp.y;
if(End.x >= 0 && End.x <= n && End.y >= 0 && End.y <= m && !vis[End.x][End.y] && Map[End.x][End.y] != '#')
{
End.step = Temp.step + 1;
vis[End.x][End.y] = 1;
q.push(End);
}
}
}
return -1;
}
int main()
{
int x1,x2,y1,y2;
while(~scanf("%d%d",&n,&m))
{
for(int i=0;i<n;i++)
{
getchar();
for(int j=0;j<m;j++)
{
scanf("%c",&Map[i][j]);
if(Map[i][j] == '*')
{
x1 = i;
y1 = j;
}
if(Map[i][j] == '$')
{
x2 = i;
y2 = j;
}
}
}
int ans = bfs(x1,y1,x2,y2);
if(ans != -1)
printf("RedHat V5!\n");
else
printf("Orz!\n");
}
}
|