poj 3009Curling 2.0
来源:互联网 发布:验证域名所有权的方法 编辑:程序博客网 时间:2024/06/03 04:46
/*2014-5-821:34DFSpoj 3008*/#include <cstdio>#include <string.h>#define MAXN 30using namespace std;//inputint w,h; //wight hightint graph[MAXN][MAXN];int sx,sy,ex,ey;int dx[4] = {0,0,-1,1},dy[4] = {-1,1,0,0};int ans,cnt;void dfs(int x,int y,int cnt){ int nx,ny,i; if(cnt > ans) return ; for(i = 0;i < 4; i++){ nx = x+dx[i]; ny = y+dy[i]; if(nx>=h || nx<0 || ny>=w || ny<0 || graph[nx][ny]==1)//越界或立即有阻挡物剪枝 continue; while(nx <= h && nx>=0 && ny>=0 && ny<=w && graph[nx][ny]!=1){ if(nx == ex&&ny == ey){ if(cnt+1 < ans) ans = cnt+1; break; } nx += dx[i]; ny += dy[i]; } if(nx==ex && ny==ey)//若由于到终点跳出去 continue; if(nx<h && nx>=0 && ny < w && ny>=0) { graph[nx][ny]=0;//若是碰到阻挡物。阻挡物消失。 dfs(nx-dx[i],ny-dy[i],cnt+1);//继续搜索 graph[nx][ny]=1;//还原阻挡物。回溯 } }}int main(){ while(scanf("%d%d",&w,&h) && w && h){ ans = 12; for(int i = 0;i < h;i++){ for(int j = 0;j < w; j++){ scanf("%d",&graph[i][j]); if(graph[i][j] == 2){ sx = i,sy = j; } if(graph[i][j] == 3) ex=i,ey=j; } } dfs(sx,sy,0); if(ans <= 10) printf("%d\n",ans); else printf("-1\n"); } return 0;}
0 0
- Poj 3009 Curling 2.0
- POJ: 3009 Curling 2.0
- poj 3009 Curling 2.0
- poj 3009 Curling 2.0
- POJ 3009 Curling 2.0
- poj 3009 Curling 2.0
- POJ 3009 Curling 2.0
- POJ-3009-Curling 2.0
- POJ 3009 Curling 2.0
- poj 3009 Curling 2.0
- POJ-3009-Curling 2.0
- POJ 3009 Curling 2.0
- POJ 3009 Curling 2.0
- POJ 3009 Curling 2.0
- poj 3009 Curling 2.0
- poj 3009 Curling 2.0
- poj 3009 - Curling 2.0
- POJ 3009 Curling 2.0
- HTML5中x-webkit-speech语音输入功能
- poj 3669Meteor Shower
- K-SVD与On-line dictionary learning
- <Lua程序设计(第二版)>书摘-11
- Cloudfoundry的部署工具BOSH介绍
- poj 3009Curling 2.0
- 网站建设的要素
- 分享一个启动vs日志的方法
- hdu2222Keywords Search
- 大学回想曲一——Geek's Life
- Insert Interval
- Junit常见错误@Test @BeforeClass
- NYOJ 45 - 棋盘覆盖
- Java高新技术 JDK1.5之新特性