一起来救天使了,O(∩_∩)O~
一起来救天使了!
#include <iostream>
#include <queue>
using namespace std;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
const int MAX_SIZE = 202;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
typedef struct
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int x, y;
int cc;//花费时间
}Node;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
int dir[4][2] =
{
{-1,0},
{1,0},
{0,-1},
{0,1}};
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
int cost[MAX_SIZE][MAX_SIZE];
char graph[MAX_SIZE][MAX_SIZE];
int m, n;
int sx, sy,//起点
dx, dy;//终点
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void init()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(graph[i][j] == 'r')
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{sx = i; sy = j;}
if(graph[i][j] == 'a')
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{dx = i; dy = j;}
cost[i][j] = -1;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
bool isBound(int x, int y)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
if(x < 0 || y < 0)
return false;
if(x >= m || y >= n)
return false;
return true;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void bfs()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
Node a,b;
int i;
queue<Node> Q;
//----------------------------
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
a.x = sx;
a.y = sy;
a.cc = 0;
cost[a.x][a.y] = 0;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//----------------------------
Q.push(a);
while(!Q.empty())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
a = Q.front();
Q.pop();
for(i = 0; i < 4; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
b.x = a.x + dir[i][0];
b.y = a.y + dir[i][1];
b.cc = a.cc + 1;
if(graph[b.x][b.y] == '#'|| !isBound(b.x, b.y))
continue;
if(graph[b.x][b.y] == 'x')
b.cc++;//砍死卫兵加时间
if(cost[b.x][b.y] == -1 || b.cc < cost[b.x][b.y])
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Q.push(b);
cost[b.x][b.y] = b.cc;
}
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
int main()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int i;
while(cin>>m>>n)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for(i = 0; i < m; i++)
cin>>graph[i];
init();
bfs();
if(cost[dx][dy] == -1)
cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
else
cout<<cost[dx][dy]<<endl;
}
return 0;
}