zoukankan      html  css  js  c++  java
  • hdu1010 Tempter of the Bone(DFS+剪枝)


    Tempter of the Bone

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


    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
    JGShining   |   We have carefully selected several similar problems for you:  1072 1312 1026 1240 1175 
     

    Statistic | Submit | Discuss | Note

    这道题不是求最短时间,而是刚好在那个时间到达。

    也要利用好剪枝,否则会超时。

    剪枝1:假设起点和终点的最短时间还比t大 一定不能到达。第二个是依据坐标奇偶性,坐标奇偶性就是(x+y)/2。

    假设S坐标和D坐标的奇偶性

    同样那么就须要偶数步到达。假设不一样就须要奇数步。


    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    char map[10][10];
    int dir[4][2]={1,0,-1,0,0,1,0,-1};
    int vis[10][10],flag,n,m,t;
    void DFS(int x,int y,int ant)
    {
    	if(ant==t&&map[x][y]=='D')
    	{
    		flag=1;
    		return ;
    	}
    	if(ant>=t)//假设步数大于t,返回上一步
    	return ;
    	for(int i=0;i<4;i++)
    	{
    		int x1=x+dir[i][0];
    		int y1=y+dir[i][1];
    		if(!vis[x1][y1]&&x1>=0&&x1<n&&y1>=0&&y1<m&&map[x1][y1]!='X')
    		{
    			vis[x1][y1]=1;
    			ant++;
    			DFS(x1,y1,ant);
    			ant--;
    			vis[x1][y1]=0;
    			if(flag)//已经查找到了 不用继续查找
    			return ;
    		}
    	}
    }
    int main()
    {
    	while(scanf("%d %d %d",&n,&m,&t)!=EOF)
    	{
    		if(!n&&!m&&!t)
    		break;
    		flag=0;
    		memset(vis,0,sizeof(vis));
    		for(int i=0;i<n;i++)
    		scanf("%s",map[i]);
    		int st1,st2,ed1,ed2;
    		for(int i=0;i<n;i++)
    		for(int j=0;j<m;j++)
    		{
    			if(map[i][j]=='S')
    			st1=i,st2=j;
    			if(map[i][j]=='D')
    			ed1=i,ed2=j;
    		}
    		if(fabs(st1-ed1)+fabs(st2-ed2)>t||(st1+st2+ed1+ed2+t)%2==1)//剪枝1
    		{
    			printf("NO
    ");
    			continue;
    		}
    		vis[st1][st2]=1;
    		DFS(st1,st2,0);
    		if(flag)
    		printf("YES
    ");
    		else
    		printf("NO
    ");
    	}
    	return 0;
    }


  • 相关阅读:
    SilverLight之路(四)
    SilverLight之路(十一)
    SilverLight之路(五)
    SilverLight之路(九)
    SilverLight之路(八)
    设计silverlight的MediaPlay控件silverlight(银光)学习(3)
    自学MVC(三):郁闷的表单验证2009年05月04日
    silverlight的Button控件补完设计silverlight(银光)学习(2)
    拖动MediaPlay控件的滚动条来控制媒体播放silverlight(银光)学习(4)
    自学MVC(四):注册页面的完成2009年05月06日
  • 原文地址:https://www.cnblogs.com/mfmdaoyou/p/6991518.html
Copyright © 2011-2022 走看看