hdu1242 dfs剪枝

来源:互联网 发布:inphic i9软件格式 编辑:程序博客网 时间:2024/05/29 18:13

Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 30765    Accepted Submission(s): 10808


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
 

Author
CHEN, Xue
 

Source
ZOJ Monthly, October 2003
 

Recommend
Eddy   |   We have carefully selected several similar problems for you:  1240 1016 1072 1241 1253 

 

题意;求r到a的最短距离。

思路写在注释上了

/* 最近在做dfs剪枝,这个题也可以用bfs做。这里一个有力的剪枝就是 map1【i】【j】,表示到大x行y列的最少步数,这次到达x,y比之前的大,就没必要深搜下去了,否则,则继续深搜*/#include<stdio.h>#include<string.h>#include<stdlib.h>int n,m;char map[210][210];int map1[210][210];int x,y,zx,zy,ans;int vis[210][210];int dir[4][2]={1,0,-1,0,0,1,0,-1};int jude(int x,int y){    if(x<=n&&x>=1&&y>=1&&y<=m) return 1;    return 0;}void dfs(int x,int y,int count){ // printf("%d %d %d\n",x,y,count);    if(x==zx&&y==zy)    {        if(ans>count) ans=count;        return ;    }    for(int i=0;i<4;i++)    {        int xx=x+dir[i][0];        int yy=y+dir[i][1];        if(!jude(xx,yy)) continue;        if(!vis[xx][yy]&&map[xx][yy]!='#')        {   vis[xx][yy]=1;            if(map[xx][yy]=='x')            {   if(count+2<map1[xx][yy])//当这次到达xx,yy比之前的小                            {   map1[xx][yy]=count+2;                                dfs(xx,yy,count+2);                                 }            }            else                {if(count+1<map1[xx][yy])                    {                     map1[xx][yy]=count+1;                     dfs(xx,yy,count+1);                    }                }            vis[xx][yy]=0;        }    }    return ;}int main(){    while(~scanf("%d%d",&n,&m))    {  getchar();        for(int i=1;i<=n;i++)        {            for(int j=1;j<=m;j++)                {                    scanf("%c",&map[i][j]);                    if(map[i][j]=='a') {zx=i,zy=j;}                    if(map[i][j]=='r'){x=i,y=j;}                  map1[i][j]=999999999;//因为是求小的所以这里保存一个大的值。                }                getchar();           }         /*for(int i=1;i<=n;i++)         {             for(int j=1;j<=m;j++)                printf("%c",map[i][j]);             printf("\n");         }*/         //  printf("%d %d\n",zx,zy);            ans=999999999;            memset(vis,0,sizeof(vis));            vis[x][y]=1;            dfs(x,y,0);            if(ans==999999999) printf("Poor ANGEL has to stay in the prison all his life.\n");            else printf("%d\n",ans);    }}

原创粉丝点击