博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj3083——Children of the Candy Corn(bfs)
阅读量:2344 次
发布时间:2019-05-10

本文共 7365 字,大约阅读时间需要 24 分钟。

Description

The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, chainsaw-wielding psychopaths, hippies, and other terrors on their quest to find the exit.

One popular maze-walking strategy guarantees that the visitor will eventually find the exit. Simply choose either the right or left wall, and follow it. Of course, there’s no guarantee which strategy (left or right) will be better, and the path taken is seldom the most efficient. (It also doesn’t work on mazes with exits that are not on the edge; those types of mazes are not represented in this problem.)

As the proprieter of a cornfield that is about to be converted into a maze, you’d like to have a computer program that can determine the left and right-hand paths along with the shortest path so that you can figure out which layout has the best chance of confounding visitors.

Input

Input to this problem will begin with a line containing a single integer n indicating the number of mazes. Each maze will consist of one line with a width, w, and height, h (3 <= w, h <= 40), followed by h lines of w characters each that represent the maze layout. Walls are represented by hash marks (‘#’), empty space by periods (‘.’), the start by an ‘S’ and the exit by an ‘E’.

Exactly one ‘S’ and one ‘E’ will be present in the maze, and they will always be located along one of the maze edges and never in a corner. The maze will be fully enclosed by walls (‘#’), with the only openings being the ‘S’ and ‘E’. The ‘S’ and ‘E’ will also be separated by at least one wall (‘#’).

You may assume that the maze exit is always reachable from the start point.

Output

For each maze in the input, output on a single line the number of (not necessarily unique) squares that a person would visit (including the ‘S’ and ‘E’) for (in order) the left, right, and shortest paths, separated by a single space each. Movement from one square to another is only allowed in the horizontal or vertical direction; movement along the diagonals is not allowed.

Sample Input

2

8 8
########
#……#
#.####.#
#.####.#
#.####.#
#.####.#
#…#..#
#S#E####
9 5
#########
#.#.#.#.#
S…….E
#.#.#.#.#
#########
Sample Output

37 5 5

17 17 9

走迷宫的时候一直靠着左边或右边的墙就一定能走出去,题目就是求这两种方法下走出迷宫需要的步数和最小的步数

靠左走和靠右走需要判断方向,可以由上一步的位置推导出来,只不过有点麻烦,最短步数不用多说了,另外数组要开大点。

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f#define MAXN 2005#define Mod 10001using namespace std;struct Node{ int x,y,step; char p;};char map[500][500];int si,sj,ei,ej;int h,w;int dfs1(int x,int y){ Node start,tmp; start.x=x,start.y=y; start.step=1; queue
q; if(y==1) { start.step++; start.y++; start.p='e'; } else if(x==1) { start.step++; start.x++; start.p='s'; } else if(y==w) { start.step++; start.y--; start.p='w'; } else if(x==h) { start.step++; start.x--; start.p='n'; } q.push(start); while(!q.empty()) { tmp=q.front(); q.pop(); if(tmp.x==ei&&tmp.y==ej) return tmp.step; if(tmp.p=='e') { if(map[tmp.x-1][tmp.y]!='#') { tmp.x--; tmp.step++; tmp.p='n'; q.push(tmp); } else if(map[tmp.x][tmp.y+1]!='#') { tmp.y++; tmp.step++; tmp.p='e'; q.push(tmp); } else if(map[tmp.x+1][tmp.y]!='#') { tmp.x++; tmp.step++; tmp.p='s'; q.push(tmp); } else if(map[tmp.x][tmp.y-1]!='#') { tmp.y--; tmp.step++; tmp.p='w'; q.push(tmp); } } else if(tmp.p=='s') { if(map[tmp.x][tmp.y+1]!='#') { tmp.y++; tmp.step++; tmp.p='e'; q.push(tmp); } else if(map[tmp.x+1][tmp.y]!='#') { tmp.x++; tmp.step++; tmp.p='s'; q.push(tmp); } else if(map[tmp.x][tmp.y-1]!='#') { tmp.y--; tmp.step++; tmp.p='w'; q.push(tmp); } else if(map[tmp.x-1][tmp.y]!='#') { tmp.x--; tmp.step++; tmp.p='n'; q.push(tmp); } } else if(tmp.p=='w') { if(map[tmp.x+1][tmp.y]!='#') { tmp.x++; tmp.step++; tmp.p='s'; q.push(tmp); } else if(map[tmp.x][tmp.y-1]!='#') { tmp.y--; tmp.step++; tmp.p='w'; q.push(tmp); } else if(map[tmp.x-1][tmp.y]!='#') { tmp.x--; tmp.step++; tmp.p='n'; q.push(tmp); } else if(map[tmp.x][tmp.y+1]!='#') { tmp.y++; tmp.step++; tmp.p='e'; q.push(tmp); } } else if(tmp.p=='n') { if(map[tmp.x][tmp.y-1]!='#') { tmp.y--; tmp.step++; tmp.p='w'; q.push(tmp); } else if(map[tmp.x-1][tmp.y]!='#') { tmp.x--; tmp.step++; tmp.p='n'; q.push(tmp); } else if(map[tmp.x][tmp.y+1]!='#') { tmp.y++; tmp.step++; tmp.p='e'; q.push(tmp); } else if(map[tmp.x+1][tmp.y]!='#') { tmp.x++; tmp.step++; tmp.p='s'; q.push(tmp); } } }}int dfs2(int x,int y){ Node start,tmp; start.x=x,start.y=y; start.step=1; queue
q; if(y==1) { start.step++; start.y++; start.p='e'; } else if(x==1) { start.step++; start.x++; start.p='s'; } else if(y==w) { start.step++; start.y--; start.p='w'; } else if(x==h) { start.step++; start.x--; start.p='n'; } q.push(start); while(!q.empty()) { tmp=q.front(); q.pop(); if(tmp.x==ei&&tmp.y==ej) return tmp.step; if(tmp.p=='e') { if(map[tmp.x+1][tmp.y]!='#') { tmp.x++; tmp.step++; tmp.p='s'; q.push(tmp); } else if(map[tmp.x][tmp.y+1]!='#') { tmp.y++; tmp.step++; tmp.p='e'; q.push(tmp); } else if(map[tmp.x-1][tmp.y]!='#') { tmp.x--; tmp.step++; tmp.p='n'; q.push(tmp); } else if(map[tmp.x][tmp.y-1]!='#') { tmp.y--; tmp.step++; tmp.p='w'; q.push(tmp); } } else if(tmp.p=='s') { if(map[tmp.x][tmp.y-1]!='#') { tmp.y--; tmp.step++; tmp.p='w'; q.push(tmp); } else if(map[tmp.x+1][tmp.y]!='#') { tmp.x++; tmp.step++; tmp.p='s'; q.push(tmp); } else if(map[tmp.x][tmp.y+1]!='#') { tmp.y++; tmp.step++; tmp.p='e'; q.push(tmp); } else if(map[tmp.x-1][tmp.y]!='#') { tmp.x--; tmp.step++; tmp.p='n'; q.push(tmp); } } else if(tmp.p=='w') { if(map[tmp.x-1][tmp.y]!='#') { tmp.x--; tmp.step++; tmp.p='n'; q.push(tmp); } else if(map[tmp.x][tmp.y-1]!='#') { tmp.y--; tmp.step++; tmp.p='w'; q.push(tmp); } else if(map[tmp.x+1][tmp.y]!='#') { tmp.x++; tmp.step++; tmp.p='s'; q.push(tmp); } else if(map[tmp.x][tmp.y+1]!='#') { tmp.y++; tmp.step++; tmp.p='e'; q.push(tmp); } } else if(tmp.p=='n') { if(map[tmp.x][tmp.y+1]!='#') { tmp.y++; tmp.step++; tmp.p='e'; q.push(tmp); } else if(map[tmp.x-1][tmp.y]!='#') { tmp.x--; tmp.step++; tmp.p='n'; q.push(tmp); } else if(map[tmp.x][tmp.y-1]!='#') { tmp.y--; tmp.step++; tmp.p='w'; q.push(tmp); } else if(map[tmp.x+1][tmp.y]!='#') { tmp.x++; tmp.step++; tmp.p='s'; q.push(tmp); } } }}int vis[500][500];int dfs3(int x,int y){ vis[x][y]=1; Node start,tmp,tmp1; start.x=x,start.y=y; start.step=1; queue
q; q.push(start); while(!q.empty()) { tmp=q.front(); q.pop(); if(tmp.x==ei&&tmp.y==ej) return tmp.step; if(map[tmp.x+1][tmp.y]!='#'&&!vis[tmp.x+1][tmp.y]) { tmp1.x=tmp.x+1; tmp1.y=tmp.y; tmp1.step=tmp.step+1; vis[tmp.x+1][tmp.y]=1; q.push(tmp1); } if(map[tmp.x-1][tmp.y]!='#'&&!vis[tmp.x-1][tmp.y]) { tmp1.x=tmp.x-1; tmp1.y=tmp.y; tmp1.step=tmp.step+1; vis[tmp.x-1][tmp.y]=1; q.push(tmp1); } if(map[tmp.x][tmp.y+1]!='#'&&!vis[tmp.x][tmp.y+1]) { tmp1.x=tmp.x; tmp1.y=tmp.y+1; tmp1.step=tmp.step+1; vis[tmp.x][tmp.y+1]=1; q.push(tmp1); } if(map[tmp.x][tmp.y-1]!='#'&&!vis[tmp.x][tmp.y-1]) { tmp1.x=tmp.x; tmp1.y=tmp.y-1; tmp1.step=tmp.step+1; vis[tmp.x][tmp.y-1]=1; q.push(tmp1); } }}int main(){ int t; scanf("%d",&t); while(t--) { memset(vis,0,sizeof(vis)); scanf("%d%d",&w,&h); for(int i=1; i<=h; ++i) for(int j=1; j<=w; ++j) { cin>>map[i][j]; if(map[i][j]=='S') { si=i; sj=j; } if(map[i][j]=='E') { ei=i; ej=j; } } int ans1=dfs1(si,sj); int ans2=dfs2(si,sj); int ans3=dfs3(si,sj); cout<
<<" "<
<<" "<
<

转载地址:http://wxcvb.baihongyu.com/

你可能感兴趣的文章
PHP参数类型限制
查看>>
IOS博客项目搭建-12-刷新数据-显示最新的微博数提示
查看>>
Laravel5 Markdown 编辑器使用教程
查看>>
php文件上传与下载
查看>>
Python3学习教程
查看>>
Python3学习笔记01-第一个Python程序
查看>>
Laravel5学生成绩管理系统-01-安装-建表-填充数据
查看>>
Mac OSX下使用apt-get命令
查看>>
Mac下安装PHP的mcrypt扩展的方法(自己总结的)
查看>>
关于html_entity_decode、空格 以及乱码
查看>>
Box2d no gravity
查看>>
mario collision
查看>>
tiled 地图工具
查看>>
小游戏
查看>>
旋转关节绳子
查看>>
射箭box2d
查看>>
cocos2d iphone-wax cocowax
查看>>
angribird level editor
查看>>
love2d 苹果运行
查看>>
GridBagLayout 的注意
查看>>