HDU 1010 Tempter of the Bone
2012-03-28 15:04
232 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010
简单搜索,奇偶剪枝,如果当前点到终点的曼哈顿距离(abs(startx-endx)+abs(starty-endy) ) 于剩余步数 奇偶性不同,说明就不可能在规定步数内到达目的地。
还有一个路径的判断的小优化,如果途中可走的点小于规定步数,就一定不可以在规定步数内到达目的地。
参考代码:
简单搜索,奇偶剪枝,如果当前点到终点的曼哈顿距离(abs(startx-endx)+abs(starty-endy) ) 于剩余步数 奇偶性不同,说明就不可能在规定步数内到达目的地。
还有一个路径的判断的小优化,如果途中可走的点小于规定步数,就一定不可以在规定步数内到达目的地。
参考代码:
#include<iostream> #include<string.h> #include<cmath> #include<cstdio> using namespace std; #define M 10 char mp[M][M]; int s[M][M],flag,endx,endy; int f[4][2]={-1,0,1,0,0,-1,0,1}; int cut[8][8] = { 0,1,0,1,0,1,0,1, 1,0,1,0,1,0,1,0, 0,1,0,1,0,1,0,1, 1,0,1,0,1,0,1,0, 0,1,0,1,0,1,0,1, 1,0,1,0,1,0,1,0, 0,1,0,1,0,1,0,1, 1,0,1,0,1,0,1,0 }; void DFS(int x,int y,int t) { if(flag) return ; if(x==endx&&y==endy && s[x][y] == t) {flag = 1;return ;} if(x == endx && y== endy || s[x][y] > t) return ; if((cut[x][y] + cut[endx][endy]) % 2 != (t-s[x][y])%2) return ; int xx,yy; for(int i=0;i<4;++i) { xx = x+f[i][0]; yy = f[i][1]+y; if(mp[xx][yy] =='.') { mp[xx][yy]='X'; s[xx][yy]=s[x][y]+1; DFS(xx,yy,t); mp[xx][yy]='.'; if(flag) return ; } } } int main() { int n,m,t,beginx,beginy; while(scanf("%d%d%d",&n,&m,&t) && m+n+t) { memset(s,0,sizeof(s)); flag=0;getchar(); int sum = 0; for(int i=1;i<=n;++i) { for(int j=1;j<=m;++j) { scanf("%c",&mp[i][j]); if(mp[i][j]=='S') { beginx=i;beginy=j; mp[i][j] = '.'; } else if(mp[i][j]=='D') { endx=i;endy=j; mp[i][j] = '.'; } else if(mp[i][j] == 'X') sum++; } getchar(); } mp[beginx][beginy]='X'; if(n * m - sum > t) DFS(beginx,beginy,t); if(flag) printf("YES\n"); else printf("NO\n"); } return 0; }
相关文章推荐
- HDU 1010 Tempter of the Bone
- HDU 1010 Tempter of the Bone (ZOJ 2110) DFS+剪枝
- 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(DFS+剪枝优化)
- 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【DFS剪枝】
- HDU【1010】Tempter of the Bone
- HDU - 1010 - Tempter of the Bone(dfs)
- 【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(dfs+01剪枝)
- 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