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

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

Source

题目大意:从入口S进到出口E,如果一直按照走左边的路(碰到墙后返回来继续),或一直选择向右走,各自需要多少步;和求最短路(简单BFS)

这道题虽说是一道题,却同时考察了DFS和BFS

当然,这道题的困难之处不在于DFS有多复杂,而在于如何一直向左走或向右走却不受位置的影响,解决办法是设置两个数组顺时针与逆时针,方法如下:

设左上右下为 0, 1, 2, 3
顺时针时,假设当前的前进方向为d, 那么从(d+2)%4,也就是相反方向开始循环,每次
(d+1)%4,遇到第一个能走的就前进。
逆时针时同理,不同的是每次(d-1+4)%4。
下面附上本人比较挫代码
#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
char str[maxn][maxn];
bool vis[maxn][maxn];
int stepl=,stepr=,stepm=;
int n,m;
int sx,sy,sd;
int dirl[][]={-,,,,,,,-};
int dirr[][]={,,,,-,,,-};
struct node{
int x,y,dis;
};
node u,v;
int step;
int dfs(int x,int y,int dir,int a[][]){
if(str[x][y]=='E')
return ;//特别注意,返回1
for(int i=;i<;i++){
int tdir=(dir+i+)%;//tdir需要重新定义
int tx=x+a[tdir][];
int ty=y+a[tdir][];
if(tx>=&&tx<n&&ty>=&&ty<m&&str[tx][ty]!='#'&&!vis[tx][ty]){
step =dfs(tx,ty,tdir,a)+;//+1
break;
}
}
return step;
} int bfs(){
memset(vis,false,sizeof(vis));
u.x=sx,u.y=sy,u.dis=;
vis[sx][sy]=true;
queue<node>q;
q.push(u);
while(!q.empty()){
u=q.front();
q.pop();
if(str[u.x][u.y]=='E')
return u.dis;
for(int i=;i<;i++){
v.x=u.x+dirl[i][];
v.y=u.y+dirl[i][];
if(v.x>=&&v.x<n&&v.y>=&&v.y<m&&
!vis[v.x][v.y]&&str[v.x][v.y]!='#'){
vis[v.x][v.y]=true;
v.dis=u.dis+;
q.push(v);
}
}
}
} int main(){
int t;
scanf("%d",&t);
while(t--){ memset(str,,sizeof(str));
memset(vis,false,sizeof(vis));
scanf("%d%d",&m,&n);
for(int i=;i<n;i++){
scanf("%s",str[i]);
for(int j=;j<m;j++){
if(str[i][j]=='S')
sx=i,sy=j;
}
}
int tx,ty; for(int i=;i<;i++){//判断左走方向
tx=sx+dirl[i][];
ty=sy+dirl[i][];
if(str[tx][ty]=='.'){
sd=i;
break;
}
}
step=;//每次向左或向右需要重新赋值为9
stepl=dfs(sx,sy,sd,dirl);//对于左走进行dfs for(int i=;i<;i++){//判断左走方向
tx=sx+dirr[i][];
ty=sy+dirr[i][];
if(str[tx][ty]=='.'){
sd=i;
break;
}
}
step=;
stepr=dfs(sx,sy,sd,dirr);//对于左走进行dfs stepm=bfs();//最短路进行bfs
printf("%d %d %d\n",stepl,stepr,stepm);
}
return ;
}

poj3083 Children of the Candy Corn BFS&&DFS的更多相关文章

  1. Children of the Candy Corn (bfs+dfs)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8120   Accepted: 3547 Description The c ...

  2. POJ:3083 Children of the Candy Corn(bfs+dfs)

    http://poj.org/problem?id=3083 Description The cornfield maze is a popular Halloween treat. Visitors ...

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

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

  4. POJ3083——Children of the Candy Corn(DFS+BFS)

    Children of the Candy Corn DescriptionThe cornfield maze is a popular Halloween treat. Visitors are ...

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

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

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

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

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

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

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

  9. poj 3083 Children of the Candy Corn(DFS+BFS)

    做了1天,总是各种错误,很无语 最后还是参考大神的方法 题目:http://poj.org/problem?id=3083 题意:从s到e找分别按照左侧优先和右侧优先的最短路径,和实际的最短路径 DF ...

随机推荐

  1. JPA将查询结果转换为DTO对象

    前言 JPA支持使用@Query自定义查询,查询的结果需要字节用DTO对象接收,如果使用HQL的查询语句,可以将直接将DTO对象的构造方法传入hql中,直接转为DTO对象:而如果使用native sq ...

  2. .net reflector 的缺陷

    .net reflector是一个反编译DLL的工具,在安装后如果电脑上有VS也会同时安装到VS里面,但是他是收费的,虽然反编译的效果很好,但是运行VS2013时(或许其他版本也有这样的问题)如果项目 ...

  3. C++各大有名科学计算库(转)

    在 C++中,库的地位是非常高的.C++之父 Bjarne Stroustrup先生多次表示了设计库来扩充功能要好过设计更多的语法的言论.现实中,C++的库门类繁多,解决的问题也是极其广泛,库从轻量级 ...

  4. 【转】IOS开发网络篇之──ASIHTTPRequest详解

    ASIHTTPRequest 详解, http 请求终结者 版权归旺财勇士所有〜转载需声名〜 原贴地地址:http://wiki.magiche.net/pages/viewpage.action?p ...

  5. maven操作手册

    ===Maven的安装=== http://blog.csdn.net/yang5726685/article/details/56486479 ===Maven的jar包仓库地址配置=== http ...

  6. 微服务SpringCloud+Docker入门到高级实战(目录)

    第一章 课程介绍和学习路线 1.微服务架构SpringCloud课程介绍 简介:课程介绍和课程大纲讲解,讲课风格和重点内容理解技巧2.技术选型和学后水平 简介:课程所需基础和技术选型讲解,学完课程可以 ...

  7. jsp页面:一个form,不同请求提交form

    需求:一个表单中有一个请求 action="url"发送数据地址: 在表单外有一个请求,请求form表单提交的数据 我们用js来写:通过每次请求传不同的action=url; 例如 ...

  8. django+xadmin在线教育平台(一)

    大家好,此教程为在慕学网的实战教程Python升级3.6 强力Django+杀手级Xadmin打造在线教育平台的学习笔记,不对望指正! 使用Django+Xadmin打造在线教育平台(Python2, ...

  9. Redis学习笔记(三)

    一.数据备份与恢复 数据备份: localhost:> save OK 该命令会在redis的安装目录中创建文件dump.rdb,并把数据保存在该文件中 查看redis的安装目录: localh ...

  10. EF上下文对象创建之线程内唯一

    在一次请求中,即一个线程内,若是用到EF数据上下文对象,就创建一个,那么会造成数据混乱,每次创建的对象执行相应的数据库操作,此同时,其他的EF对象内获得的数据可能已经是“过期”的了.即这个数据已经变动 ...