标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 101603 Accepted Submission(s): 27518
这题好像只能用dfs,因为并不是去求最短路径,到达‘D’需要刚好到达limit时间,所有要讨论多条路径,bfs可取
还有这题剪枝剪的很厉害,数据比较强,
考虑两方面,一方面是奇偶性,一方面是bfs的出口,如果已经找到了路径,如果还未找到,时间已经达到limit,都可以剪枝
其实已经做过了,但是还是卡了很久,毕竟菜,重要不要失去信心就好
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <stack> #include <queue> const int inf = (1<<31)-1; const int MAXN = 1e1; using namespace std; char G[MAXN][MAXN]; int n,m,tt,flag; int mov[4][2]={-1,0,1,0,0,1,0,-1}; int check(int x,int y){ if(x<1||y<1||x>n||y>m)return 0; if(G[x][y]==‘X‘)return 0; else return 1; } void dfs(int x,int y,int t){ if(flag)return;//继续剪枝 if(t>tt)return; if(G[x][y]==‘D‘){ if(tt==t) flag = 1; // cout<<"ok"<<endl; } int nx,ny; if(G[x][y]==‘.‘){ for(int i=0;i<4;i++){ nx = x+mov[i][0]; ny = y+mov[i][1]; if(check(nx,ny)){ G[x][y] = ‘X‘; dfs(nx,ny,t+1); G[x][y] = ‘.‘; } } } } int main() { while(scanf("%d%d%d",&n,&m,&tt),n){ flag = 0; int sx,sy,dx,dy; for(int i=1;i<=n;i++){ scanf("%s",G[i]); for(int j=m;j>=1;j--){ G[i][j] = G[i][j-1]; if(G[i][j]==‘S‘){ sx = i; sy = j; }else if(G[i][j]==‘D‘){ dx = i; dy = j; } } } int tp = fabs((sx-dx)*1.)+fabs((sy-dy)*1.); if(tp%2==tt%2&&tp<=tt){ //剪枝 G[sx][sy]=‘.‘; dfs(sx,sy,0); } G[sx][sy]=‘.‘; dfs(sx,sy,0); if(flag)cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0; }
标签:
原文地址:http://www.cnblogs.com/EdsonLin/p/5430431.html