Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 51440 Accepted Submission(s): 13834
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.
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.
4 4 5 S.X. ..X. ..XD .... 3 4 5 S.X. ..X. ...D 0 0 0
NO YES
/*奇偶性剪枝: 可以把map看成这样: 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或1->0 必然是奇数步 0->0 走1->1 必然是偶数步*/ #include <stdio.h> #include <math.h> #include <string.h> char map[10][10]; int dis[10][10]; int n, m, s; int sx, sy, ex, ey; int flag; int dx[]= {0,1,-1,0}; int dy[]= {1,0,0,-1}; void dfs(int x, int y, int k) { int i, xx , yy; if(flag) return; if(k>s) return; if(k==s) { if(x==ex && y==ey) flag = 1; return; } int t = s-k-dis[x][y]; if( t<0 | t&1 ) return; for(i=0; i<4; ++i) { xx= x + dx[i]; yy= y + dy[i]; if(xx<0 || xx >= n || yy <0 || yy >=m || map[xx][yy] =='X') continue; if(map[xx][yy] != 'X') { map[xx][yy] ='X'; dfs(xx, yy, k+1); map[xx][yy] = '.'; } } } int main() { int i, j, count; int ch; //freopen("in.txt","r",stdin); while(scanf("%d%d%d",&n,&m,&s)&&(n+m+s)) { flag = 0; count = 0; for(i=0; i<n; i++) scanf("%s",map[i]); for(i=0; i<n; ++i) for(j=0; j<m; ++j) { if(map[i][j] == 'S') { sx = i; sy = j; map[i][j]='X'; } if(map[i][j] == 'D') { ex = i; ey = j; } if(map[i][j]=='.') count++; } if(count+1< s) {printf("NO\n");continue;} for(i=0; i<n; ++i) for(j=0; j<m; ++j) dis[i][j] = abs(ex-i) + abs(ey-j); dfs(sx, sy, 0); if(flag) printf("YES\n"); else printf("NO\n"); } return 0; }
code2: (宽搜, 每个状态下都保存一个棋盘)/*在HDU上交挂了*/
#include<cstdio> #include<cstring> #include<queue> using namespace std; int n,m,s; char ma[10][10]; struct node{ int x,y,step; char ma[10][10]; }s_pos; int d_x,d_y; int flag; bool vis[10][10][10][10][50]; int dx[]={0,0,-1,1}; int dy[]={1,-1,0,0}; void bfs(){ memset(vis,false,sizeof(vis)); vis[s_pos.x][s_pos.y][s_pos.x][s_pos.y][s_pos.step]=true; queue<node > q; q.push(s_pos); while(!q.empty()){ node now = q.front(); q.pop(); if(now.x==d_x&&now.y==d_y){ if(now.step==s){ flag=1; return ; }else continue; } for(int i=0;i<4;i++){ node next=now; next.x+=dx[i]; next.y+=dy[i]; if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m){ next.step++; if(next.ma[ next.x ][next.y]=='X'||next.step>s) continue; next.ma[ now.x ][now.y]='X'; // if(next.ma[next.x][next.y]=='.'||next.ma[next.x][next.y]=='D'){ if(!vis[now.x][now.y][next.x][next.y][next.step]){ vis[now.x][now.y][next.x][next.y][next.step]=true; if(next.x==d_x&&next.y==d_y){ if(next.step==s){ flag=1; return ; }else continue; }else q.push(next); } // } } } } } int main(){ while(scanf("%d %d %d",&n,&m,&s)!=EOF,n+m+s){ for(int i=0;i<n;i++) scanf("%s",ma[i]); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(ma[i][j]=='S'){ s_pos.x=i; s_pos.y=j; s_pos.step=0; } if(ma[i][j]=='D'){ d_x=i; d_y=j; } s_pos.ma[i][j]=ma[i][j]; } } flag=0; bfs(); if(flag) printf("YES\n"); else printf("NO\n"); } return 0; }