POJ 2251 简单BFS
来源:互联网 发布:电影票用什么软件 编辑:程序博客网 时间:2024/05/16 06:03
这题虽然题目大,但是其实只是一道BFS最短路径的裸题。
BFS之所以能求最短路是因为它采用广度优先搜索,每次接触到的面时间都是相等的。所以在找的目的地时一定是最短路径。
代码如下
//// main.cpp// Code//// Created by KFM on 16/6/25.// Copyright © 2016年 KFM. All rights reserved.//#include <iostream>#include <cstdio>#include <queue>using namespace std;#define maxn 45int L,R,C;char map[maxn][maxn][maxn];int dis[6][3]= {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};struct node{ int x,y,z; int time; friend bool operator <(node n1,node n2) { return n1.time>n2.time; }};node b;node e;void BFS(){ queue<node> q; q.push(b); map[b.x][b.y][b.z]='#'; while(!q.empty()){ node start = q.front(); q.pop(); if(start.x==e.x&&start.y==e.y&&start.z==e.z){ printf("Escaped in %d minute(s).\n",start.time); return ; } for(int i=0;i<6;i++){ node move; move.x=start.x+dis[i][0]; move.y=start.y+dis[i][1]; move.z=start.z+dis[i][2]; if(move.x>=0&&move.x<L&&move.y>=0&&move.y<R&&move.z>=0&&move.z<C&&map[move.x][move.y][move.z]!='#'){ move.time=start.time+1; map[move.x][move.y][move.z]='#'; q.push(move); } } } printf("Trapped!\n");}int main() { while(~scanf("%d %d %d",&L,&R,&C)){ if(!L&&!R&&!C) return 0; for(int i=0;i<L;i++) for(int j=0;j<R;j++) for(int k=0;k<C;k++){ cin>>map[i][j][k]; if(map[i][j][k]=='S'){b.x=i;b.y=j;b.z=k;} if(map[i][j][k]=='E'){e.x=i;e.y=j;e.z=k;} } BFS(); } return 0;}
0 0
- POJ 2251 BFS 简单
- poj 2251(简单BFS)
- POJ 2251 简单BFS
- poj 2251 简单搜索bfs
- POJ 2251 BFS(简单)
- POJ - 2251 Dungeon Master (简单BFS)
- poj 2251 逃出牢笼,简单的bfs
- POJ - 2251 - Dungeon Master (简单BFS)
- POJ 2251 (BFS 简单三维迷宫)
- POJ 2251 Dungeon Master(简单BFS)
- POJ 2251 Dungeon Master 简单BFS
- poj 3626 简单bfs
- poj 3126 简单bfs
- poj 3278 简单bfs
- POJ 3278 简单 BFS
- poj 3414(简单bfs)
- POJ 3669 简单BFS
- poj 3278 简单BFS
- 计算机学习速成法
- leetcode 345. Reverse Vowels of a String 解题报告
- 文章标题
- 图像局部特征(三)--FAST角点检测子
- 51nod 1417 天堂里的游戏
- POJ 2251 简单BFS
- Linux学习之CentOS(三)----CentOS 7 下的软件安装方法及策略
- 从0开始学习GitHub系列之「Git 速成」
- 学术论文英文催稿模板
- 剑指offer题解 孩子们的游戏(圆圈中最后剩下的数)
- 多思考
- 【Java】JavaEE 13 规范
- [2016/06/25] CUDA矩阵乘法 简单实现
- 周期、周期型事件