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 明天写 阅读详情

相关推荐

Smart200 PLC实现Bool信号接通延时、关闭延时作用

解决Smart200 PLC时间继电器和脉冲扫描P或N不能重复使用的问题,用于Bool信号延时接通和延时关闭的作用,可调整时间基数

gk_121的博客 4318

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

【论文精读】Formal Mathematical Reasoning: A New Frontier in AI

注:本文根据师兄汇报内容总结。

m0_60634555的博客 1189

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自动化测试报告:飞书群机器人消息卡片实战

本文详细介绍了如何利用Python和飞书群机器人实现自动化测试报告的智能通知。通过构建可定制的消息卡片,将测试结果(如通过数、失败数)以清晰、美观的格式自动推送至团队群聊,并支持一键跳转查看详细报告,从而显著提升团队协作与问题响应效率。

k6l7m8n9的博客 811

更多 LSTM 变种——应用于 CMAPSS 数据集

本文并模型介绍,只是把一些比较火的技术,像双向注意力机制,自注意力机制这样的比较火的内容,简单的和LSTM堆叠,结果发现效果都不如最基础的,这里作者将本人代码及实现结果,进行展示,给各位想更改LSTM的朋友一些。

2301_77491330的博客 1765

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

扣子Coze智能体万字教程:从入门到精通,一文掌握AI工作流搭建

智能体 = 角色设定 + 提示词 + 变量输入一个好提示词的核心是结构、风格和复用性创建后可以一键发布成网页应用、对话助手整个过程0代码,只靠配置,人人可学,人人可用四、进阶一:理解「工作流」的威力如果你已经用扣子创建了几个简单的智能体,比如写个爆款小红书文案、生成一段短视频脚本,那恭喜你已经入门。但如果你还停留在“一个智能体 = 一段提示词”的层级,你离真正的智能体生产力革命,还差一步:学会搭建“工作流”——让多个大模型自动协同完成任务。

互联网架构师笔记 3501

POJ 3083 Children of the Candy Corn 解题报告

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

141

ANSYS2022R1——安装源文件下载和安装教程——软件管家

14.当提示输入安装目录:#3时,①点击【浏览】,②选安装包解压后的【ANSYS2022R1_WINX64_Disk2】文件夹中【ANSYS2022R1_WINX64_Disk3】文件夹,③点击【选择文件夹】,④点击【OK】。12.当提示输入安装目录:#2时:①点击【浏览】,②选安装包解压后的【ANSYS2022.R1(64bit)】文件夹中的【ANSYS2022R1_WINX64_Disk2】文件夹,③点击【选择文件夹】,④点击【OK】。】②点击【OK】③点击【OK】。

qq_15963745的博客 2325

华东理工大学计算机预推免面试

华东理工大学预推免面试经验分享

D1379951481的博客 1402

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

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

taoxin52的专栏 731

NTscan扫描工具

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

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

POJ - 3083

薛崇祥的博客 834

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

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

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

Poj 3083 Children of the Candy Corn

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

RoronoaDZoro的博客 291

【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、付费专栏及课程。

余额充值