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

【题解】POJ3083 Children of the Candy Corn(DFS + BFS) POJ3083 Children of the Candy Corn 明天写 阅读详情

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

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为终点。

  三种方法从S到E:1.从前进方向的左侧开始顺时针寻找下一个可行点;

2.从前进方向的右侧开始逆时针寻找下一个可行点;

3.找最短路径;

  求分别通过这三种方法走过的格子数。


解题思路:前两种方法用DFS,四个方向,若是方法一,下一个点不可行时,顺时针判断下一个点,可行时,逆时针回到上一个方向。即nextdex=(dex+1)%4和nextdex=(dex+3)%4,方法二直接两个式子位置互换。找最短路显然用BFS,要注意visit数组的标记!!当时忘了TLE了好久。


代码如下:

#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
int dx[4]={-1,0,1,0};
int dy[4]={0,-1,0,1};
char a[42][42];
int vis[42][42];
int n,m,cnt;
bool p;
struct node   //每个格子的信息,num是起点到当前格子的步数
{
	int x,y,num;
};
void dfsl(int x,int y,int i)   //左转优先
{
	if(p||a[x][y]=='E')
	{
		p=true;
		return ;
	}
	int cx=x+dx[i];
	int cy=y+dy[i];
	while(cx<0||cx>=n||cy<0||cy>=m||a[cx][cy]=='#')//下一个点不可行
	{
		i=(i+3)%4;                //顺时针转判断下一个点
		cx=x+dx[i];
		cy=y+dy[i];
	}
	cnt++;
	dfsl(cx,cy,(i+1)%4);      //下一个点可行,方向逆时针回到上一个方向
}
void dfsr(int x,int y,int i)  //右转优先,和左转优先正好相反
{
	if(p||a[x][y]=='E')
	{
		p=true;
		return ;
	}
	int cx=x+dx[i];
	int cy=y+dy[i];
	while(cx<0||cx>=n||cy<0||cy>=m||a[cx][cy]=='#')
	{
		i=(i+1)%4;
		cx=x+dx[i];
		cy=y+dy[i];
	}
	cnt++;
	dfsr(cx,cy,(i+3)%4);
}
int bfs(int sx,int sy)  //找最短路径
{
	queue<node>q;
	node sd;
	sd.x=sx;
	sd.y=sy;
	sd.num=1;
	q.push(sd);
	vis[sx][sy]=1;       //起点也要标记
	while(!q.empty())
	{
		node no=q.front();
		q.pop();
		for(int i=0;i<4;i++)//每个方向都要判断
		{
			int cx=no.x+dx[i];
			int cy=no.y+dy[i];
			if(vis[cx][cy]||cx<0||cx>=n||cy<0||cy>=m||a[cx][cy]=='#')
				continue;       //下一个点不可行,换一个方向
			sd.x=cx;            //下一个点可行,步数加以后加入队列
			sd.y=cy;
			sd.num=no.num+1;
			vis[cx][cy]=1;
			if(a[cx][cy]=='E')
				return sd.num;
			q.push(sd);
		}
	}
}
int main()
{
	int t,sx,sy;
	scanf("%d",&t);
	while(t--)
	{
		memset(vis,0,sizeof(vis));
		scanf("%d%d",&m,&n);
		for(int i=0;i<n;i++)
		{
			scanf("%s",&a[i]);
			for(int j=0;j<m;j++)
			{
				if(a[i][j]=='S')
				{
					sx=i;
					sy=j;
				}
			}
		}
		cnt=1,p=false;
		dfsl(sx,sy,0);
		printf("%d ",cnt);
		cnt=1,p=false;
		dfsr(sx,sy,0);
		printf("%d ",cnt);
		printf("%d\n",bfs(sx,sy));
	}
	return 0;
}



PostgreSQL:Patroni 高可用架构,基于etcd的自动故障转移集群 本文详细介绍了基于Patroni和etcd构建PostgreSQL高可用集群的方案。Patroni作为外部守护进程,通过etcd分布式协调服务实现自动故障转移、主节点选举等关键功能,弥补了PostgreSQL原生缺乏自动故障转移的不足。文章涵盖核心架构(3节点PG+3节点etcd)、环境规划、etcd集群部署(包括配置验证)以及Patroni安装配置的关键步骤,特别强调了Patroni自动初始化PG实例、配置同步复制等生产级特性。该方案通过外部组件协同工作,在不修改PG源码的情况下实现了生产环境所需的高可用 阅读详情

相关推荐

MobileNet V3 图像分类

MobileNet V3

Henry的博客 1774

