Tempter of the Bone
Total Submission(s): 55868 Accepted Submission(s): 15080
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.
'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.
題意:
一隻小狗想要逃出迷宮,他的當前位置在‘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;
}