The dog is going out and just in t-seconds.
is DFS + pruning, contact pruning tips
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespaceStd;typedefLong Longll;Const intstep[][2] = {0,1,0,-1,1,0,-1,0};Const intMAXN =Ten+7;CharMAP[MAXN][MAXN];intm,n,t;intAimx,aimy;intBex,bey;BOOLDFS (intXintYintTT) { if(x = =0|| x = = m+1|| y = =0|| y = = n+1)return false; if(x = = Aimx && y = = Aimy && tt = = t)return true; intDis = ((T-TT)-ABS (X-AIMX)-ABS (Y-aimy)); if(Dis%2|| Dis <0)return false; for(inti =0; I <4; ++i) {if(map[x+step[i][0]][y+step[i][1]] !='X') {map[x+step[i][0]][y+step[i][1]] ='X'; //cout << "x:" << x << "y:" << y << Endl; if(DFS (x+step[i][0], y+step[i][1], tt+1))return true; Map[x+step[i][0]][y+step[i][1]] ='.'; } } return false;}intMain () { while(Cin >> m >> n >> t && (n + M +t)) { for(inti =0; i < MAXN; ++i) for(intj =0; J < Maxn; ++J) Map[i][j] ='X'; intCNT =0; for(inti =1; I <= m; ++i) {scanf ("%s", Map[i] +1); for(intj =1; J <= N; ++J)if(Map[i][j] = ='S') Bex = i, Bey =J; Else if(Map[i][j] = ='D') Aimx = i, Aimy =J; Else if(Map[i][j] = ='X') cnt++; } if(M*n-cnt <t) {printf ("no\n"); Continue; } Map[bex][bey]='X'; if(DFS (Bex,bey,0) {printf ("yes\n"); } Elseprintf"no\n"); } return 0;}
ZOJ 2110 DFS