poj 2251 Dungeon Master
来源:互联网 发布:新星星知我心 编辑:程序博客网 时间:2024/06/02 12:42
简单的bfs。。
没啥好说的。。
#include<stdio.h>#include<string.h>int l,r,c;char map[35][35][35];int way[35][35][35];struct sb{ int l; int r; int c;}s,e;int move[6][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};int queue[100010];int bfs(){ int front=0; int end=0; queue[end++]=s.l; queue[end++]=s.r; queue[end++]=s.c; while(end!=front) { int oldl=queue[front++]; int oldr=queue[front++]; int oldc=queue[front++]; for(int i=0;i<6;i++) { int newl=oldl+move[i][0]; int newr=oldr+move[i][1]; int newc=oldc+move[i][2]; if(newl>=0&&newl<l&&newr>=0&&newr<r&&newc>=0&&newc<c) { if(map[newl][newr][newc]!='#'&&way[newl][newr][newc]==0) { way[newl][newr][newc]=way[oldl][oldr][oldc]+1; queue[end++]=newl; queue[end++]=newr; queue[end++]=newc; } } } } return way[e.l][e.r][e.c];}int main(){ while(scanf("%d%d%d",&l,&r,&c)!=EOF) { if(l==0&&r==0&&c==0) break; int flags=0; int flage=0; for(int i=0;i<l;i++) { for(int j=0;j<r;j++) { scanf("%s",map[i][j]); for(int k=0;k<c;k++) { if(flags==0&&map[i][j][k]=='S') { s.l=i; s.r=j; s.c=k; flags=1; } if(flage==0&&map[i][j][k]=='E') { e.l=i; e.r=j; e.c=k; flage=1; } } } } memset(way,0,sizeof(way)); int ans=bfs(); if(ans!=0) printf("Escaped in %d minute(s).\n",ans); else printf("Trapped!\n"); } return 0;}
另外用函数做bfs。。 感觉简明一点。。
#include<stdio.h>#include<string.h>#include<queue>using namespace std;int l,r,c;char map[35][35][35];int way[35][35][35];struct sb{ int l; int r; int c;}s,e;int move[6][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};queue<int>Q;int bfs(){ Q.push(s.l); Q.push(s.r); Q.push(s.c); while(!Q.empty()) { int oldl=Q.front(); Q.pop(); int oldr=Q.front(); Q.pop(); int oldc=Q.front(); Q.pop(); for(int i=0;i<6;i++) { int newl=oldl+move[i][0]; int newr=oldr+move[i][1]; int newc=oldc+move[i][2]; if(newl>=0&&newl<l&&newr>=0&&newr<r&&newc>=0&&newc<c) { if(map[newl][newr][newc]!='#'&&way[newl][newr][newc]==0) { way[newl][newr][newc]=way[oldl][oldr][oldc]+1; Q.push(newl); Q.push(newr); Q.push(newc); } } } } return way[e.l][e.r][e.c];}int main(){ while(scanf("%d%d%d",&l,&r,&c)!=EOF) { if(l==0&&r==0&&c==0) break; int flags=0; int flage=0; for(int i=0;i<l;i++) { for(int j=0;j<r;j++) { scanf("%s",map[i][j]); for(int k=0;k<c;k++) { if(flags==0&&map[i][j][k]=='S') { s.l=i; s.r=j; s.c=k; flags=1; } if(flage==0&&map[i][j][k]=='E') { e.l=i; e.r=j; e.c=k; flage=1; } } } } memset(way,0,sizeof(way)); int ans=bfs(); if(ans!=0) printf("Escaped in %d minute(s).\n",ans); else printf("Trapped!\n"); } return 0;}
结果时间反而变长了,但是内存小了。。。
最后的是结构体的代码
#include<stdio.h>#include<string.h>#include<queue>using namespace std;int l,r,c;char map[35][35][35];int way[35][35][35];struct node{ int l; int r; int c;}s,e;int move[6][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};queue<node>Q;int bfs(){ Q.push(s); while(!Q.empty()) { node old=Q.front(); Q.pop(); for(int i=0;i<6;i++) { node new1; new1.l=old.l+move[i][0]; new1.r=old.r+move[i][1]; new1.c=old.c+move[i][2]; if(new1.l>=0&&new1.l<l&&new1.r>=0&&new1.r<r&&new1.c>=0&&new1.c<c) { if(map[new1.l][new1.r][new1.c]!='#'&&way[new1.l][new1.r][new1.c]==0) { way[new1.l][new1.r][new1.c]=way[old.l][old.r][old.c]+1; Q.push(new1); } } } } return way[e.l][e.r][e.c];}int main(){ while(scanf("%d%d%d",&l,&r,&c)!=EOF) { if(l==0&&r==0&&c==0) break; int flags=0; int flage=0; for(int i=0;i<l;i++) { for(int j=0;j<r;j++) { scanf("%s",map[i][j]); for(int k=0;k<c;k++) { if(flags==0&&map[i][j][k]=='S') { s.l=i; s.r=j; s.c=k; flags=1; } if(flage==0&&map[i][j][k]=='E') { e.l=i; e.r=j; e.c=k; flage=1; } } } } memset(way,0,sizeof(way)); int ans=bfs(); if(ans!=0) printf("Escaped in %d minute(s).\n",ans); else printf("Trapped!\n"); } return 0;}
这个字数最少。。。我喜欢!
0 0
- poj 2251 Dungeon Master
- poj 2251 Dungeon Master
- poj 2251 Dungeon Master
- poj 2251 Dungeon Master
- Poj 2251 Dungeon Master
- poj 2251 Dungeon Master
- poj 2251 Dungeon Master
- POJ 2251 Dungeon Master
- POJ-2251-Dungeon Master
- POJ 2251Dungeon Master
- poj 2251 Dungeon Master
- poj 2251 Dungeon Master
- poj 2251 Dungeon Master
- POJ 2251 - Dungeon Master
- POJ 2251 Dungeon Master
- POJ 2251 Dungeon Master
- POJ-2251-Dungeon Master
- poj 2251 Dungeon Master
- ELK+Redis日志分析平台测试性能记录
- 六种常用的设计模式java实现(五)代理模式
- sql中exists,not exists的用法
- 黑马程序员——网络编程
- 第一篇blog
- poj 2251 Dungeon Master
- 自己总结的Xcode5和Xcode7的主要区别
- 内省调用JavaBean实现getXxx和setXxx的两种方式
- 钱宝订单查询 chaqb.com
- ui进阶第二天,一些琐碎的东西
- 读书笔记 -《Python 黑帽子》 ( 二 )
- java33.HTTP通信------使用Http的Post方式与网络交互通信
- 线程倒计时
- Android app引导页(背景图片切换加各个页面动画效果)