Tempter of the Bone HDU

来源:互联网 发布:大数据需要掌握的技术 编辑:程序博客网 时间:2024/05/21 10:27

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 5S.X...X...XD....3 4 5S.X...X....D0 0 0
Sample Output
NOYES


题意很明确问你能否在时刻t恰好到达门口,过程中走过的点不能再走.

思路: 超时了很多次,百度了才知道有一种方法叫奇偶性剪枝,推荐博客:点击打开链接.

先说一下奇偶性剪枝:


图片说的很明确了,当然这道题目需要转化一下.


当然这里面还有一些细节要自己考虑一下.

上代码:

#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>#include<cmath>using namespace std;int n,m,t;int ex,ey;int s[10][10];int vis[10][10];// 标记那些点不能走 int nex[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; // 四个方向 bool dfs(int x, int y, int tmp){if(tmp > t)return false;if(tmp != t && s[x][y] == 'D')return false;if(tmp == t && s[x][y]=='D')return true;if(tmp == t && s[x][y]!='D')return false; // 剪枝 int left = t - tmp; // 剩余的时间 int maxn = abs(ex-x) + abs(ey-y); // 剩余需要走的步数 if(left < maxn) return false; // 如果剩余的时间小于至少需要走的步数,那么剪枝 if(abs(maxn-left) %2 == 1)return false; // 根据奇偶行剪枝 for(int i=0; i<4; i++)    {  int xx = x + nex[i][0];  int yy = y + nex[i][1];  if(xx>=1 &&xx<=m && yy>=1 && yy <=n && vis[xx][yy]==0 && s[xx][yy]!='X')  {    vis[xx][yy] = 1;if(dfs(xx,yy,tmp+1))return true;vis[xx][yy] = 0;// 回溯  }}return false;}int main(){while(scanf("%d %d %d", &m,&n, &t)!= EOF){   if(m+n+t==0)break;   memset(vis, 0, sizeof(vis));   int x,y;   for(int i=1; i<=m; i++)   {     for(int j=1; j<=n; j++) {    scanf(" %c", &s[i][j]);if(s[i][j]=='S'){  x=i; y=j;}if(s[i][j]=='D'){ex=i; ey=j;} }   }    vis[x][y] = 1; // 起点只能走一次,所以以后是不可能走起点了 if(dfs(x,y,0))cout << "YES" << endl;else cout << "NO" << endl;}return 0;}




水波.






原创粉丝点击