poj 3083 Children of the Candy Corn【BFS+DFS】

    poj 3083 Children of the Candy Corn Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9528   Accepted: 4126 Description The cornfield maz...

Y390d的博客 256

耕地质量评价软件实操指南:如何用Excel+ArcGIS一键生成土壤普查报告(附避坑技巧)

本文提供了一套融合Excel数据处理与ArcGIS空间分析的耕地质量评价实战工作流。详细讲解了从数据规范化预处理、高效数据桥接,到在ArcGIS中实现指标评分、综合指数计算与等级划分的全流程,旨在帮助土壤普查工作者大幅提升报告生成效率与准确性,并分享了关键的避坑技巧。

weixin_28455801的博客 509

Poj 3083 Children of the Candy Corn

题目大意:前提:(迷宫中一直向左或者向右走(可用DFS),一定可以走出迷宫,即从S走到T) 要求输出一直向左走走出迷宫需要的步数,一直向右走走出迷宫需要的步数,和走出迷宫需要的最小步数(两次深搜,一次广搜) 附AC代码 #include<iostream> #include<queue> using namespace std; #define MapMax 40 #def...

RoronoaDZoro的博客 292

300m半径无人机单兵侦测设备技术详解

300m半径无人机单兵侦测设备凭借其卓越的侦测性能、高效的识别能力、便捷的告警方式以及广泛的应用场景,成为现代军事、安保及应急救援领域的重要装备。300m半径无人机单兵侦测设备,融合了最新的无人机技术、高精度传感技术与实时数据处理技术,为单兵作战、巡逻及侦察任务提供了前所未有的视野与效率。该设备通过无人机搭载的高性能侦测模块,能够在复杂环境中实现300米范围内的精准侦测,大幅提升单兵的战场感知能力和决策效率。设备为被动式探测设备,确保安静安全运行,不暴露目标 设备耗电功率小,可长时间在野外使用。

专注多旋翼、复合翼无人机技术研究,从事低空经济行业。 1128

AI提示词实战:用EARS语法5步搞定产品需求拆解(附完整案例)

本文详细介绍了如何运用EARS语法将模糊的产品需求进行原子化拆解,转化为清晰、可执行的结构化指令。通过一个完整的自由职业者项目管理工具案例,演示了从原始需求分析到构建增强AI提示词的五步实战流程,帮助产品经理和技术人员实现高效、精准的需求传达与协作。

nokia的博客 1050

[POJ 3083]Children of the Candy Corn[DFS+BFS]

题目链接:[POJ 3083]Children of the Candy Corn[DFS+BFS] 题意分析: 从起点S到终点E,贴着左边的墙壁走需要几步?贴着右边的墙壁走需要走几步?直接走最短路需要几步?请全部输出来。 解题思路: 第三项用BFS求最短路即可。主要是前两项。首先自己模拟一下如果让你贴着墙壁走你会怎么走?然后根据自己的逻辑编写DFS即可。贴左边和贴右边差不多,改改就行。另

闲停 644

uniapp运行钉钉小程序

因项目原因,公司需要在钉钉里面开发小程序。之前用uniapp开发过app,H5,小程序。还真没尝试过钉钉小程序,今天就简单的记录下uniapp运行钉钉小程序中的过程。

YL 2926

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

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 t

光明~~~ 2万+

Cartographer纯定位实战:从仿真到真机部署的避坑指南

本文详细介绍了将Cartographer算法应用于机器人纯定位模式的完整实战流程。通过对比传统AMCL方案的不足,阐述了Cartographer基于子图匹配实现高精度、高鲁棒性定位的优势。内容涵盖从仿真环境测试到真机部署的核心步骤,包括关键配置文件修改、源码调整以及参数调优,旨在帮助开发者避开常见陷阱,成功实现稳定可靠的机器人定位系统。

weixin_29063681的博客 253

ABAP 大规模数据并行处理实战,六套 Parallel Processing Framework 如何选,性能调优真正该盯住什么

如果今天真的面对那份执行 14 个小时的生产报表,我不会马上把原来的 Loop 改成 50 个aRFC。我会先用 SAT、ST12、SQL Monitor 和 HANA 侧工具确认时间到底花在哪里。如果 90% 时间其实消耗在一条低效 SQL 上,Parallel Processing 只是让 50 个 Worker 同时执行那条低效 SQL,数据库会比原来更痛苦。应该先把串行逻辑本身优化到合理水平。该做 Set-based Processing 的地方先交给 HANA。该避免SELECT。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 164

POJ3083 Children of the Candy Corn【DFS、BFS】

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

胡小涛的博客 227

人形机器人电源系统硬件开发:从原理图设计到PCB布局的完整流程

