Question address: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1010
A very gray night contributed two TLE.
Two pruning methods (Not done yet. Let's take a look at it first. It's a white font and it's a front shave ):
★★Pruning 1: If sum is available for the remaining space, and T> sum, it will never arrive.
★★Pruning 2: If s takes an odd number of steps to D and T is an even number, it will never arrive at step T (and vice versa ).
Zookeeper is healthier:
★Answers, questions, and questions I have done (I have an impression )... I have seen a lot of people, and I think about it... Directly go to DFS... Contributed the first tle...
★I still don't think about it. I think it's strange to hang up... On heuristic search, I do not believe it can still be hung up... As a result, role contributes another tle...
★Thought of the [pruning a], so add, 515 MS success...
★On the way back to bed, I suddenly thought of [pruning 2] and sighed: I can think of it at ordinary times. How can I think of it tonight! Back to bedroom plus, easy 0 MS...
★Suddenly interest in pruning, the [pruning a] removed, 437 MS success...
★Then think of you should have been how, the heuristic removed, [pruning a] and [pruning two] at the same time to add, really 78 MS on the crash ..
★Of course, if you only use [pruning 1] or [pruning 2], you must use TLE...
========================================================== ========================================================== ======================================
Non heuristic (78 MS ):
# Include <iostream> # include <cstdio> using namespace STD; char map [10] [10]; int FX [4] = {1, 0,-1 }; int FY [4] = {0, 1, 0}; int n, m; int DFS (int x, int y, int t) {for (int K = 0; k <4; k ++) {// Four Directions: int Nx = x + FX [k]; int ny = Y + FY [k]; if (nx <0 | NX> = n | ny <0 | ny> = m) continue; If (t = 1) // if the last step can reach D, 1 is returned. Otherwise, if (Map [NX] [NY] = 'D') return 1; else continue; if (Map [NX] [NY]! = '. ') Continue; // only '. 'can walk map [NX] [NY] = 'X'; If (DFS (NX, NY, t-1) return 1; Map [NX] [NY] = '. ';} return 0;} int main () {int X, Y, T, sum, ZX, ZY; while (~ Scanf ("% d", & N, & M, & T) {If (! N) break; sum = 1; for (INT I = 0; I <n; I ++) {scanf ("% s", map [I]); for (Int J = 0; j <m; j ++) if (Map [I] [J] ='s ') map [I] [J] = 'x', x = I, y = J; else if (Map [I] [J] = 'D') zx = I, ZY = J; else if (Map [I] [J] = '. ') sum ++;} // The two pruning methods are judged first and cannot be reached at t time. No if (T> sum | (ZX-x + Zy-y + T) & 1) puts ("no"); else if (DFS (X, Y, t) puts ("yes"); else puts ("no ");} return 0;}/* 6 34s ............................ X ..... D */
========================================================== ========================================================== ======================================
Heuristic Search (0 MS)
#include<iostream>#include<cstdio>#include<cstring>using namespace std;char map[10][10];int fx[4]={1,0,0,-1};int fy[4]={0,1,-1,0};int n,m,zx,zy;struct Node{int x,y;Node(){}Node(int a,int b){x=a,y=b;}}que[100];int bfs(int x,int y){int s,t,k,nx,ny;int vis[7][7];memset(vis,-1,sizeof(vis));que[s=t=0]=Node(x,y);vis[x][y]=0;while(s<=t){Node no=que[s++];for(k=0;k<4;k++){nx=no.x+fx[k];ny=no.y+fy[k];if(nx<0 || nx>=n || ny<0 || ny>=m) continue;if(map[nx][ny]=='X' ||vis[nx][ny]!=-1 ) continue;vis[nx][ny]=vis[no.x][no.y]+1;if(map[nx][ny]=='D') return vis[nx][ny];que[++t]=Node(nx,ny);}}return vis[zx][zy];}int dfs(int x,int y,int t){int h=bfs(x,y);if(h==-1 || h>t) return 0;if(h==t) return 1;for(int k=0;k<4;k++){int nx=x+fx[k];int ny=y+fy[k];if(nx<0 || nx>=n || ny<0 || ny>=m) continue;if(map[nx][ny]!='.') continue;map[nx][ny]='X';if(dfs(nx,ny,t-1)) return 1;map[nx][ny]='.';}return 0;}int main(){int x,y,t,sum=0;while(~scanf("%d%d%d",&n,&m,&t)){if(!n) break;sum=1;for(int i=0;i<n;i++){scanf("%s",map[i]);for(int j=0;j<m;j++)if(map[i][j]=='S')map[i][j]='X',x=i,y=j;else if(map[i][j]=='D')zx=i,zy=j;else if(map[i][j]=='.')sum++;}if(t>sum || (zx-x+zy-y+t)&1) puts("NO");else if(dfs(x,y,t)) puts("YES");else puts("NO");}return 0;}/*6 6 34S............................X.....D*/