poj 3009(深度搜索)
2012-11-03 15:09
246 查看
#include<stdio.h> #include<string.h> #include<iostream> using namespace std; #define N 22 int map ; int minstep; int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}}; int w,h; bool check(int x,int y) { if(x>=1 && x<=h && y>=1 && y<=w) return 1; return 0; } void dfs(int step ,int x, int y) { if(step>= 10) return ; for(int i=0;i<4;i++) { int xx=x+dir[i][0]; int yy=y+dir[i][1]; if(map[xx][yy]== 1) continue; while(map[xx][yy]==0) { xx=xx+dir[i][0]; yy=yy+dir[i][1]; } if(check(xx,yy)) { if(map[xx][yy]==1) { map[xx][yy]=0; dfs(step+1,xx-dir[i][0],yy-dir[i][1]); map[xx][yy]=1; } if(map[xx][yy]==3) { if(step+1<minstep) minstep=step+1; } } } } int main() { int i,j; int dx,dy; while(scanf("%d%d",&w,&h)!=EOF) { if(w==0 && h==0) break; //memset( map,0,sizeof(map) ); for(i=1;i<=h;i++) for(j=1;j<=w;j++) { cin>>map[i][j]; if(map[i][j] == 2) { dx=i; dy=j; map[i][j]=0; } } minstep=9999999; dfs(0,dx,dy); // dfs(0,dx,dy); if(minstep<=10) printf("%d\n",minstep); else printf("-1\n"); } return 0; }
相关文章推荐
- POJ3009 - Curling 2.0 - 深度优先搜索
- poj3009之深度优先搜索 dfs解法
- 深度优先搜索【POJ 3009】
- POJ-3009 深度优先搜索
- 深度优先搜索 poj 2386
- POJ-1979 深度优先搜索DFS
- poj1312 dfs 深度优先搜索
- POJ_3009——冰球,IDS迭代加深搜索
- DFS深度搜索算法实现深度探究解析-以POJ 1040为例
- poj 3009 搜索
- POJ 1979 深度优先搜索
- POJ 2286 The Rotation Game 迭代搜索深度 + A* == IDA*
- POJ1321,深度优先搜索
- poj入门水题--深度搜索(dfs)题 1011,含各种剪枝,比较经典
- 20124330102 【 搜索 -- 深度优先搜索 】 POJ 3414 Pots
- Late Counting (POJ 2386) 深度优先搜索
- POJ 2286 The Rotation Game 迭代搜索深度 + A* == IDA*
- POJ 1008 深度优先、记忆搜索
- 深度优先搜索dfs(poj 1655)
- poj 3009(Curling 2.0 暴力搜索)