hdu 1010 Tempter of the Bone(搜索)
2013-04-08 20:02
447 查看
这是我做的第一到深搜题,写的时候参照着杭电的教学PPT还参照着别人的代码,就这还WA了四遍才通过。一个严重发育不良的开局。
不过没关系,反正比这还发育不良的开局多了去了。
因为是刚学,而且题目不是完全独立做出来的,就不献丑了,只说一下写代码时犯的一些错误。
1、剪枝的重要性在这个题目中体现的非常彻底,刚看到题目的时候还不以为然,觉得一个7*7的迷宫运算量肯定不会大,但是事实证明,即使是我按照杭电教学课件上的要求剪枝之后,程序仍然跑出了609ms。这要是直接提交,铁定是要超时的。
2、对深搜理解的非常肤浅,刚开始用一个while去控制搜索的方向,错都不知道错哪儿了。其实我现在还不知道错哪儿了-_-
3、没有注意在对四个方向循环递进的时候,在将一组数据放入DFS之前,应该将map[x][y]改为1,但是之后应该马上把数据改回来,开始一直不理解,思考了许久之后……好挫啊我。
不过没关系,反正比这还发育不良的开局多了去了。
因为是刚学,而且题目不是完全独立做出来的,就不献丑了,只说一下写代码时犯的一些错误。
1、剪枝的重要性在这个题目中体现的非常彻底,刚看到题目的时候还不以为然,觉得一个7*7的迷宫运算量肯定不会大,但是事实证明,即使是我按照杭电教学课件上的要求剪枝之后,程序仍然跑出了609ms。这要是直接提交,铁定是要超时的。
2、对深搜理解的非常肤浅,刚开始用一个while去控制搜索的方向,错都不知道错哪儿了。其实我现在还不知道错哪儿了-_-
3、没有注意在对四个方向循环递进的时候,在将一组数据放入DFS之前,应该将map[x][y]改为1,但是之后应该马上把数据改回来,开始一直不理解,思考了许久之后……好挫啊我。
#include<stdio.h> #include<string.h> #include<stdlib.h> #define N 10 int map ; int m,n,k; int a,b,x1,y1; int x,y,flag; int dir[4][2]={1,0, -1,0, 0,1, 0,-1}; int judge(int x,int y) { if(x>=0&&x<m&&y>=0&&y<n&&map[x][y]==0) return 1; return 0; } void DFS(int x,int y,int t) { int i,temp,tx,ty; if(x==x1&&y==y1) { if(t==0) flag=1; return ; } temp=abs(x-x1)+abs(y-y1); if(t<temp||(t-temp)%2!=0) return ; if(flag==1) return ; tx=x; ty=y; temp=t; for(i=0;i<4;i++) { x=tx+dir[i][0]; y=ty+dir[i][1]; if(judge(x,y)==1) { temp=t-1; map[x][y]=1; DFS(x,y,temp); map[x][y]=0;//这里应该把值改回来 } } } int main() { int i,j,count; char c; while(scanf("%d%d%d",&m,&n,&k),m||n||k) { count=0; getchar(); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%c",&c); if(c=='X') map[i][j]=1; else { map[i][j]=0; count++; } if(c=='S') { a=i; b=j; } else if(c=='D') { x1=i; y1=j; } } getchar(); } if(count<k) { printf("NO\n"); continue; } flag=0; x=a; y=b; map[x][y]=1; DFS(x,y,k); if(flag==1) printf("YES\n"); else printf("NO\n"); } return 0; }
相关文章推荐
- hdu-1010-Tempter of the Bone(搜索 优化)
- HDU1010——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 (dfs+奇偶性剪枝) && hdu-1015 Safecracker(简单搜索)
- 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 (dfs+奇偶剪枝)
- hdu 1010 Tempter of the Bone(DFS+剪枝优化)
- HDU 1010 Tempter of the Bone heuristic 修剪
- 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)