poj 3009
2015-10-17 14:33
88 查看
dfs
我使用dfs来做的,因为涉及到Maze的形状时刻在变,撞到block后,该block就消失,用bfs还要保存Maze,不方便
上代码
算水题
我使用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 end int 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; }
相关文章推荐
- JAVA1.5新特性--注解Annotation
- Stackoverflow上人气最旺的10个Java问题
- android之音乐播放和音效播放
- Nginx配置文件nginx.conf中文详解
- Javascript的this用法
- Xcode7 beta 网络请求报错:The resource could not be loaded because the App Transport Security policy requir
- php 判断memcache key/value是否存在方法
- delphi edit编辑框使用
- 锐浪报表列排序(按指定顺序输出)
- python 自己写的计算工资的小程序
- 机器学习笔记(2)——感知机
- 浅析Easyui Datagrid的Rownumber行号显示
- 面试:创新工场,淘宝等面试题--09/2015
- <LeetCode><Easy> 28 Remove Element
- Java-Cookie源码
- Java-Cookie源码
- jQuery源码分析之blur focus focusin focusout load resize scroll unload click dblclick等方法
- Java-Cookie源码
- poj3414--Pots(bfs,记录路径)
- HTTP POST 通讯 CURL模拟POST进行URL 通讯