Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 55868 Accepted Submission(s): 15080
Total Submission(s): 55868 Accepted Submission(s): 15080
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.
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.
'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
题意:
题意:
一只小狗想要逃出迷宫,他的当前位置在‘S’,如果能恰好经过t秒到达‘D’门逃出迷宫,输出"YES",否则输出“NO”。(被走过的空点不能重复走;‘X’为墙,不能走,‘.’为可走的路)思路:
深度搜索+减枝优化
1.如果可走的空点比时间t要少,一定不能逃出,因为不能重复走过的路。
2.奇偶减枝,从一个点到另一个点不管怎么变化路径,所走的步数都是奇数或都是偶数,那么若所剩路径的需要经过的最短时间和所剩时间不是同奇同偶,那么一定不能逃出迷宫。
/*************************************************************************
> File Name: I.cpp
> Author: BSlin
> Mail:
> Created Time: 2013年09月25日 星期三 21时24分49秒
************************************************************************/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <iterator>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#define MP make_pair
#define INF (1<<30)
#define PI acos(-1.0)
#define esp 1e-8
const int dx[4]={0,0,0,0};
using namespace std;
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
#if defined (_WIN32) || defined (__WIN32) || defined (WIN32) || defined (__WIN32__)
#define LL __int64
#define LLS "%" "I" "6" "4" "d"
#else
#define LL long long
#define LLS "%" "l" "l" "d"
#endif
char mark[10][10];
int to[4][2] = {-1,0,0,-1,1,0,0,1};
int n,m,t,stx,sty,enx,eny;
bool flag;
bool inmap(int x, int y) {
if(x >= 1 && x <= n && y >= 1 && y <= m) return true;
return false;
}
void dfs(int x,int y, int tm) {
int i,nowx,nowy,temp; //nowx、nowy,这怎么能设成全局变量呢
if(flag == true) return; //1.立即返回
if(tm == t && x == enx && y == eny) {
flag = true;
return ;
}
if(tm > t) return;
if((abs(enx - x) + abs(eny - y)) % 2 != (t - tm) % 2) return ; //2.奇偶减枝
for(i=0; i<4; i++) {
nowx = x + to[i][0];
nowy = y + to[i][1];
if(!inmap(nowx,nowy) || mark[nowx][nowy] == 'X') continue;
mark[nowx][nowy] = 'X'; //标记访问
dfs(nowx, nowy, tm + 1);
mark[nowx][nowy] = '.'; //回溯取消标记
}
return ;
}
int main(int argc, char** argv) {
//read;
int i,j,lu;
while(scanf("%d %d %d\n",&n,&m,&t)) {
if(n == 0 && m == 0 && t == 0) break;
memset(mark,0,sizeof(mark));
lu = 0;
for(i=1; i<=n; i++) {
for(j=1; j<=m; j++) {
scanf("%c",&mark[i][j]);
if(mark[i][j] == 'S') {
stx = i;
sty = j;
mark[i][j] = 'X';
}
else if(mark[i][j] == 'D') {
enx = i;
eny = j;
lu++;
}
else if(mark[i][j] == '.') {
lu++;
}
}
getchar();
}
if(lu < t) { //3.路程减枝
printf("NO\n");
continue;
}
flag = false;
dfs(stx,sty,0);
if(flag) printf("YES\n");
else printf("NO\n");
}
return 0;
}