Rescue DFS()+剪枝

http://acm.hdu.edu.cn/showproblem.php?pid=1242

 

Rescue

 

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


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
 
 
题意: 天使被恶魔抓在监狱中 天使的朋友去救她
.表示有路可走
#表示墙壁 无法通过
x表示 卫兵 想要通过 需要花费一个时间打败他
a表示 angle的位置
r表示朋友的初始位置
 
 
直接DFS()的话要超时 看了下大神的剪枝真的很好
#include<stdio.h>
#define max 32000
int n, m, time, visited[202][202], map[202][202];
int dir[4][2]= {1,0,-1,0,0,1,0,-1};
int ax, ay, ex, ey;
void dfs(int x, int y, int t)
{
    int i, ch, xn, yn;
    if(x == ex && y == ey)
    {// 如若走到目的地   比较花费的时间  取花费最短的时间
        if(t < time)
            time = t;
    }
    else
    {
        for(i = 0; i <4; ++i)
        {
            xn = x + dir[i][0];
            yn = y + dir[i][1];
            if((visited[xn][yn] == 1 || visited[xn][yn] == 2) && t + visited[xn][yn] <= time)
            {//如果有路或者有卫兵才能走 另外 要保证走这个点花费的时间小于之前的最短时间才能进行
                if(map[xn][yn] == -1 || t + visited[xn][yn] <= map[xn][yn])
                {//  map==-1 表示没走过这点  t+v<=map  要保证 再次过这点时的时间必须小于等于之前都过此点时所花费的时间
                    map[xn][yn] = t + visited[xn][yn];//更新map  使经过此点的时间为最小值
                    ch = visited[xn][yn];//记录过这点的时间
                    visited[xn][yn] = -1;//标记走过的点不能再次再走
                    dfs(xn, yn, t + ch);//  继续搜 
                    visited[xn][yn] = ch;//回溯
                }
            }
        }
    }
}

int main()
{
    int i, j;
    char str[203];
    while(scanf("%d %d\n", &n, &m) != EOF)
    {
        time = max;
        for(i = 0; i <= n + 2; ++i)
            for(j = 0; j <= m + 2; ++j)
            {
                map[i][j] = -1;
                visited[i][j] = -1;
            }
        for(i = 1; i <= n; ++i)
        {
            gets(str);
            for(j = 0; j <= m - 1; ++j)
                if(str[j] == '.')
                    visited[i][j + 1] = 1;
                else if(str[j] == 'a')
                {
                    ax = i;
                    ay = j + 1;
                }
                else if(str[j] == 'r')
                {
                    ex = i;
                    ey = j + 1;
                    visited[i][j + 1] = 1;
                }
                else if(str[j] == 'x')
                    visited[i][j + 1] = 2;
        }
        dfs(ax, ay, 0);
        if(time != max)
            printf("%d\n", time);
        else
            printf("Poor ANGEL has to stay in the prison all his life.\n" );
    }
    return 0;
}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值