标签:
狗要出门,且正好在T秒
就是DFS + 剪枝, 联系一下剪枝技巧
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; const int step[][2] = {0,1,0,-1,1,0,-1,0}; const int maxn = 10 + 7; char Map[maxn][maxn]; int m,n,t; int aimx,aimy; int bex,bey; bool DFS(int x,int y,int tt) { if(x == 0 || x == m+1 || y == 0 || y == n+1) return false; if(x == aimx && y == aimy && tt == t) return true; int Dis = ((t - tt) - abs(x-aimx) - abs(y -aimy)); if(Dis % 2 || Dis < 0) return false; for(int i = 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; } int main() { while(cin >> m >> n >> t && (n + m + t)) { for(int i = 0; i < maxn; ++i) for(int j = 0; j < maxn; ++j) Map[i][j] = ‘X‘; int cnt = 0; for(int i = 1; i <= m; ++i) { scanf("%s",Map[i] + 1); for(int j = 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"); } else printf("NO\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/aoxuets/p/4747210.html