HDU 1010 Tempter of the Bone

来源:互联网 发布:unity3d开发的游戏案例 编辑:程序博客网 时间:2024/04/30 05:10

这是一道搜索+回溯的题目。。依旧很坑爹……让我感受到人生无处不坑爹。


话说收获最大的是通过这道题学到了剪枝技巧,刚开始做完了是TLE的。然后上网搜索,学了“奇偶剪枝”,

剪枝后快了几千倍的速度。。真让人汗颜。


题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010


附上额外测试数据:


2 2 3
D.
.S
正确输出是NO


注意  the doggie had to arrive at the door on exactly the T-th second  这一句。


AC代码如下:

#include <iostream>#include <cmath>using namespace std;int m,n,bx,by,ex,ey,s,wall;char map[7][7];int dx[4]={ 0,0,-1,1 }, dy[4]={ -1,1,0,0 };bool flag;bool check(int x ,int y){if(x<0 || x>m-1 || y<0 || y>n-1 ) return false;if(map[x][y]=='X') return false;return true;}void dfs( int x,int y,int dep ){if ( flag)  return ;if(x==ex && y==ey && dep==s ) flag=true;//int tx,ty;//=========这两行剪枝很关键===========int temp = (s-dep) - abs(x-ex) - abs(y-ey);if(temp<0 || temp%2==1)  return;//=========传说中的奇偶剪枝============for(int i=0;i<4;i++){tx=x+dx[i];ty=y+dy[i];if( check(tx,ty)  ){map[tx][ty]='X';dfs(tx,ty,dep+1);map[tx][ty]='.';}}}int main(){while(cin>>m>>n>>s,m || n || s){wall=0;for(int i=0;i<m;i++){for(int j=0;j<n;j++){cin>>map[i][j];if(map[i][j]=='S') {bx=i;by=j; map[i][j]='X'; }else if(map[i][j]=='D') {ex=i;ey=j; }else if(map[i][j]=='X') {wall++; }}}flag=false;if( m*n-wall>s ) dfs(bx,by,0);if(flag) cout<<"YES\n";else cout<<"NO\n";}return 0;}


原创粉丝点击