HDOJ 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): 60901    Accepted Submission(s): 16664


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:   1241  1242  1072  1312  1026 
 

实在无法理解为什么不加优化会TLE,数据看起来没多大啊……

不过奇偶优化确实很强力。


奇偶优化的话,,,我画个图举例吧:


X是墙,其他地方是空地。从A点到B点的话,最少走2步。如果我们绕过C点的话,需要走4步,DEF点同理。每绕过一个点多走两步。

如果要求A点到B点走三步的话,就没有可行解了。

奇偶优化就是这个意思(不会数学证明……捂脸逃走)


附hdoj讨论区的数据(m,n最大为8。不加奇偶优化也能过这些数据)

4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
2 2 1
S.
.D
8 8 7
.DXS...X
........
XX..XX..
.X.X.X.X
..X.....
X....X..
........
XXXX....
5 4 18
S...
....
....
....
...D
6 6 10
S.....
......
......
......
......
.....D
2 3 2
SDX
..X
2 2 3
SD
..
2 2 2
SD
XX
4 4 6
.S..
XXX.
XXX.
XXXD
5 4 8
S...
.XX.
.X..
.X.X
....
3 3 3333
.S.
...
...
2 2 1
SD
..
1 5 4
S...D
4 5 5
.S...
..X..
.XDX.
..X..
2 4 7
SD..
....
2 2 3
S.
D.
4 4 9
S..X
X.X.
..XD
....
0 0 0

输出:
NO
YES
NO
NO
NO
YES
NO
YES
NO
NO
NO
NO
YES
YES
NO
YES
YES
YES


Code:
#include <iostream>
using namespace std;

const int dx[4]={ 1, 0,-1, 0};
const int dy[4]={ 0, 1, 0,-1};

string map[7];
int n,m,t,i,j,x1,y1,x2,y2;

int abs(int x){
	return x>0?x:-x;
} 

bool dfs(int x,int y,int i){
	int temp=t-i-abs(x2-x)-abs(y2-y);
	if (temp<0 || temp%2) return false;
	for (int j=0;j<4;++j){
		x+=dx[j]; y+=dy[j];
		if (x>=0 && x<n && y>=0 && y<m){
			if (i+1==t && map[x][y]=='D') return true;
			if (map[x][y]=='.'){
				map[x][y]='X';
				if (dfs(x,y,i+1)) return true;
				map[x][y]='.';
			}
		}
		x-=dx[j]; y-=dy[j];
	} 
	return false;
}
int main()
{
	while (cin>>n>>m>>t && n){
		for (i=0;i<n;++i){
			cin>>map[i];
			for (j=0;j<m;++j)
				if (map[i][j]=='S'){
					x1=i; y1=j;
				}
				else if (map[i][j]=='D'){
					x2=i; y2=j;
				}
		}
		if (dfs(x1,y1,0)) cout<<"YES\n";
		else cout<<"NO\n";
	}
	return 0;
}


kdwycz的网站:  http://kdwycz.com/

kdwyz的刷题空间:http://blog.csdn.net/kdwycz



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值