POJ3083——Children of the Candy Corn(DFS+BFS)
Children of the Candy Corn
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
题目大意:
一个迷宫,'.','S','E'为可以到达的点,'#'为墙。 (S点保证在迷宫边缘,且只有一个方向可以走)
规定了三种行动方式(计算从S->N的路程)
1)优先走当前方向的左方,前方,右方,后方。
2)优先走当前方向的右方,前方,左方,后方。
3)S->E的最短路径。
解题思路:
用DFS求前两个行动方式的解。
使用0123来表示当前所在位置的方向。 0前 1右 2后 3左
再通过当前的方向来确定递归的优先级。
BFS求最短路径。
Code:
#include<stdio.h>
#include<iostream>
#include<string>
#include<cstring>
#include<memory.h>
#include<algorithm>
#define MAXN 41
using namespace std;
struct qu
{
int x,y;
}q[];
int N,M,end_i,end_j;
bool vis[MAXN+][MAXN+],flag[MAXN+][MAXN+];
int dis[],Lstep,Rstep;
void dfs_left(int x1,int y1,int d)
{
Lstep++;
if (x1==end_i&&y1==end_j) return ;
if (d==)
{
if (flag[x1][y1-]) dfs_left(x1,y1-,);
else if (flag[x1-][y1]) dfs_left(x1-,y1,);
else if (flag[x1][y1+]) dfs_left(x1,y1+,);
else if (flag[x1+][y1]) dfs_left(x1+,y1,);
}
if (d==)
{
if (flag[x1-][y1]) dfs_left(x1-,y1,);
else if (flag[x1][y1+]) dfs_left(x1,y1+,);
else if (flag[x1+][y1]) dfs_left(x1+,y1,);
else if (flag[x1][y1-]) dfs_left(x1,y1-,);
}
if (d==)
{
if (flag[x1][y1+]) dfs_left(x1,y1+,);
else if (flag[x1+][y1]) dfs_left(x1+,y1,);
else if (flag[x1][y1-]) dfs_left(x1,y1-,);
else if (flag[x1-][y1]) dfs_left(x1-,y1,);
}
if (d==)
{
if (flag[x1+][y1]) dfs_left(x1+,y1,);
else if (flag[x1][y1-]) dfs_left(x1,y1-,);
else if (flag[x1-][y1]) dfs_left(x1-,y1,);
else if (flag[x1][y1+]) dfs_left(x1,y1+,);
}
}
void dfs_right(int x1,int y1,int d)
{
Rstep++;
if (x1==end_i&&y1==end_j) return ;
if (d==)
{
if (flag[x1][y1+]) dfs_right(x1,y1+,);
else if (flag[x1-][y1]) dfs_right(x1-,y1,);
else if (flag[x1][y1-]) dfs_right(x1,y1-,);
else if (flag[x1+][y1]) dfs_right(x1+,y1,);
}
if (d==)
{
if (flag[x1+][y1]) dfs_right(x1+,y1,);
else if (flag[x1][y1+]) dfs_right(x1,y1+,);
else if (flag[x1-][y1]) dfs_right(x1-,y1,);
else if (flag[x1][y1-]) dfs_right(x1,y1-,);
}
if (d==)
{
if (flag[x1][y1-]) dfs_right(x1,y1-,);
else if (flag[x1+][y1]) dfs_right(x1+,y1,);
else if (flag[x1][y1+]) dfs_right(x1,y1+,);
else if (flag[x1-][y1]) dfs_right(x1-,y1,);
}
if (d==)
{
if (flag[x1-][y1]) dfs_right(x1-,y1,);
else if (flag[x1][y1-]) dfs_right(x1,y1-,);
else if (flag[x1+][y1]) dfs_right(x1+,y1,);
else if (flag[x1][y1+]) dfs_right(x1,y1+,);
}
}
int bfs(int x1,int y1)
{
int front=,rear=;
dis[front]=;
q[front].x=x1,q[front].y=y1;
while (front<rear)
{
int x=q[front].x,y=q[front].y;
if (x==end_i&&y==end_j) break;
int a[]={x+,x-,x,x},b[]={y,y,y+,y-};
for (int i=;i<=;i++)
{
if (!(a[i]<=||a[i]>=M+||b[i]<=||b[i]>=N+)&&vis[a[i]][b[i]]==){
q[rear].x=a[i],q[rear].y=b[i];
dis[rear]=dis[front]+;
vis[a[i]][b[i]]=;
rear++;
}
}
front++;
}
return dis[front]; }
int main()
{
int T,start_i,start_j;
cin>>T;
while (T--)
{
Lstep=Rstep=;
memset(vis,,sizeof(vis));
memset(dis,,sizeof(dis));
memset(q,,sizeof(q));
int d;
cin>>N>>M;
getchar();
for (int i=; i<=M; i++)
{
for (int j=; j<=N; j++)
{
char tmp;
scanf("%c",&tmp);
flag[i][j]=;
if (tmp=='#') vis[i][j]=,flag[i][j]=;
if (tmp=='E') end_i=i,end_j=j;
if (tmp=='S') start_i=i,start_j=j;
}
getchar();
}
if (start_i==) d=;
else if (start_i==M) d=;
else if (start_j==) d=;
else if (start_j==N) d=;
dfs_left(start_i,start_j,d);
dfs_right(start_i,start_j,d);
int step1=bfs(start_i,start_j);
printf("%d %d %d\n",Lstep,Rstep,step1);
}
return ;
}
POJ3083——Children of the Candy Corn(DFS+BFS)的更多相关文章
- POJ3083 Children of the Candy Corn(Bfs + Dfs)
题意:给一个w*h的迷宫,其中矩阵里面 S是起点,E是终点,“#”不可走,“.”可走,而且,S.E都只会在边界并且,不会在角落,例如(0,0),输出的话,每组数据就输出三个整数,第一个整数,指的是,以 ...
- POJ-3083 Children of the Candy Corn (BFS+DFS)
Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and mus ...
- POJ 3083 Children of the Candy Corn (DFS + BFS + 模拟)
题目链接:http://poj.org/problem?id=3083 题意: 这里有一个w * h的迷宫,给你入口和出口,让你分别求以下三种情况时,到达出口的步数(总步数包括入口和出口): 第一种: ...
- POJ 3083 Children of the Candy Corn (DFS + BFS)
POJ-3083 题意: 给一个h*w的地图. '#'表示墙: '.'表示空地: 'S'表示起点: 'E'表示终点: 1)在地图中仅有一个'S'和一个'E',他们为位于地图的边墙,不在墙角: 2)地图 ...
- poj3083 Children of the Candy Corn BFS&&DFS
Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11215 Acce ...
- POJ3083 Children of the Candy Corn(搜索)
题目链接. 题意: 先沿着左边的墙从 S 一直走,求到达 E 的步数. 再沿着右边的墙从 S 一直走,求到达 E 的步数. 最后求最短路. 分析: 最短路好办,关键是沿着墙走不太好想. 但只要弄懂如何 ...
- poj3083 Children of the Candy Corn 深搜+广搜
这道题有深搜和广搜.深搜还有要求,靠左或靠右.下面以靠左为例,可以把简单分为上北,下南,左西,右东四个方向.向东就是横坐标i不变,纵坐标j加1(i与j其实就是下标).其他方向也可以这样确定.通过上一步 ...
- POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE
POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...
- POJ 3083 Children of the Candy Corn bfs和dfs
Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8102 Acc ...
随机推荐
- 添加点标注IMarkerElement
private void AddPointElement(IPoint pPoint) { if (pPoint != null) { IElement pElement = null; IRgbCo ...
- hash桶
#include <stdio.h> #include <stdlib.h> #include "chain.c" //include the chain. ...
- TweenMax动画库学习(五)
目录 TweenMax动画库学习(一) TweenMax动画库学习(二) TweenMax动画库学习(三) Tw ...
- jquery返回顶部特效
<style> p#back-to-top{position:fixed; bottom:100px;right:10px; display: none; } p#back-to-top ...
- JavaScript加强之自定义callback示例
callback回调函数在本文以自定义的方式出现. html: <select id="select"> <option value="111& ...
- mvc 之 @Html.DropDownList
Dictionary<string, string> myDic = new Dictionary<string, string>(); myDic.Add(System.DB ...
- window2003安全设置
1. 网上邻居->右键 属性->本地连接 右键属性->Microsoft网络的文件和打印机共享去掉选中 (影响端口: 139,445) 2. 禁止ADMIN$缺省共享 ...
- DB2分区表删除和添加分区
1.数据库版本 2.具体procedure DROP PROCEDURE DB2USER.TOOLS_PARTITION_TABLE_SHOW (VARCHAR ()); )) /********** ...
- 编码错误设置错误报 "SyntaxError: Non-ASCII character '/xe6' "
无意中碰到键盘导致一段处理中文拼音的 python 代码跑起来报了个错 “SyntaxError: Non-ASCII character ‘/xe6' " 看了下是注释 # coding: ...
- vim使用总结
tar -xf vim.tar -C ~ vim /etc/vimrc vim /root/.vimrc set ts=4 设置tab有多少空格 set ai 自动对齐 set nu set mous ...