POJ-3083 Children of the Candy Corn 解题报告

【poj 3083】Children of the Candy Corn 中文题意&题解&代码(C++) poj3083 阅读详情

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


       题目链接:http://poj.org/problem?id=3083

       解法类型:DFS+BFS

       解题思路:这题最短路直用BFS。left和right搜索只是方向问题,用vis数组记录每个点的方向,搜索下一个点时,根据上一个点的方向来分别选择left和right移动方向即可。

       算法实现:

//STATUS:C++_AC_0MS_188K
#include<stdio.h>
#include<memory.h>
const int MAXN=50;
int L_DFS();
int R_DFS();
int S_BFS();
int w,h,x1,y1,x2,y2,q[MAXN*MAXN],fa[MAXN][MAXN]
,dlx[4]={0,-1,0,1},dly[4]={-1,0,1,0}
,drx[4]={0,-1,0,1},dry[4]={1,0,-1,0};
char map[MAXN][MAXN],vis[MAXN][MAXN]={{0}};
int main()
{
//	freopen("in.txt","r",stdin);
	int t,i,j;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&w,&h);
		for(i=0;i<h;i++){
			scanf("%s",map[i]);
			for(j=0;j<w;j++)
				if(map[i][j]=='S')x1=i,y1=j;
				else if(map[i][j]=='E')x2=i,y2=j;
		}

		printf("%d %d %d\n",L_DFS(),R_DFS(),S_BFS());
	}
	return 0;
}

int L_DFS()  //搜索left
{
	int i,j,x,y,nx,ny,tot;
	for(i=0;i<4;i++){ //寻找起点出口
		nx=x1+dlx[i],ny=y1+dly[i];
		if(nx>=0&&nx<h && ny>=0&&ny<w
			&& map[nx][ny]=='.'){vis[nx][ny]=(i+3)%4;break;}
	}

	for(x=nx,y=ny,tot=2;x!=x2||y!=y2;){
		for(i=vis[x][y],j=0;j<4;j++,i=(i+1)%4){ //i=vis[x][y]保证从左路开始搜
			nx=x+dlx[i],ny=y+dly[i];
			if(map[nx][ny]=='.'||map[nx][ny]=='E'){
				vis[nx][ny]=(i+3)%4; //方向选择i的左方向
				x=nx,y=ny;
				tot++;
				break;
			}
		}
	}
	return tot;
}

int R_DFS()  //搜索right
{
	int i,j,x,y,nx,ny,tot;
	for(i=0;i<4;i++){ //寻找起点出口
		nx=x1+drx[i],ny=y1+dry[i];
		if(nx>=0&&nx<h && ny>=0&&ny<w
			&& map[nx][ny]=='.'){vis[nx][ny]=(i+3)%4;break;}
	}

	for(x=nx,y=ny,tot=2;x!=x2||y!=y2;){  //深度搜索
		for(i=vis[x][y],j=0;j<4;j++,i=(i+1)%4){  //i=vis[x][y]保证从右路开始搜
			nx=x+drx[i],ny=y+dry[i];
			if(map[nx][ny]=='.'||map[nx][ny]=='E'){
				vis[nx][ny]=(i+3)%4; //方向i的右方向
				x=nx,y=ny;
				tot++;
				break;
			}
		}
	}
	return tot;
}

int S_BFS()  //搜索最短路,队列优先
{
	memset(vis,0,sizeof(vis));
	memset(fa,0,sizeof(fa));
	int i,u,x,y,nx,ny,tot,front=0,rear=0;
	q[rear++]=x1*w+y1;
	vis[x1][y1]=1;
	while(front<rear)
	{
		u=q[front++];
		x=u/w,y=u%w;
		if(x==x2&&y==y2)break;
		for(i=0;i<4;i++){
			nx=x+dlx[i],ny=y+dly[i];
			if(nx>=0&&nx<h && ny>=0&&ny<w && !vis[nx][ny] && (map[nx][ny]=='.'||map[nx][ny]=='E')){
				vis[nx][ny]=1;
				q[rear++]=nx*w+ny;
				fa[nx][ny]=x*w+y;
			}
		}
	}
	for(nx=x2,ny=y2,tot=1;nx!=x1||ny!=y1;tot++){ //计算最短路
		x=fa[nx][ny]/w;
		ny=fa[nx][ny]%w;
		nx=x;
	}
	return tot;
}






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

相关推荐

AT32F421玩转AM32电调:手把手教你调试过零点比较器,告别电机抖动

本文详细介绍了如何使用AT32F421芯片调试AM32电调的过零点比较器,解决电机抖动问题。通过硬件配置、软件环境搭建、比较器电路原理分析及filter_level参数动态调整等实战步骤,帮助开发者精准诊断和优化电机控制性能。

weixin_42525632的博客 276

POJ-3083 Children of the Candy Corn

Children of the Candy Corn 原题链接 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14564 Accepted: 6283 Description The cornfield maze is a popular Halloween treat. Visitors a...

xu0_zy的博客 592

win11本地部署openclaw实操第1集

OpenClaw 是一款开源、本地优先、可自托管的 AI 智能体(AI Agent)网关 / 自动化框架,核心是让 AI 从 “只聊天、给建议” 变成 “能在你电脑上真做事” 的数字助手。

weixin_44626085的博客 7126

B - Children of the Candy Corn

B - Children of the Candy Corn Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3083 Description The cornfield maze is a popu

CSUST_ACM的专栏 796

Python实现ARIMA模型

