poj 3009
来源:互联网 发布:怎么利用淘宝客放单 编辑:程序博客网 时间:2024/06/06 09:23
dfs
我使用dfs来做的,因为涉及到Maze的形状时刻在变,撞到block后,该block就消失,用bfs还要保存Maze,不方便
上代码
算水题
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <cmath>using namespace std;#define inf 0x3f3f3f//0 vacant 1 block 2 start 3 endint dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};int Map[55][55];int cnt=0;int Min;void dfs(int x,int y,int cnt){ cnt++; if(cnt>10)return ; int tx,ty; for(int i=0;i<4;i++){ tx=x+dir[i][0]; ty=y+dir[i][1]; if(Map[tx][ty]==1)continue; while(Map[tx][ty]==0||Map[tx][ty]==2){ tx+=dir[i][0]; ty+=dir[i][1]; } if(Map[tx][ty]<0)continue; if(Map[tx][ty]==1){ Map[tx][ty]=0; dfs(tx-dir[i][0],ty-dir[i][1],cnt); Map[tx][ty]=1; } if(Map[tx][ty]==3){ if(cnt<Min)Min=cnt; continue; } }}int main(){ int n,m; while(~scanf("%d%d",&m,&n)&&n&&m){ int mx,my; Min=inf; memset(Map,-1,sizeof(Map)); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { scanf("%d",&Map[i][j]); if(Map[i][j]==2){mx=i,my=j;} } dfs(mx,my,0); if(Min==inf)cout<<-1<<endl; else printf("%d\n",Min); } return 0;}
0 0
- POJ 3009
- poj 3009
- POJ 3009
- POJ 3009
- POJ-3009
- POJ 3009
- POJ 3009
- poj-3009
- poj--3009
- poj 3009
- POJ 3009
- poj-3009
- poj 3009
- POJ 3009
- poj 3009
- 【POJ】3009
- POJ 3009
- poj 3009
- 浅析Easyui Datagrid的Rownumber行号显示
- Java方法调用时传递参数问题
- jQuery源码分析之blur focus focusin focusout load resize scroll unload click dblclick等方法
- python 调用java 简单例子
- Java类的初始化
- poj 3009
- python 自己写的计算工资的小程序
- 锐浪报表列排序(按指定顺序输出)
- Android SlidingTabLayout底部选项卡槽Tab水平居中
- Javascript的this用法
- RadioButton的坑
- php 判断memcache key/value是否存在方法
- Xcode7 beta 网络请求报错:The resource could not be loaded because the App Transport Security policy requir
- java中的io(一)