hdu1010 Tempter of the Bone
来源:互联网 发布:手机文档解密软件 编辑:程序博客网 时间:2024/05/18 02:55
一道dfs题,需要剪枝。
(1)奇偶剪枝,当前位置和目标位置距离(横坐标之差加纵坐标之差)大于剩余步数,则不可能到达,进行剪枝(搜索过程中剪枝)
(2)当可通行位置数量小于t,则不可能在t时刻到达,需要剪枝
(3)如果当前位置到目标位置的距离(横坐标之差加纵坐标之差)的奇偶性和剩余步数不同,则不可能到达,进行剪枝(在搜索中进行动态剪枝)
#include<iostream>#include<cmath>using namespace std;int n,m,t;char ma[10][10];int dir[4][2]={1,0,0,1,-1,0,0,-1};int victory;int si,sj,ei,ej;void dfs(int i,int j,int d){ if(victory==1) return; if(i==ei&&j==ej&&d==t) { victory=1; return; } int tmp=(t-d)-abs(ei+ej-i-j); if(tmp<0||tmp%2) return; for(int k=0;k<4;k++) { if(i+dir[k][0]<0||i+dir[k][0]>=n) continue; if(j+dir[k][1]<0||j+dir[k][1]>=m) continue; if(ma[i+dir[k][0]][j+dir[k][1]]=='X') continue; ma[i+dir[k][0]][j+dir[k][1]]='X'; dfs(i+dir[k][0],j+dir[k][1],d+1); ma[i+dir[k][0]][j+dir[k][1]]='.'; }}int main(){ while(cin>>n>>m>>t) { int wall=0; if(n==0&&m==t&&t==0) break; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { cin>>ma[i][j]; if(ma[i][j]=='S') { si=i; sj=j; } if(ma[i][j]=='D') { ei=i; ej=j; } if(ma[i][j]=='X') { wall++; } } } victory=0; int tmp=t-abs(si+sj-ei-ej); if(n*m-wall<t||tmp%2) { cout<<"NO"<<endl; continue; } ma[si][sj]='X'; //这个一定要变成'X',在这里WA了一个小时T_T。。。 dfs(si,sj,0); if(victory==1) cout<<"YES"<<endl; else cout<<"NO"<<endl; }}
0 0
- HDU1010 Tempter of the Bone
- Tempter of the Bone hdu1010
- (hdu1010)Tempter of the Bone
- hdu1010 Tempter of the Bone
- HDU1010 Tempter of the Bone
- HDU1010 Tempter of the Bone
- HDU1010:Tempter of the Bone
- HDU1010 Tempter of the Bone
- HDU1010 Tempter of the Bone
- hdu1010(Tempter of the Bone)
- hdu1010 Tempter of the Bone
- HDU1010 Tempter of the Bone
- hdu1010 Tempter of the Bone
- hdu1010 Tempter of the Bone
- hdu1010 Tempter of the Bone
- hdu1010---Tempter of the Bone
- HDU1010---Tempter of the Bone
- hdu1010 Tempter of the Bone
- 微信js sdk invalid signature签名错误 问题解决。
- 简单的内存对齐
- 委托和接口
- 00000000000000000
- WaitForMultipleObjects使用详解
- hdu1010 Tempter of the Bone
- Effective C++ 条款44
- 编程技术面试的五大要点
- flash中声音的控制
- all of these is operation
- Android4.0代码目录结构
- 五子棋AI算法浅谈
- 慎用Outline ,UGUI Outline实现原理分析
- 380多个梦想的力量