以下是一个使用Python实现ARIMA(自回归移动平均模型)的简单示例代码。在运行代码之前,请确保已经安装了pandasnumpy和库。ARIMAorder(2,0,2)p = 2d = 0q = 2forecastmatplotlib在实际应用中,你需要将模拟数据替换为真实的时间序列数据,并且通过合适的方法(如信息准则等)来确定pdq的最佳值。除了auto_arima在使用pmdarima。

huanghm88的专栏 1736

复用浏览器之跳过扫码登录

复用浏览器 1、通过命令启动浏览器:chrome --remote-debugging-port=9222(前提是退出所有的chrome浏览器) 2、启动浏览器之后,在地址栏输入127.0.01:9222,成功复用如下图所示:

qq_43911915的博客 2033

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

O - Children of the Candy Corn Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Practice POJ 3083 Appoint description:  System Crawler  (Jul 24, 201

Calm微笑 511

Ubuntu下Ollama局域网共享大模型服务:5分钟搞定配置与防火墙设置

本文提供了一份在Ubuntu系统上快速配置Ollama实现局域网共享大模型服务的实战指南。通过修改服务配置使其监听所有网络接口,并设置UFW防火墙规则,用户可在5分钟内让同一Wi-Fi下的其他设备(如笔记本、手机)便捷调用主机上的AI模型能力,实现团队或多设备间的AI资源共享。

ik6789012的博客 822

POJ 3083 Children of the Candy Corn 解题报告

最短用BFS即可。关于左手走和右手走也很容易理解,走的顺序是左上右下。 值得注意的是,从起点到终点的右手走法和从终点到起点的左手走法步数是一样。 所以写一个左手走法就好了。贴代码,0MS #include <cstdio> #include <cstring> #include <deque> using ...

141

【Power Compiler手册】4.saif文件

功耗计算的准确性取决于切换活动数据的准确性。这些数据是通过使用RTL仿真或门级仿真生成的,并存储在SAIF文件中。在执行功耗优化和分析之前,应该使用SAIF文件来反标设计对象上的切换活动信息。SAIF是由Synopsys支持的ASCII格式,用于促进不同Synopsys工具之间的信息交换(参见IEEE 1801标准,附录J)。使用`read_saif`命令读取SAIF文件,使用`write_saif`命令写出SAIF文件。有关更多信息,请参见`read_saif`和`write_saif`命令的手册页。

QRBQ 2186

Python 爬虫进阶技巧:新手必学的反爬突破与效率优化

本文系统总结了爬虫进阶的核心技巧,针对新手常见的反爬拦截、效率低下、动态页面解析等痛点问题,提出了可落地的解决方案。主要内容包括:通过随机UA池、代理IP池和Cookie池突破常见反爬机制;利用多线程/异步请求提升爬取效率;使用Selenium解析动态渲染页面;以及数据清洗方法提升数据质量。文中提供了完整的代码示例和实战对比数据,并强调爬虫开发应遵守合规性要求。这些技巧能帮助开发者快速提升爬虫实战能力,应对80%以上的常见爬取需求。

2503_91057718的博客 1436

poj 3083 Children of the Candy Corn 解题报告-- DFS BFS

题目大意:给出一个迷宫,#表示墙,S表示起点,E表示终点。求从S到E的优先靠左边的墙走、优先靠右边的墙走和S到E的最短路径。 思路:考虑向左向右用DFS,最短路径用BFS。注意几点,一是尽量不用递归,开始用的递归,BFS会栈溢出,改用队列实现后没问题,而且其实逻辑上反而更统一,这里DFS还好,递归没有问题,大概是因为有向左向右的规则,并不是一味地遍历所有状态;再一个就是BFS实在是状

taoxin52的专栏 731

MIKE 21 教程 1.5 基于卫星影像绘制河道边界

上一节讲解了如何输入高程数据与绘制网格本节讲解如何根据卫星影像绘制河道边界。在实际项目中,经常遇到河道图纸缺少的情况,此时可以根据卫星影像进行勾勒绘制。常用的地理信息软件均可完成此功能,这里使用大家较为熟悉的ArcGis进行教学(本文默认大家有基础ArcGis操作知识,如有不懂可留言)。

Hello Word 3282

POJ - 3083 Children of the Candy Corn 解题报告

POJ - 3083

薛崇祥的博客 834

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

POJ-3083 Children of the Candy Corn 解题报告(搜索) 广搜深搜走迷宫

B - Children of the Candy Corn Time Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u http://openoj.awaysoft.com:8080/judge/problem/viewProblem.action?id=28859   Descriptio

潘卓夫 1842

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

Poj 3083 Children of the Candy Corn

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

RoronoaDZoro的博客 291

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

【POJ 3083】Children of the Candy Corn

POJ【3083】Children of the Candy Corn Dfs+Bfs

ACMer' 1074

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

poj 3083 Children of the Candy Corn

题目链接:点击打开链接

飞火扑蛾的专栏 522

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

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

闲停 644

【POJ3083】Children of the Candy Corn

本题传送门 本题知识点:深度优先搜索 + 宽度优先搜索 本题题意是求三个路径长度,第一个是一直往左手边走的距离,第二个是一直往右手边走的距离,第三个是最短距离。 第三个很好办,就是一个简单的bfs的模板,问题出在第一二个。 但第一二个只是方向的丝丝不同,所以会其中一个也就解决了。 读懂题意后很简单,问题是怎么简单高效地实现。 推荐这篇博客 一些感慨:该题做了我差不多4个小时吧,前前后后d...

LT4585的博客 304

POJ3083 Children of the Candy Corn 解题思路

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

SDUTACM的博客 507
上一篇: HDU-1728 逃离迷宫 解题报告
下一篇: POJ-3009 Curling 2.0 解题报告
angle555945
博客等级 码龄15年 66粉丝 74原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值