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)的更多相关文章

  1. POJ3083 Children of the Candy Corn(Bfs + Dfs)

    题意:给一个w*h的迷宫,其中矩阵里面 S是起点,E是终点,“#”不可走,“.”可走,而且,S.E都只会在边界并且,不会在角落,例如(0,0),输出的话,每组数据就输出三个整数,第一个整数,指的是,以 ...

  2. 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 ...

  3. POJ 3083 Children of the Candy Corn (DFS + BFS + 模拟)

    题目链接:http://poj.org/problem?id=3083 题意: 这里有一个w * h的迷宫,给你入口和出口,让你分别求以下三种情况时,到达出口的步数(总步数包括入口和出口): 第一种: ...

  4. POJ 3083 Children of the Candy Corn (DFS + BFS)

    POJ-3083 题意: 给一个h*w的地图. '#'表示墙: '.'表示空地: 'S'表示起点: 'E'表示终点: 1)在地图中仅有一个'S'和一个'E',他们为位于地图的边墙,不在墙角: 2)地图 ...

  5. poj3083 Children of the Candy Corn BFS&&DFS

    Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11215   Acce ...

  6. POJ3083 Children of the Candy Corn(搜索)

    题目链接. 题意: 先沿着左边的墙从 S 一直走,求到达 E 的步数. 再沿着右边的墙从 S 一直走,求到达 E 的步数. 最后求最短路. 分析: 最短路好办,关键是沿着墙走不太好想. 但只要弄懂如何 ...

  7. poj3083 Children of the Candy Corn 深搜+广搜

    这道题有深搜和广搜.深搜还有要求,靠左或靠右.下面以靠左为例,可以把简单分为上北,下南,左西,右东四个方向.向东就是横坐标i不变,纵坐标j加1(i与j其实就是下标).其他方向也可以这样确定.通过上一步 ...

  8. POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE

    POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...

  9. POJ 3083 Children of the Candy Corn bfs和dfs

      Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8102   Acc ...

随机推荐

  1. ZigBee2006,2007,pro各个版本的区别

    文章转载自http://home.eeworld.com.cn/my/space-uid-361439-blogid-224722.html

  2. lex&yacc4

    yacc: we cannt use the $$ value dirictly. we need get it irrotly;

  3. 一个CFile::Remove引起的奇怪问题

    今天收到测试的一个反馈,我们的一个程序,在WIN7.WIN8下安装后,运行不起来,在进程列表中可以看到,但就是不出来窗口,同样的程序在XP下正常,在UAC关闭的情况下也正常,在以管理员权限运行时也正常 ...

  4. centos 7 lNMP 安装之php 篇

    1.准备工作 安装依赖包 yum install -y gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype ...

  5. 2W/月和1W/月的工作,你会怎么选?

    只看标题的话,肯定有不少人会选择月薪 2W 的工作,很明显,钱多嘛!但实际上,这里是有前提的,完整的问题如下: 一份月薪 2W,但加班无底线,基本没有自由时间的工作,和一份月薪 1W,但正常工作时长, ...

  6. 如何将无线路由器作为交换机,将光猫(路由器A)分出来的一条网线接到自家另一台路由器B上,最大化利用网络资源

    从隔壁邻居只接了一条网线过来,由于无线网络的距离有限,不能覆盖到家里任何角落,然而,我又想家里一台台式电脑和无线设备都能够连接wifi进行上网。 摸索了一个上午,知道将家里的无线路由器B当作一个无线A ...

  7. 详解Google-ProtoBuf中结构化数据的编码

    本文的主要内容是google protobuf中序列化数据时用到的编码规则,但是,介绍具体的编码规则之前,我觉得有必要先简单介绍一下google protobuf.因此,本文首先会介绍一些google ...

  8. redis参考

    www.redis.cn www.redis.io http://blog.nosqlfan.com/ 可以移步http://try.redis.io/进行实验命令 Redis 设计与实现(第一版) ...

  9. 【BZOJ 3172】 [Tjoi2013]单词

    Description 某人读论文,一篇论文是由许多单词组成.但他发现一个单词会在论文中出现很多次,现在想知道每个单词分别在论文中出现多少次. Input 第一个一个整数N,表示有多少个单词,接下来N ...

  10. ios App 加急审核

    下面进入正题.提交完成后进入加急审核页面. 链接:https://developer.apple.com/appstore/contact/appreviewteam/index.html 在i wo ...