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

HDU 1010 Tempter of the Bone(DFS+奇偶性剪枝)

时间:2014-08-12 10:27:33      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:奇偶性剪枝   dfs   

Tempter of the Bone

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


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

要点:

1、每个block只能走一次
2、要求恰好某个给定的时间到达出口

算法分析:

迷宫==搜索无疑,关键在于剪枝。

1、若时间大于剩余可行走block,则必定失败。

2、

可以把map看成这样: 
0 1 0 1 0 1 
1 0 1 0 1 0 
0 1 0 1 0 1 
1 0 1 0 1 0 
0 1 0 1 0 1 
从为 0 的格子走一步,必然走向为 1 的格子 
从为 1 的格子走一步,必然走向为 0 的格子 
即: 
 0 ->1或1->0 必然是奇数步 
 0->0 走1->1 必然是偶数步 

结论:
所以当遇到从 0 走向 0 但是要求时间是奇数的,或者, 从 1 走向 0 但是要求时间是偶数的 都可以直接判断不可达!关于这句话在本题中的使用,我是这样理解的,假设有一条最短路径p,我们若每偏离p路径一次,就需要增加2或者0才能达到目的地,因此该路径的奇偶性与时间必定等同。

深搜代码如下:

# include <iostream> 
char map[9][9]; 
int n,m,t,di,dj; 
bool escape; 
int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}}; 
void dfs(int si,int sj,int cnt) 
{    int i,temp; 
   if(si>n||sj>m||si<=0||sj<=0) return; 
   if(cnt==t&&si==di&&sj==dj)    escape=1;
   if(escape) return; 
  
   temp=(t-cnt)-abs(si-di)-abs(sj-dj); //(t-cnt)为剩余分钟,abs(si-di)+abs(sj-dj)为最少需要步数, 
      //若前者小于后者则一定失败,若二者的奇偶性不同也一定失败
   if(temp<0||temp&1) return; 
   
   for(i=0;i<4;i++){ //尝试相邻四个可能位置 
      if(map[si+dir[i][0]][sj+dir[i][1]]!='X')
	  { 
         map[si+dir[i][0]][sj+dir[i][1]]='X'; //已走过,置为x 
         dfs(si+dir[i][0],sj+dir[i][1],cnt+1); 
         map[si+dir[i][0]][sj+dir[i][1]]='.'; //向上一步回溯时把该方格仍置为空 
      } 
   } 
   return; 
} 

int main() 
{ 
	int i,j,si,sj; 
	while(std::cin>>n>>m>>t)
	{ 
		if(n==0&&m==0&&t==0) break; 
		int wall=0;
		for(i=1;i<=n;i++) 
			for(j=1;j<=m;j++)
			{ 
				std::cin>>map[i][j]; 
				if(map[i][j]=='S') { si=i; sj=j; }  //si,sj 记下入口位置
				else if(map[i][j]=='D') { di=i; dj=j; } //di,dj记下出口位置
				else if(map[i][j]=='X') wall++;  //统计墙的总数
			} 
			if(n*m-wall<=t)  //可走的block的总数小于时间
			{
				std::cout<<"NO"<<"\n";
				continue;
			}
			escape=0; 
			map[si][sj]='X';
			dfs(si,sj,0); 
			if(escape) std::cout<<"YES"<<"\n"; 
			else std::cout<<"NO"<<"\n"; 
	} 
	return 0; 
}

如有不当之处,欢迎跟帖指出!

HDU 1010 Tempter of the Bone(DFS+奇偶性剪枝),布布扣,bubuko.com

HDU 1010 Tempter of the Bone(DFS+奇偶性剪枝)

标签:奇偶性剪枝   dfs   

原文地址:http://blog.csdn.net/u014492609/article/details/38510057

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