B - Rescue
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit
Status
Description
Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.
Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.
You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
Input
First line contains two integers stand for N and M.
Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.
Process to the end of the file.
Output
For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life."
Sample Input
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
Sample Output
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit
Status
Description
Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.
Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.
You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
Input
First line contains two integers stand for N and M.
Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.
Process to the end of the file.
Output
For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life."
Sample Input
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
Sample Output
13
1.题意:x代表警卫,点代表路,#代表墙,r代表你的位置,a代表公主的位置。走一条路耗费1秒,打死警卫耗费1秒,问你最少多长时间能见到公主。
2.思路:从你的位置开始搜索,将所有路看成点,每个点之间可能有连线,此图就是一张带权图。试想从节点出发,访问该节点,可能有几个点与之相通,那么该选择哪一步呢,没错,当然是耗时最小到达的了,那么是否最优情况是否一定是选择此点呢,答案是否定的,但是,我们可以用优先队列先处理该点,如果该点沿此点的路线最先找到了终点,那么就应该选择该点,如果沿此路线没有沿其他的点快,那么我们得到的答案也是正确的,因为我们通过其他点走到终点的时间比选择每步最优的情况要好。即用到了排除法。在优先队列的选择过程之中也含有递归的思想。
3.失误:
4.代码如下:
bfs:
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int MAXN=1e3+10;
int m,n,sx,sy,ex,ey;
char map[MAXN][MAXN];
int dir[5][2]={ //方向
0,0,
1,0,
-1,0,
0,1,
0,-1
};
struct node{
int x;
int y;
int step;
friend bool operator <(node n1,node n2)
{
return n1.step>n2.step;//右边的优先级高,即小的在队头
}
};
bool judge(node go)//搜索条件
{
bool a=go.x>=1&&go.x<=n;
bool b=go.y>=1&&go.y<=m;
bool c=map[go.x][go.y]!='#';
return a&&b&&c;
}
int bfs(int x,int y)
{
priority_queue<node> que;
node spos;
spos.x=x;
spos.y=y;
spos.step=0;
que.push(spos);
while(!que.empty())
{
node now;
now=que.top();
que.pop();
if(now.x ==ex&&now.y ==ey) return now.step;
for(int i=1;i<=4;++i)
{
node go;
go.x=now.x+dir[i][0];
go.y=now.y+dir[i][1];
if(judge(go))
{
if(map[go.x][go.y]=='x')
{
go.step=now.step+2;
}
else
{
go.step=now.step+1;
}
map[go.x][go.y]='#';//先走到这一点,然后清除
que.push(go);
}
}
}
return -1;
}
int main()
{
int i,j;
while(~scanf("%d%d",&n,&m))
{
getchar();
for(i=1;i<=n;++i)
{
for(j=1;j<=m;++j)//写成++i,无输入
{
scanf("%c",&map[i][j]);
if(map[i][j]=='a')
{
ex=i;
ey=j;
}
else if(map[i][j]=='r')
{
sx=i;sy=j;
}
}
getchar();//经常忘记 常提醒
}
int ans=bfs(sx,sy);
if(ans==-1)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",ans);
}
return 0;
}
#include<cstring>
#include<queue>
using namespace std;
const int MAXN=1e3+10;
int m,n,sx,sy,ex,ey;
char map[MAXN][MAXN];
int dir[5][2]={ //方向
0,0,
1,0,
-1,0,
0,1,
0,-1
};
struct node{
int x;
int y;
int step;
friend bool operator <(node n1,node n2)
{
return n1.step>n2.step;//右边的优先级高,即小的在队头
}
};
bool judge(node go)//搜索条件
{
bool a=go.x>=1&&go.x<=n;
bool b=go.y>=1&&go.y<=m;
bool c=map[go.x][go.y]!='#';
return a&&b&&c;
}
int bfs(int x,int y)
{
priority_queue<node> que;
node spos;
spos.x=x;
spos.y=y;
spos.step=0;
que.push(spos);
while(!que.empty())
{
node now;
now=que.top();
que.pop();
if(now.x ==ex&&now.y ==ey) return now.step;
for(int i=1;i<=4;++i)
{
node go;
go.x=now.x+dir[i][0];
go.y=now.y+dir[i][1];
if(judge(go))
{
if(map[go.x][go.y]=='x')
{
go.step=now.step+2;
}
else
{
go.step=now.step+1;
}
map[go.x][go.y]='#';//先走到这一点,然后清除
que.push(go);
}
}
}
return -1;
}
int main()
{
int i,j;
while(~scanf("%d%d",&n,&m))
{
getchar();
for(i=1;i<=n;++i)
{
for(j=1;j<=m;++j)//写成++i,无输入
{
scanf("%c",&map[i][j]);
if(map[i][j]=='a')
{
ex=i;
ey=j;
}
else if(map[i][j]=='r')
{
sx=i;sy=j;
}
}
getchar();//经常忘记 常提醒
}
int ans=bfs(sx,sy);
if(ans==-1)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",ans);
}
return 0;
}