HDU 1010 Tempter of the Bone
2012-10-29 22:19
447 查看
dfs+剪枝,若当前坐标到终点的最短距离的奇偶性与剩余可走步数的奇偶性不一样时(自己画一下图就明白了,不同的到终点的路径的步数是以偶数步增长的),dfs结束,另当最短距离大于剩余可走时间时当前dfs也结束。
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<vector> #include<queue> #include<cmath> using namespace std; int n,m,k,dx[]={-1,1,0,0},dy[]={0,0,-1,1},ex,ey; bool ok,vis[10][10]; char map[10][10]; void dfs(int x,int y,int c){ if(ok) return; if(map[x][y]=='D'&&c==k) ok=true; int kk=abs(ex-x)+abs(ey-y); if((kk&1)!=((k-c)&1)) return; if(kk>k-c) return; for(int i=0;i<4;i++){ int nx=x+dx[i],ny=y+dy[i]; if(nx<0||nx>=n||ny<0||ny>=m||map[nx][ny]=='X'||vis[nx][ny]) continue; vis[nx][ny]=true; dfs(nx,ny,c+1); vis[nx][ny]=false; } } int main(){ #ifndef ONLINE_JUDGE freopen("a.in","r",stdin); freopen("a.out","w",stdout); #endif while(cin>>n>>m>>k,n||m||k){ int sx,sy; ok=false; memset(vis,false,sizeof(vis)); for(int i=0;i<n;i++){ cin>>map[i]; for(int j=0;j<m;j++) if(map[i][j]=='S') sx=i,sy=j; else if(map[i][j]=='D') ex=i,ey=j; } vis[sx][sy]=true; dfs(sx,sy,0); if(ok) puts("YES"); else puts("NO"); } return 0; }
相关文章推荐
- HDU 1010 Tempter of the Bone 深搜+奇偶剪枝
- hdu 1010 Tempter of the Bone
- HDU 1010 Tempter of the Bone(bfs)
- hdu 1010 Tempter of the Bone(dfs+奇偶剪枝)
- HDU-1010-Tempter of the Bone
- HDU-OJ-1010 Tempter of the Bone
- hdu 1010 Tempter of the Bone
- HDU 1010 Tempter of the Bone(搜索经典题)
- hdu-1010-Tempter of the Bone(搜索 优化)
- HDU 1010 Tempter of the Bone
- HDU 1010 Tempter of the Bone(dfs+剪枝)
- hdu 1010 Tempter of the Bone(搜索)
- HDU 1010 Tempter of the Bone
- hdu - 1010 - Tempter of the Bone
- 【剪枝】HDU 1010——tempter of the bone
- 【HDU 1010】Tempter of the Bone(DFS,奇偶剪枝)
- HDU-1010-Tempter of the Bone
- HDU 1010 Tempter of the Bone
- HDU 1010 Tempter of the Bone (深度搜索+减枝优化)
- HDU 1010 Tempter of the Bone【经典DPS】