• HDU 1010: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): 146511    Accepted Submission(s): 39059

    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的矩阵和一个时间T,问能不能在时间恰好为T的时候从S走到D。X不能走'.'可以走,每次走过之后'.'就会消失

    AC代码

    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <math.h>
    #include <limits.h>
    #include <map>
    #include <stack>
    #include <queue>
    #include <vector>
    #include <set>
    #include <string>
    #define ll long long
    #define ms(a) memset(a,0,sizeof(a))
    #define pi acos(-1.0)
    #define INF 0x3f3f3f3f
    const double E=exp(1);
    const int maxn=1e2+10;
    using namespace std;
    char ch[maxn][maxn];
    int vis[maxn][maxn];
    int ans;
    int xx,yy;
    int T,n,m;
    int flag;
    int dir[4][2]={1,0,-1,0,0,1,0,-1};
    void dfs(int x,int y,int t)
    {
    	vis[x][y]=1;
    	//如果正好在T时刻走到D
    	if(t==T&&ch[x][y]=='D')
    	{
    		flag++;
    		return ;
    	}
    	//奇偶剪枝,如果相差的时间和相差的曼哈顿距离的奇偶性不同,则一定无法到达
    	//或者相差的时间小于曼达顿距离也不行
    	//PS:不剪枝会超时
    	int res=T-t-abs(xx-x)-abs(yy-y);
    	if(res<0||res%2)
    		return ;
    	for(int i=0;i<4;i++)
    	{
    		int dx=x+dir[i][0];
    		int dy=y+dir[i][1];
    		if(dx>=0&&dx<n&&dy>=0&&dy<m&&ch[dx][dy]!='X'&&vis[dx][dy]==0)
    		{
    			dfs(dx,dy,t+1);
    			//如果能够走到D,就可以结束了,不需要回溯
    			if(flag)
    				return ;
    			// 回溯
    			vis[dx][dy]=0;
    		}
    	}
    }
    int main(int argc, char const *argv[])
    {
    	ios::sync_with_stdio(false);
    	while(cin>>n>>m>>T&&T&&n&&m)
    	{
    		ms(vis);
    		flag=0;
    		ans=0;
    		int x,y;
    		for(int i=0;i<n;i++)
    			cin>>ch[i];
    		// 记录开始和结束的位置
    		for(int i=0;i<n;i++)
    			for(int j=0;j<m;j++)
    			{
    				if(ch[i][j]=='S')
    				{x=i;y=j;}
    				if(ch[i][j]=='D')
    				{xx=i;yy=j;}
    			}
    				
    		dfs(x,y,0);
    		if(flag)
    			cout<<"YES"<<endl;
    		else
    			cout<<"NO"<<endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    ASP.NET 2.0 中改进的缓存功能
    Python 一门神奇的语言
    showModalDialog()、showModelessDialog()方法使用详解
    在VS2005中 GridView导入Excel的两点小技巧附源码
    DVB码流中业务信息与电子节目指南
    js 日历控件
    js收藏
    什么是ECM,EMM,AU,EMU?
    精解PSISI(一)
    Oracle第四课(学习笔记)
  • 原文地址:https://www.cnblogs.com/Friends-A/p/10324427.html
Copyright © 2020-2023  润新知