POJ2251 Dungeon Master kuangbin-搜索入门-B
来源:互联网 发布:js 直接写 和 onload 编辑:程序博客网 时间:2024/06/16 16:43
题目链接:POJ2251
题目大意:给你一个三维的空间,给你起始点和终点,求一条从起点到终点的最短的时间也就是最短的路径。
AC代码,简单BFS
/*2017年7月30日10:20:13POJ2251 一直交了好几十发都是 MLE最后发现 没有打访问标记 = =AC */#include<stdio.h>#include<queue>#include<string.h>using namespace std;const int maxn=30;char maze[maxn][maxn][maxn];bool vis[maxn][maxn][maxn];int l,r,col,sx,sy,sz,ex,ey,ez;int d[6][3]={{1,0,0},{0,1,0},{0,0,1},{0,-1,0},{0,0,-1},{-1,0,0}};struct node{ int x,y,z,step;};int bfs(){ node a,c; queue <node > q; a.x=sx; a.y=sy; a.z=sz; a.step=1; vis[sx][sy][sz]=1; q.push(a); while(!q.empty()){ a=q.front(); q.pop(); for(int i=0;i<6;i++){ // node c; c.x=a.x+d[i][0]; c.y=a.y+d[i][1]; c.z=a.z+d[i][2]; if(c.x==ex&&c.y==ey&&c.z==ez) return a.step; if(c.x>=0&&c.x<l &&c.y>=0&&c.y<r &&c.z>=0&&c.z<col&&maze[c.x][c.y][c.z]=='.'&&!vis[c.x][c.y][c.z]){ c.step=a.step+1; vis[c.x][c.y][c.z]=1; q.push(c); } } } return 0;}int main(){ int ans; while(~scanf("%d%d%d",&l,&r,&col)&&l){ memset(maze,'/0',sizeof(maze)); memset(vis,false,sizeof(vis)); for(int i=0;i<l;i++){ for(int j=0;j<r;j++ ){ scanf("%s",&maze[i][j]); for(int k=0;k<col;k++){ if(maze[i][j][k]=='S') { sx=i; sy=j; sz=k; } else if(maze[i][j][k]=='E'){ ex=i; ey=j; ez=k; } } } } ans=bfs(); if(ans) printf("Escaped in %d minute(s).\n",ans); else printf("Trapped!\n"); } return 0;}
大家记得BFS一定要打标记,不然队列中会存很多重复的点,导致MLE
阅读全文
0 0
- POJ2251 Dungeon Master kuangbin-搜索入门-B
- [kuangbin带你飞]专题一 简单搜索 B - Dungeon Master poj2251
- POJ2251 Dungeon Master(搜索BFS)
- poj2251 Dungeon Master (广度搜索)
- poj2251 Dungeon Master(B)
- POJ2251 - Dungeon Master - 广度优先搜索
- POJ2251:Dungeon Master(BFS)3D搜索
- POJ2251 Dungeon Master (简单搜索)
- B - Dungeon Master POJ2251 三维的图 进行搜索,注意三维图的读入细节
- POJ2251 Dungeon Master
- POJ2251 Dungeon Master
- poj2251 Dungeon Master (BFS)
- poj2251 - Dungeon Master
- poj2251 Dungeon Master
- POJ2251:Dungeon Master
- Dungeon Master(poj2251,bfs)
- Dungeon Master(poj2251)
- poj2251 Dungeon Master
- Wooden Sticks
- cs231n 学习过程 问题记录
- [leetcode]50. Pow(x, n)@Java解题报告
- ECMAScript 2017(ES8)特性概述
- 简单实现自己的类似UniversalImageLoader网络图片加载缓存框架
- POJ2251 Dungeon Master kuangbin-搜索入门-B
- sizeof的一些用法(给自己看的)
- 快速幂+求值
- 【CodeForces
- python爬虫上手 笔记<1>
- JS计时器
- 一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。
- C语言字符串操作函数
- 深入理解simhash原理