码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1010 DFS+剪枝

时间:2015-01-29 14:39:30      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:acm算法   amp   c   math.h   printf   

Tempter of the Bone

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


Problem Description
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.

The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.
 

Input
The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:

‘X‘: a block of wall, which the doggie cannot enter;
‘S‘: the start point of the doggie;
‘D‘: the Door; or
‘.‘: an empty block.

The input is terminated with three 0‘s. This test case is not to be processed.
 

Output
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
 

Sample Input
4 4 5 S.X. ..X. ..XD .... 3 4 5 S.X. ..X. ...D 0 0 0
 

Sample Output
NO YES
 

Author
ZHANG, Zheng
 

Source
 

Recommend

TIE了非常多次,总结一下坑点,

1 输入的循环要从1开始。

2 需要进行优化

深搜部分还是挺简单的,剪枝的地方确实难想到,这也是我TIL很多次的原因。

上代码

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int abs(int a)
{
    return a>0?a:-a;
}
int x1,y1;    
int n,m,t,ok;
char map[10][10];
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
void dfs(int x,int y,int w)
{
    int sx,sy,i,pp;
    if(x<=0 ||y<=0 ||x>n ||y>m ) 
		return;
	pp=t-w-abs(x1-x)-abs(y1-y);//剪枝关键的一步。
    if(pp<0 ||pp%2)
        return;     
	//  0 1 0 1 0 1 0 1 0
	//  1 0 1 0 1 0 1 0 1               
	//  0 1 0 1 0 1 0 1 0
	//  1 0 1 0 1 0 1 0 1
	//  0 1 0 1 0 1 0 1 0
	//  1 0 1 0 1 0 1 0 1
	//  0 1 0 1 0 1 0 1 0
	//  1 0 1 0 1 0 1 0 1 
	//  无论是从o 开始还是从1开始,
	//  都是 0--->1 或者1--->0 都是奇数步
	//  0-->0 , 1--->1 都是偶数步	
    if(x==x1 &&y==y1 &&w==t)    //满足出来的坐标,并且时间为t,令OK为一。
    {  
        ok=1;
        return;
    }
    for(i=0;i<4;i++)//深搜。
    {
        sx=x+dir[i][0];
        sy=y+dir[i][1];
        if(map[sx][sy]!='X')
        {
            map[sx][sy]='X';
            dfs(sx,sy,w+1);
            if(ok)
                return;
            map[sx][sy]='.';
        }
    }
    return;
}
int main()
{
    int i,j,x3,y3;
    while(scanf("%d%d%d",&n,&m,&t)!=EOF)
    {
        if(n==0&&n==0 &&t==0)
            break;
		getchar();
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                scanf("%c",&map[i][j]);
                if(map[i][j]=='S')
                {
                    x3=i;//进入的坐标
                    y3=j;
                }
                else if(map[i][j]=='D')
                {
                    x1=i;//出来的坐标
                    y1=j;
                }
            }
			getchar();
        }
        ok=0;
        map[x3][y3]='X';//标志已访问过
        dfs(x3,y3,0);// 深搜
        if(ok)
            printf("YES\n");//OK为一则行,反之不行
        else
            printf("NO\n");
    }
    return 0;
}


 

HDU 1010 DFS+剪枝

标签:acm算法   amp   c   math.h   printf   

原文地址:http://blog.csdn.net/sky_miange/article/details/43269885

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!