Rescue (bfs)
来源:互联网 发布:家庭智能音乐系统 知乎 编辑:程序博客网 时间:2024/05/16 04:52
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649
第一次做bfs,感觉还是和dfs有点不一样的,基本都是找书上敲的。。。
#include <cstdio>#include <cstring>#include <cstdlib>#include <string>#include <map>#include <cmath>#include <queue>#include <stack>#include <iostream>#include <stdexcept>#include <cstddef>#include <algorithm>#include <vector>#include <numeric>#include <cctype>#define LL long long#define Endl endl#define INF 0x7fffffff//#define WJusing namespace std;const int maxn=205;char Map[maxn][maxn];int mintime[maxn][maxn],ex,ey,n,m;int dir[4][2]={1,0,-1,0,0,1,0,-1};struct point{int x,y;int time;int step;};queue<point> q;int bfs(point s){q.push(s);point hd;while(!q.empty()){hd=q.front(); q.pop();for(int i=0;i<4;i++){int x=hd.x+dir[i][0],y=hd.y+dir[i][1];if(x>=0&&x<n&&y>=0&&y<m&&Map[x][y]!='#'){point t;t.x=x; t.y=y;t.time=hd.time+1; t.step=hd.step+1;if(Map[x][y]=='x') t.time++;if(t.time<mintime[x][y]){mintime[x][y]=t.time;q.push(t);}}}}return mintime[ex][ey];}int main(int argc, char *argv[]){#ifdef WJ//freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);#endifwhile(~scanf("%d%d",&n,&m)){for(int i=0;i<n;i++)scanf("%s",Map[i]);int bx,by;for(int i=0;i<n;i++){for(int j=0;j<m;j++){mintime[i][j]=INF;if(Map[i][j]=='a'){ex=i; ey=j;}else if(Map[i][j]=='r'){bx=i; by=j;}}}point start;start.x=bx; start.y=by;start.time=0; start.step=0;mintime[bx][by]=0;int mint=bfs(start);if(mint<INF) printf("%d\n",mint);else printf("Poor ANGEL has to stay in the prison all his life.\n");}return 0;}
0 0
- Rescue+BFS
- Rescue (bfs)
- Rescue(BFS)
- ZOJ 1649 Rescue (BFS)
- zoj 1649 rescue (bfs)
- hdoj 1242 Rescue(bfs)
- Rescue(BFS)
- hdu 1242 Rescue (bfs)
- HDU1242 Rescue 【BFS】
- HDU 1242 Rescue BFS
- HDU--1242:Rescue (BFS)
- Rescue(逆向BFS)
- ZOJ 1649 : Rescue - BFS
- Hdoj 1424 Rescue 【BFS】
- hdoj 1242 Rescue (BFS)
- zoj 1649 Rescue bfs
- HDU 1242:Rescue 【bfs】
- hdu1242 rescue【BFS】
- GridView中的CheckBox单击事件(oncheckedchanged)
- 导出数据到Excel 2003 单个Sheet页(NPOI)
- xcode6中如何对scrollview进行自动布局(autolayout)
- Android Activity管理类优化内存利器非常方便
- 将程序中文件保存至沙盒中
- Rescue (bfs)
- ORACLE 11g 自动内存管理
- 过度拟合
- [Android] AsyncTask使用实例---加载网络图片
- cc3200学习系列--开发环境的搭建
- there is no statement named “ ” in this sqlmap报错原因。
- 设备驱动1
- Spring quartz定时器动态多任务实现
- 黑马程序员 高新技术——JAVA1.5新特性及反射