hdu1242 Rescue (dfs+剪枝)
2016-12-03 10:49
429 查看
Problem 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
思路
注意Angle朋友不止一个.
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
思路
注意Angle朋友不止一个.
/************************************************************************* > File Name: hdu1242.cpp > Author:gens_ukiy > Mail: > Created Time: 2016年12月02日 星期五 14时53分46秒 ************************************************************************/ #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<cstdlib> #include<climits> #include<string> #include<cstring> #include<vector> #include<set> #include<list> #include<map> int dire[4][2]={ {0,1},{1,0},{0,-1},{-1,0} }; int dire2[8][2]={{-1,-1},{-1,0},{-1,1},{ 0,-1},{ 0,1},{ 1,-1},{ 1,0},{ 1,1}}; #define rep(i,a,b) for(int i=(a);i<=(b);(i++)) #define inf 0x3f3f3f #define ll long long #define pi acos(-1) using namespace std; char a[205][205]; int n,m; int tot; int sx,sy; void dfs(int x,int y,int step){ int flag=0; if(x<1||x>n||y<1||y>m) return; if(a[x][y]=='#') return; if(step>tot) return; if(a[x][y]=='x'){ step++; flag=1; } if(a[x][y]=='r'){ tot=tot>step?step:tot; return; } rep(i,0,3){ int px=x+dire[i][0]; int py=y+dire[i][1]; a[x][y]='#'; dfs(px,py,step+1); if(flag==1) a[x][y]='x'; else a[x][y]='.'; } } int main() { #ifndef OnlineJudge //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); #endif while(cin>>n>>m){ tot=inf; rep(i,1,n){ scanf("%s",a[i]+1); rep(j,1,m) if(a[i][j]=='a') sx=i,sy=j; } dfs(sx,sy,0); if(tot==inf) printf("Poor ANGEL has to stay in the prison all his life.\n"); else printf("%d\n",tot); } return 0; }
相关文章推荐
- HDU 1242 Rescue (DFS+剪枝,一个起点多个终点)
- HDU 1242 Rescue(dfs深搜)
- HDU 1242 Rescue(DFS)
- HDU 1242 Rescue (BFS or DFS)
- HDU 1242 Rescue(DFS入门)
- 对BFS与DFS的一些看法 ( 以HDU 1242 Rescue为例 )
- hdu-1242 dfs+各种剪枝
- HDU 1242 Rescue 【DFS】
- HDU 1242 dfs+剪枝或BFS+优先队列 求最短路
- HDU 1242 -- Rescue (bfs,dfs)
- 模板:HDU 1242: Rescue: (BFS DFS)
- HDU 1242 Rescue(DFS or BFS)
- HDU 1242 Rescue - DFS 回溯
- HDU 1242 Rescue (DFS)
- HDU 1242 Rescue (搜索 DFS)
- hdu 1242 Rescue(方法一:BFS+优先队列,方法二:DFS)
- hdu 1242Rescue(DFS)
- HDU 1242 Rescue
- hdu 5676 ztr loves lucky numbers【dfs+剪枝】
- hdu 1242 Rescue