poj3009 Curling 2.0---bfs
来源:互联网 发布:叮当猫网络什么意思 编辑:程序博客网 时间:2024/05/14 11:33
#include <iostream>#include <cstring>#include <string>#include <cstdio>#include <cmath>#include <algorithm>#include <vector>#include <queue>#include <map>#define inf 0x3f3f3f3fusing namespace std;int ans,a[25][25],sr,sc,er,ec,n,m,step;int dx[]={1,-1,0,0};int dy[]={0,0,1,-1};void dfs(int r,int c){ int i,x,y; if(step>=10) return ; for(i=0;i<4;i++) { x=r;y=c; while(1) { x+=dx[i]; y+=dy[i]; if(x<0||x>=m||y<0||y>=n) break; if(x==er&&y==ec) { step++; if(step<ans) ans=step; step--; return ; } else if(a[x][y]==1) { if(x-dx[i]!=r||y-dy[i]!=c)//如果不是起点 { a[x][y]=0; step++; dfs(x-dx[i],y-dy[i]); a[x][y]=1; step--; } break; } } }}int main(){ int i,j; while(scanf("%d%d",&n,&m)&&(n||m)) { for(i=0;i<m;i++) for(j=0;j<n;j++) { scanf("%d",&a[i][j]); if(a[i][j]==2) { sr=i; sc=j; } else if(a[i][j]==3) { er=i; ec=j; } } ans=inf; step=0; dfs(sr,sc); printf("%d\n",ans==inf?-1:ans); } return 0;}
0 0
- poj3009 Curling 2.0---bfs
- POJ3009:Curling 2.0(DFS)
- POJ3009--Curling 2.0
- poj3009 Curling 2.0
- poj3009 Curling 2.0
- poj3009 Curling 2.0
- POJ3009 Curling 2.0(DFS)
- POJ3009 Curling 2.0
- POJ3009---Curling 2.0
- POJ3009 Curling 2.0
- POJ3009 Curling 2.0 DFS
- POJ3009 Curling 2.0【DFS】
- poj3009 Curling 2.0
- POJ3009 Curling 2.0(DFS)
- POJ3009-Curling 2.0
- POJ3009 Curling 2.0
- POJ3009-Curling 2.0
- poj3009 Curling 2.0
- 在VC中使用WebBrowser控件的两方法
- EIAC.SSO接入接口文档
- 【Java】Hashmap不能用基本的数据类型 Dimensions expected after this token
- 《Apache MINA 2.0 用户指南》第二章:基础知识
- Skype开始支持微软账号与Skype账号的解绑定
- poj3009 Curling 2.0---bfs
- chrome插件详解
- Jquery 的扩展方法总结
- 清空数据库错误:因为该表正由 FOREIGN KEY 约束引用 解决办法
- 关于并行查询调优涉及的相关知识[收集中]
- C#对DataGridView操作
- eclipse Cannot run program "ndk-build": Launching failed
- android 制作9.png图片
- 无法在Windows资源管理器中查看此FTP站点 (Windows XP)