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. PERL 脚本

    PERL: Practical Extraction and Report Language 参考文档 1.Perl 5 version 24.0 documentation

  2. uva439 - Knight Moves(BFS求最短路)

    题意:8*8国际象棋棋盘,求马从起点到终点的最少步数. 编写时犯的错误:1.结构体内没构造.2.bfs函数里返回条件误写成起点.3.主函数里取行标时未注意书中的图. #include<iostr ...

  3. Linux一

    1,debian默认需要手动开启SSH连接# Authentication:LoginGraceTime 120PermitRootLogin without-passwordStrictModes ...

  4. SecureCRT for Linux突破30天使用限制

    当然还有一种方法,就是当你试用点i agree到时候,在~/.vandyke/Config 会生成一个文件SecureCRT_eval.lic,删除以后就可以恢复30天试用

  5. OpenJudge 2721 忽略大小写比较字符串大小

    1.Link: http://bailian.openjudge.cn/practice/2721/ 2.Content: 总时间限制: 1000ms 内存限制: 65536kB 描述 一般我们用st ...

  6. margin折叠

    什么是margin折叠:当两个或更多个垂直边距相遇时,它们将形成一个外边距.这个外边距的高度等于两个发生叠加的外边距的高度中的较大者. 注意:                           (1 ...

  7. [Linux] Linux学习笔记(5)-文件与目录管理

    1.Linux目录结构为树状结构,最顶层的目录为跟目录"/",其它目录通过挂载可以将它添加到目录树中,通过解除挂载移除它们. 2.绝对路径与相对路径 绝对路径写法:由根目录&quo ...

  8. git命令行

    cmd下运行或者 进入git bash运行 输入 exit退出切换到仓库目录后再git statusgit commit -m 注释 git pull origin1 mastergit push o ...

  9. Nested Loop,Sort Merge Join,Hash Join

    三种连接工作方式比较: Nested loops 工作方式是从一张表中读取数据,访问另一张表(通常是索引)来做匹配,nested loops适用的场合是当一个关联表比较小的时候,效率会更高. Merg ...

  10. 【多路复用】I/O多路复用

    http://www.tuicool.com/articles/RBvqUz C#下用select方法实现socket服务端