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. POJ-2195 Going Home---KM算法求最小权值匹配(存负边)

    题目链接: https://vjudge.net/problem/POJ-2195 题目大意: 给定一个N*M的地图,地图上有若干个man和house,且man与house的数量一致.man每移动一格 ...

  2. 【BZOJ3209】花神的数论题(数位DP)

    点此看题面 大致题意: 设\(sum(i)\)表示\(i\)二进制中1的个数,请求出\(\prod_{i=1}^n sum(i)\). 数位\(DP\) 很显然,这是一道数位\(DP\)题.我们可以先 ...

  3. C语言exp()函数:e的次幂函数(以e为底的x次方值)

    头文件:#include <math.h> exp()用来计算以e 为底的x 次方值,即ex 值,然后将结果返回.其原型为:    double exp(double x); [返回值]返 ...

  4. 使用pip 提示UnicodeDecodeError: 'ascii' codec can't decode解决方法

    python目录 Python27\Lib\site-packages 建一个文件sitecustomize.py 内容写: import sys sys.setdefaultencoding('gb ...

  5. js中随机数获取

    // 结果为0-1间的一个随机数(包括0,不包括1) var randomNum1 = Math.random(); //console.log(randomNum1); // 函数结果为入参的整数部 ...

  6. cf550C. Divisibility by Eight(结论)

    题意 给出长度为$n$的字符串,判断是否能删除一些数后被$8$整除 Sol 神仙题啊Orz 结论: 若数字的后三位能被$8$整除,则该数字能被$8$整除 证明 设$x = 10000 * a_i + ...

  7. 2018.11.5 Nescafe26 T1 小猫爬山

    题目 题目描述 Freda 和 rainbow 饲养了 N 只小猫,这天,小猫们要去爬山.经历了千辛万苦,小猫们 终于爬上了山顶,但是疲倦的它们再也不想徒步走下山了(呜咕>_<). Fre ...

  8. 6- vue django restful framework 打造生鲜超市 -完成商品列表页(下)

    Vue+Django REST framework实战 搭建一个前后端分离的生鲜超市网站 Django rtf 完成 商品列表页下 drf中的request和response drf对于django的 ...

  9. python 实现简单语音聊天机器人

    '''思路:使用百度的文本转音频API,将结果保存成mp3格式,并用mp3play库播放该文件.''' 1 # -*- coding:utf-8 -*- import sys import reque ...

  10. 判断浏览器环境(QQ,微信,安卓设备,IOS设备,PC微信环境,移动设备)

    判断浏览器环境(QQ,微信,安卓设备,IOS设备,PC微信环境,移动设备) // ===== 判断浏览器环境 ===== // // 判断是否是QQ环境 function isQQ() { retur ...