• 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): 135529    Accepted Submission(s): 36393


    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



    题意:

    一个n*m的矩阵,老鼠的起点在矩阵中的'S'上,终点在矩阵中的'D',其中'X'是墙,老鼠不能通过,'.'是路但是只能通过一次,过了一次之后就不能再走这个地方了,终点D在第K秒是打开,这就要求老鼠能够在第K秒是正好到达D点,如果不能就输出NO,可以的话就输出YES.

    思路:

    用dfs解决,但是如果剪枝没弄好会超时,新学到了一个奇偶剪枝。

    奇偶剪枝:从a到b的路径大小永远是 dis=abs(ax-bx)+abs(ay-by)+偶数 。所以以此可以先一步判断


    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<queue>
    #include<math.h>
    #include<iostream>
    #include<algorithm>
    #define INF 0x3f3f3f3f
    #define N 10
    using namespace std;
    int m,n,t,flag;
    char map[N][N];
    int vis[N][N],step,to[4][2]={0,1,1,0,-1,0,0,-1};
    
    void dfs(int x,int y){
    	if(flag)	return;
    	if(map[x][y]=='D' && step==t){
    		flag=1;
    		return;
    	}
    	if(step>=t)	return;
    
    	for(int i=0;i<4;i++){
    		int fx=x+to[i][0],fy=y+to[i][1];
    		if(fx>=1 && fx<=n && fy>=1 && fy<=m && vis[fx][fy]==0 && map[fx][fy]!='X'){
    			step+=1;
    			vis[fx][fy]=1;
    			dfs(fx,fy);
    			step-=1;
    			vis[fx][fy]=0;
    		}
    	}
    	return;
    } 
    
    int main(){
    	int sx,sy,ex,ey,i,j;
    	while(~scanf("%d%d%d",&n,&m,&t) && n+m+t){
    		for(i=1;i<=n;i++){
    			scanf("%s",map[i]+1);
    			for(j=1;j<=m;j++){
    				if(map[i][j]=='S'){
    					sx=i,sy=j;
    				}
    				if(map[i][j]=='D'){
    					ex=i,ey=j;
    				}
    			}
    		}
    		int dis=abs(sx-ex)+abs(sy-ey);
    		if((dis%2)!=(t%2)){    //奇偶剪枝
    			printf("NO
    ");
    			continue;
    		}
    		memset(vis,0,sizeof(vis));
    		step=0;
    		flag=0;
    		vis[sx][sy]=1;
    		dfs(sx,sy);
    		if(flag) printf("YES
    ");
    		else printf("NO
    ");
    	}
    	return 0;
    }

  • 相关阅读:
    SQL SERVER代码生成器必备
    jquery实现ajax提交表单信息
    linux下的守护进程
    异步在单进程系统中的重要性
    php常用函数(持续更新)
    根据html容器大小和显示文字多少调节字体大小
    弹出框插件
    本地连接虚拟机上面的redis
    Flume学习——BasicTransactionSemantics
    Flume学习——BasicChannelSemantics
  • 原文地址:https://www.cnblogs.com/KirinSB/p/9409137.html
Copyright © 2020-2023  润新知