HDU 1010 Tempter of the Bone
来源:互联网 发布:unity3d开发的游戏案例 编辑:程序博客网 时间:2024/04/30 05:10
这是一道搜索+回溯的题目。。依旧很坑爹……让我感受到人生无处不坑爹。
话说收获最大的是通过这道题学到了剪枝技巧,刚开始做完了是TLE的。然后上网搜索,学了“奇偶剪枝”,
剪枝后快了几千倍的速度。。真让人汗颜。
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010
附上额外测试数据:
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;}
- 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
- 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
- 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
- android软件更新方法初探
- as 和 is的区别
- 中国传媒大学数字信号处理精品课程
- windows多线程
- mysql的JDBC连接
- HDU 1010 Tempter of the Bone
- 【Linux 驱动】第五章 并发和竞态
- 第十二章 取样问题
- 我的时间管理——我的时间我做主之《一机在手,日程无忧》
- [转]openwrt网上资料
- adb 的使用
- 【黑马程序员】HTML学习日记
- Linux 压缩认识误区
- 核酸序列分析