HDU1010奇偶剪枝
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 165492 Accepted Submission(s): 43863
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
题意:让你从起点到终点,期间不能走“X”上,以及不能重复走在一个地方,必须按照规定的时间到达
思路:深搜一遍肯定超时,要进行奇偶剪枝以及排除一些不可能的情况
(奇偶剪枝:把矩阵看成如下形式:
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 必然是奇数步,从 0 走向 0 必然是偶数步。
所以当遇到从 0 走向 0 但是要求时间是奇数的或者 从 1 走向 0 但是要求时间是偶数的,都可以直接判断不可达!
比如有一地图:
S…
…
…
…
…D
要求从S点到达D点,此时,从S到D的最短距离为s = abs ( dx - sx ) + abs ( dy - sy )。
如果地图中出现了不能经过的障碍物:
此时的最短距离s’ = s + 4,为了绕开障碍,不管偏移几个点,偏移的距离都是最短距离s加上一个偶数距离。
就如同上面说的矩阵,要求你从0走到0,无论你怎么绕,永远都是最短距离(偶数步)加上某个偶数步;要求你从1走到0,永远只能是最短距离(奇数步)加上某个偶数步。
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<iterator>
#include<vector>
#include<stdlib.h>
#include<map>
#include<queue>
#include<stack>
typedef long long ll;
using namespace std;
char mp[9][9];
int n, m, t, di, dj;
int dir[4][2] = {{0,-1}, {0,1}, {1, 0}, {-1, 0}};
bool escape;
using namespace std;
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);//奇偶剪枝,
if(temp < 0||temp&1) return ;//剩下的时间与所需的最小路程差必须要是奇数,要不然肯定走不到
for(i = 0; i < 4; i++)
{
if(mp[si+dir[i][0]][sj+dir[i][1]] != 'X')
{
mp[si+dir[i][0]][sj+dir[i][1]] = 'X';
dfs(si+dir[i][0], sj+dir[i][1], cnt+1);
mp[si+dir[i][0]][sj+dir[i][1]] = '.';
}
}
return ;
}
int main()
{
int i, j, si, sj;
while(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++)
{
cin >> mp[i][j];
if(mp[i][j] == 'S') si = i, sj = j;
else if(mp[i][j] == 'D') di = i, dj = j;
else if(mp[i][j] == 'X') wall++;//记录墙的数目
}
if(n*m - wall <= t)
{
cout << "NO" << endl;//如果能走的方格数小于规定的时间则不行
continue;
}
escape = 0;
mp[si][sj] = 'X';
dfs(si, sj, 0);
if(escape) cout << "YES" <<endl;
else cout << "NO" <<endl;
}
return 0;
}