Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) 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 題意:在一個坐標內,給定起點和終點,問能否恰好在t時刻到達終點。 以前很少寫搜索題,所以看到這個題,就按照普通的深搜寫了一下,交上去超時了。後來在網上搜了一下才知道,要剪枝才行。可是,我以前從沒寫過剪枝,不知道怎麼剪,就按照別人的思路往下想。看懂以後,我對剪枝的理解是:對於一些沒有必要繼續搜索的路徑,不再往下深搜,提前返回到上一層。花了半天時間調試代碼,終於AC了。 奇偶剪枝:根據題目,doggie必須在第t秒到達門口。也就是需要走t-1步。設doggie開始的位置為(sx,sy),目標位置為(ex,ey).如果abs(ex-x)+abs(ey-y)為偶數,則abs(ex-x)和abs(ey-y)奇偶性相同,所以需要走偶數步; 當abs(ex-x)+abs(ey-y)為奇數時,則abs(ex-x)和abs(ey-y)奇偶性不同,到達目標位置就需要走奇數步。先判斷奇偶性再搜索可以節省很多時間,不然的話容易超時。t-sum為到達目標位置還需要多少步。因為題目要求doggie必須在第t秒到達門的位置,所以(t-step)和abs(ex-x)+abs(ey-y)的奇偶性必然相同。因此temp=(t-step)-abs(ex-x)+abs(ey-y)必然為偶數。
#include<stdio.h> #include<string.h> int flag,sx,sy,ex,ey,num; int n,m,t,vis[10][10]; int dx[]={-1,0,1,0}; int dy[]={0,-1,0,1}; char map[10][10]; int abs(int p) { return p>=0?p:-p; } void dfs(int x,int y,int sum) { int i,xx,yy; if(flag==1) return; if(x==ex&&y==ey&&sum==t) { flag=1; return; } int mindis=abs(x-ex)+abs(y-ey); /*當前點到終點的最短距離*/ if(mindis>t-sum||(mindis+ t-sum )%2!=0) return; for(i=0;i<4;i++) { xx=x+dx[i]; yy=y+dy[i]; if(xx>=0&&xx<n&&yy>=0&&yy<m&&!vis[xx][yy]&&map[xx][yy]!='X') /*在map范圍內且可以繼續搜下去*/ { vis[xx][yy]=1; dfs(xx,yy,sum+1); vis[xx][yy]=0; } } } int main() { int i,j; while(~scanf("%d%d%d",&n,&m,&t)) { if(n==0&&m==0&&t==0) break; num=0; for(i=0;i<n;i++) { scanf("%s",map[i]); for(j=0;j<m;j++) { if(map[i][j]=='S') { sx=i; sy=j; /*記錄起點坐標*/ } if(map[i][j]=='D') { ex=i; ey=j; /*記錄終點坐標*/ } if(map[i][j]=='X') num++; /*記錄牆的數量*/ } } if(n*m-num-1<t) { printf("NO\n"); continue; } flag = 0; memset(vis, 0, sizeof(vis)); vis[sx][sy] = 1; dfs(sx, sy, 0); if(flag) printf("YES\n"); else printf("NO\n"); } return 0; }