人形机器人是“移动的智能终端”,其电源系统如同“心脏+血管”:既要为大脑(主控)、肌肉(电机)、感官(传感器)提供稳定能量,又要在有限体积内应对高动态负载(如跳跃时电机瞬间大电流)。本文聚焦“硬件开发”维度,覆盖从需求分析到PCB制板的完整流程,适合硬件工程师、机器人爱好者快速掌握工程实战方法。

AI天才研究院 2186

POJ3083 Children of the Candy Corn BFS+DFS

题目大意:给出一个n*m的迷宫和起点S以及终点E,让你找出一下三种迷宫求解结果:(1)左转优先搜索;(2)右转优先搜索;(3)迷宫最短路线; 分析:前两者可以借助DFS来完成,对于最短路线,就只能用BFS了。 我们首先来考虑一下怎么实现左转优先,对于下图来说,最初的方向由起点S确定,而下一步的方向则由前一步的 方向决定。 例如 左边优先搜索: 当前位置的方向指向 1(向左),(这同时说明

AC_Gibson的专栏 518

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

Children of the Candy Corn(BFS+DFS POJ3083)

~题目链接~ http://poj.org/problem?id=3083 Sample Input 2 8 8 ######## #......# #.####.# #.####.# #.####.# #.####.# #...#..# #S#E#### 9 5 ######### #.#.#.#.# S.......E #.#.#.#.# ####...

ddn220120的博客 157

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

poj 3083 Children of the Candy Corn dfs+bfs

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

呼,是出一口气;吸,是争一口气! 524

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

[Poj 3083] Children of the Candy Corn bfs+dfs

Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12060 Accepted: 5177Description The cornfield maze is a popular Halloween treat. Visitors are shown

ATOD 764

POJ 3083-Children of the Candy Corn(dfs+bfs)

Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10380   Accepted: 4485 Description The cornfield maze is a popular Halloween t

Rocky0429 1635

POJ3083-Children of the Candy Corn(bfs+dfs)

POJ3083 原题链接:http://poj.org/problem?id=3083 Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14221   Accepted: 6151 Description

Yorozuya 317

POJ3083 Children of the Candy Corn (BFS ,DFS)

Children of the Candy Corn Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submission(s) : 6   Accepted Submission(s) : 6 Problem Description The co

daydream1000的博客 313

POJ3083 Children of the Candy Corn DFS+BFS

这道题最坑的一点是,,G++一直WA,但是C++过了。。。。。EXM????? 一开始我以为可能有不记录方向的方法,但网上一搜发现,,算了还是慢慢做吧。。比较巧妙的是: 1. 把方向用数字表示,避免了字符串后续很烦的操作。 2. DFS时不能用vis数组,用break保证了每步只走一种情况并且不会陷入死循环。 附上AC代码(又丑又长QAQ): #include<cstdio&gt...

A confused programmer 362

poj 3083 Children of the Candy Corn——dfs与bfs的混合

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 ex

five_east_west的博客 234

POJ3083 Children of the Candy Corn(Bfs + Dfs)

题意:给一个w*h的迷宫,其中矩阵里面 S是起点,E是终点,“#”不可走,“.”可走,而且,S、E都只会在边界并且,不会在角落,例如(0,0),输出的话,每组数据就输出三个整数,第一个整数,指的是,以S的起点为当前所对着的路径为正方向,如果正方向的左边能走的话,就走左边,不能就按正方向走,不行的话就就往回走,如此反复,记录步数,并输出,第二个整数也是如此,只不过搜的方向改成正方向的右边。...

angjijian7243的博客 180

poj 3083 Children of the Candy Corn DFS+BFS

题意: 按照靠左走,靠右走以及最短路径的规则求从S点出发到E点的距离 方向有点难,因为当你改变方向的时候,下一步的方向也就随之改变了; #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <que...

ailincheng0815的博客 170

POJ3083 Children of the Candy Corn(BFS+DFS)

题意: 普通的迷宫求路径,但这题需要输出三个解:1.沿着左边查找,2.沿着右边查找,3.最短路径 要点: 最短的简单,直接BFS搞定。沿着左边查找和沿着右边查找其实是一个问题,也即是DFS中先遍历左边还是右边,沿着左边查找就把顺时针查找,如果不成功就向右查找,向右查找一次其实是向前,两次向右,三次向后,反之同理。 15191443 Seasonal 3083

SeasonJoe的博客 393

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

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 que

旧时光 832
上一篇: poj 2488 A Knight's Journey (DFS)
下一篇: poj 3009 Curling 2.0 (DFS)
雨狮子
博客等级 码龄12年 24粉丝 185原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址