标签:hdu
Description
Input
Output
Sample Input
4 4 5 S.X. ..X. ..XD .... 3 4 5 S.X. ..X. ...D 0 0 0
Sample Output
NO YES
题目大意:
一扇门只能在第T秒时打开,问小狗是否能在开门时恰好到达这扇门,逃出去。
解题思路:
DFS问题。
s | ||||
| | ||||
| | ||||
| | ||||
+ | — | — | — | e |
s | — | — | — | |
— | — | + | ||
| | + | |||
| | ||||
+ | — | — | — | e |
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define N 10 using namespace std; bool flag,ans,visited[N][N]; int n,m,t,xe,ye; char map0[N][N]; void dfs(int x,int y,int timen){ if(flag) return ; if(timen>t) return ; if(x<0||x>n-1||y<0||y>m-1) return ; if(timen==t&&map0[x][y]=='D') {flag=ans=true;return ;} int temp=t-timen-abs(xe-x)-abs(ye-y); if(temp&1) return ;//奇偶剪枝,位运算判断是否为奇数,比mod更快. if(!visited[x-1][y]&&map0[x-1][y]!='X'){ visited[x-1][y]=true; dfs(x-1,y,timen+1); visited[x-1][y]=false; } if(!visited[x+1][y]&&map0[x+1][y]!='X'){ visited[x+1][y]=true; dfs(x+1,y,timen+1); visited[x+1][y]=false; } if(!visited[x][y-1]&&map0[x][y-1]!='X'){ visited[x][y-1]=true; dfs(x,y-1,timen+1); visited[x][y-1]=false; } if(!visited[x][y+1]&&map0[x][y+1]!='X'){ visited[x][y+1]=true; dfs(x,y+1,timen+1); visited[x][y+1]=false; } } int main(){ int xs,ys; while(scanf("%d%d%d",&n,&m,&t)!=EOF&&(n||m||t)){ int cnt=0; getchar(); memset(visited,false,sizeof(visited)); flag=ans=false; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ cin>>map0[i][j]; if(map0[i][j]=='S'){ xs=i;ys=j; visited[i][j]=true; } if(map0[i][j]=='D'){ xe=i;ye=j; } if(map0[i][j]=='X'){ cnt++; } } } if(n*m-cnt-1>=t) dfs(xs,ys,0); if(ans) printf("YES\n"); else printf("NO\n"); } return 0; }
HDU1010 Tempter of the Bone(小狗是否能逃生----DFS),布布扣,bubuko.com
HDU1010 Tempter of the Bone(小狗是否能逃生----DFS)
标签:hdu
原文地址:http://blog.csdn.net/hush_lei/article/details/38557145