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. Java流的正确关闭方式

    因为流是无论如何一定要关闭的,所以要写在finally里.如下: BufferedReader reader = null; try { reader = (BufferedReader) getRe ...

  2. CodeBlocks对C++模板的支持

    坦率的说CodeBlocks是一款不错的跨平台编译器,一般编写C/C++都是使用它,但最近在编写C++模板文件时,发现它对模板的支持并不是很好.具体表现在模板的定义与声明分开的问题上. 一般编写C/C ...

  3. [java学习笔记]java语言核心----面向对象之构造函数

    1.构造函数概念 特点: 函数名与类名相同 不用定义返回值类型 没有具体的返回值 作用:                给对象进行初始化 注意: 默认构造函数 多个构造函数是以重载出现的 一个类中如果 ...

  4. 动态linq表达式新方法,Dynamic LINQ Extension Method

    Remember those old posts on Dynamic LINQ? You are probably aware that Microsoft has made its impleme ...

  5. *HTML5 新元素

    HTML5 新元素 自1999年以后HTML 4.01 已经改变了很多,今天,在HTML 4.01中的几个已经被废弃,这些元素在HTML5中已经被删除或重新定义. 为了更好地处理今天的互联网应用,HT ...

  6. 【转】 c++拷贝构造函数(深拷贝,浅拷贝)详解

     c++拷贝构造函数(深拷贝,浅拷贝)详解 2013-11-05 20:30:29 分类: C/C++ 原文地址:http://blog.chinaunix.net/uid-28977986-id-3 ...

  7. C#实现网络传输数据加密

    1. 分组密码 分组密码是将明文消息编码表示后数字序列划分成长为n的分组,各组分别在密钥的作用下进行变换输出等长的数字序列,即密文.一次加密一个数据组,加解密所使用的是同一密钥,故其通常也称为对称加密 ...

  8. Delphi摄像头操作

    /*Title:Delphi摄像头操作 *Author:Insun *Blog:http://yxmhero1989.blog.163.com *From:www.4safer.com */ 为了笔耕 ...

  9. GridView分页排序

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridviewPage.asp ...

  10. Android:使用命令行工具adb、mksdcard等

    有一些Android的工具需要在命令行的环境中运行,只是可以选择Windows的开始->运行,键入cmd并确定,进入命令行的界面中运行.主要的命令行工具包括adb和mksdcard等.命令行的工 ...