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. SAP成都研究院CEC团队三巨头之一:M君的文章预告

    国人总倾向于把特点或者作用类似的人或物放在一起比较并做出排名,于是就有了许多"某某某三巨头"的称谓. 最举世闻名的莫过于二战三巨头:丘吉尔,罗斯福和斯大林. 还有陪伴咱八零后童年时 ...

  2. UVA Live Archive 4490 Help Bubu(状压dp)

    难点在于状态设计,从左向右一本书一本书的考虑,每本书的决策有两种拿走或者留下, 对于拿走后的书,之后要放回,但是决策过程中不知道到往哪里放, 虽然前面的书的种类确定,可能是往后面放更优,而后面的书的类 ...

  3. 【BZOJ3106】[CQOI2013] 棋盘游戏(对抗搜索)

    点此看题面 大致题意: 在一张\(n*n\)的棋盘上有一枚黑棋子和一枚白棋子.白棋子先移动,然后是黑棋子.白棋子每次可以向上下左右四个方向中任一方向移动一步,黑棋子每次则可以向上下左右四个方向中任一方 ...

  4. 前端小记6——项目中常用的ES6方法

    现在很多功能用es5的方法也能实现功能,但es6提供的方法显得更为高效.记录下目前常用的几个方法. 1.字符包含 通过str.includes('a')来判断, 若str中包含a则结果为true,否则 ...

  5. Python 一行代码输出心形图案

    Python3 >>> print('\n'.join([''.join([('Love'[(x-y)%4]if((x*0.05)**2+(y*0.1)**2-1)**3-(x*0. ...

  6. react的Redux基础

    redux的中文文档:http://www.redux.org.cn/ redux的英文官网:https://redux.js.org/ redux相当于vuex Redux 是 JavaScript ...

  7. Dtree 添加 checkbox 复选框 可以默认选中

    一:目标 要实现用一个树形结构的展示数据,每个节点(除了根节点)前有一个checkbox,同时,点击父节点,则子节点全选或者全不选,当选中了全部子节点,父节点选中:如下图所示: 同时可以在创建的时候, ...

  8. SVG path

    在网页上画一图形,比如星星或波浪线,开始是想着图形软件画一个的,后来发现SVG这绘图程序的语言,感觉甚是可以,就发了些时间学了一下,在此做一简单分享和记录. 菜鸟上是这么介绍的(SVG 是使用 XML ...

  9. Mysql--7种Join查询

    0.sql的执行顺序 手写顺序 机读顺序 总结 ①From:对from左边的表和右边的表计算笛卡尔积,产生虚拟表c1 ②On:对c1中的数据进行on过滤,只有符合过滤条件的数据记录才会记录在虚拟表c2 ...

  10. 初学Docker

    1.基本概念Docker 包括三个基本概念镜像( Image )容器( Container )仓库( Repository )理解了这三个概念,就理解了 Docker 的整个生命周期. 2.Docke ...