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

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

 

 

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

 

题意:

 

三个输出,第一个数据是要求从开始的那个点开始一直沿着左边的强走,一直走到终点,如果没路了就原路返回,返回的路程也算在总步数step里面,第二个则是沿着右边的墙走,第三个是最短路径;

 

思路:

第三个数据是最好求的,BFS不断扩展就能求出,比较麻烦的是第一个和第二个的数据,因为每到一个地方,可能遍历的方向会不同,也就没办法一个数组就存一个固定遍历方向来求解了,我们需要在dfs的调用参数里面加一个 last_p 来记录上一次的位置状态,这里的状态是一个方向的代表值; 我们需要遍历的方向就只有 4 个,上下左右,对这些方向用数字来标记一下,分别是:

右(0),下(1),左(2),上(3) ,对应的方向数组的下标与状态相同,每走一个地方,用 last_p 来判断该遍历的方向是什么,比如 last_p = 0 ,那么,该遍历的方向是 1,2 , 3 ,0,因为沿着左边的墙走,那么遍历的方向必须是顺时针的,这里的dfs不能标记走过的地方,因为一般情况是需要往回走的,而且返回步数也需要记录下来 ,如果 last_p = 0 ,然后又遍历到 0 这个方向就说明开始往回走了,这是第一个数据的,第二个数据只需要从 结束的点走到开始的点,然后记录步数就行了,因为从开始沿着右边的墙走到终点是逆时针,而从结束点走到开始的点是顺时针,但步数是等价的,这样就不需要写两个dfs了,一个就能算两个的步数,下面给出代码:

 

 

#include<iostream>
#include<string>
#include<cstring>
#include<queue>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;

char a[45][45];
int x,y,dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}},book[45][45];  //右下左上  
int ri,le;

struct node {
	int x_,y_,step;
};

int bfs(struct node tp) {
	queue<struct node> qu;
	qu.push(tp);
	book[tp.x_][tp.y_] = 1;
	struct node tmp,Front;
	while(!qu.empty()) {
		Front = qu.front();
		qu.pop();
		if(a[Front.x_][Front.y_] == 'E') return Front.step;
		for(int i = 0 ; i < 4 ; ++i) {
			tmp.x_ = Front.x_ + dir[i][0];
			tmp.y_ = Front.y_ + dir[i][1];
			if(tmp.x_ >= 0 && tmp.x_ < x && tmp.y_ >=0 && tmp.y_ < y && a[tmp.x_][tmp.y_] != '#' && book[tmp.x_][tmp.y_] == 0) {
				tmp.step = Front.step + 1;
				book[tmp.x_][tmp.y_] = 1;
				qu.push(tmp);
			}
		}
	}
	return -1;
}

int flag;

void dfs(int cur_x,int cur_y,int e_x,int e_y,int step,int last_p) {
//	cout << cur_x << " " << cur_y << endl;
	if(cur_x == e_x && cur_y == e_y) {
		le = step;
		flag = 1; 
		return;
	}
	int tmp = (last_p + 1)%4,tp_x,tp_y;
	for(int i = tmp  ;  ; i = (i + 1)%4) { // 方向的遍历,+ 1 % 4,保证状态是在3以内的值 
		tp_x = cur_x + dir[i][0];
		tp_y = cur_y + dir[i][1];
		if(tp_x >= 0 && tp_x < x && tp_y >= 0 && tp_y < y && a[tp_x][tp_y] != '#') {
			dfs(tp_x,tp_y,e_x,e_y,step + 1, (i + 2)%4); // 这里代入的 last_p 要做一些处理,
													// 因为这里算出来的 i 是一个反方向,比如 i 是 2 ,是向下的,那last_p 应该为 4
													// 是 2 的反方向 状态 
			if(flag == 1) return; // 一旦到了终点就不用在dfs了,一路return回起点,时间会快一点 
		}
	}
}


int main(void)
{
	int n,s_x,s_y,e_x,e_y;
	while(scanf("%d",&n)!=EOF) {
		for(int k = 0 ; k < n; ++k ) {
			
				scanf("%d%d",&y,&x);
				for(int i = 0 ; i < x ; ++i) {
					getchar();
					for(int j = 0 ; j < y ; ++j) {
						scanf("%c",&a[i][j]);
						if(a[i][j] == 'S') {
							s_x = i;s_y = j;
						}
						if(a[i][j] == 'E') {
							e_x = i;e_y = j;
						}
					}
				}
				// BFS找最小路径 
				struct node start;
				start.x_ = s_x;start.y_ = s_y;
				start.step = 1;
				memset(book,0,sizeof(book));
				int res;
				res = bfs(start);
				
				int pos; // 判断起点的方向状态 
				
				if(s_x == 0) pos = 3;
				else if(s_x == x - 1) pos = 1;
				else if(s_y == 0) pos = 2;
				else pos = 0;
 				dfs(s_x,s_y,e_x,e_y,1,pos);
 				
 				printf("%d ",le);
 				
 				if(e_x == 0) pos = 3;
				else if(e_x == x - 1) pos = 1;
				else if(e_y == 0) pos = 2;
				else pos = 0;
				dfs(e_x,e_y,s_x,s_y,1,pos);
				
				ri = le;
				printf("%d %d\n",ri,res);
			}
		} 
	return 0;
 }

 

 

 

 

 

POJ3083-Children of the Candy Corn 北大POJ3083-Children of the Candy Corn 解题报告+AC代码 立即下载

相关推荐

POJ 3083 Children of the Candy Corn解题报告

业余爱好。所以,算法不一定好,CODING也不一定佳,效率不一定高,只是能通过online judge而已。

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

六自由度机器人(机械臂)运动学建模及运动规划系列(一)——简介

毕业设计做了六轴机器人相关的课题,做完之后学到很多,在这里分享一下。

weixin_47849087的博客 2万+

poj3083

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

weixin_33719619的博客 260

从《视觉SLAM十四讲》到项目实战:一文读懂拓扑地图与语义地图为何是服务机器人的未来

本文探讨了服务机器人地图技术的演进,从传统几何地图到拓扑地图与语义地图的跨越。通过分析栅格地图的局限性和拓扑地图、语义地图的优势,揭示了它们在动态开放环境中的智能应用。文章还介绍了多模态语义融合架构和动态语义更新技术,为服务机器人的未来发展提供了技术路线图。

weixin_30292843的博客 478

华中科技大学 计算机组成原理 educoder Logisim平台 计算机数据表示实验

本文主要是对 计算机数据表示实验的笔记和相关Logisim平台的图,供大家学习交流使用。 需要源文件或者源代码,可以留言评论! 目录实验目的第1关:汉字国标码转区位码实验第2关:汉字机内码获取实验第3关:偶校验编码设计第4关:偶校验解码电路设计第5关:16位海明编码电路设计第6关:16位海明解码电路设计第7关:海明编码流水传输实验第8关:16位CRC并行编解码电路设计第9关:CRC编码流水传输实验实验结果: 实验目的 本实训将帮助同学们巩固对计算机中数据表示方法的理解,要求能设计汉字国标码与区位码之间的转换

weixin_48383580的博客 2万+

Children of the Candy Corn (dfs+bfs)

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

sdau20163940的博客 464

从零到千兆:KSZ9031 PHY芯片在Zynq PS端的驱动移植心路历程

本文详细记录了在Xilinx Zynq-7000平台(基于XC7Z020芯片)上,为KSZ9031千兆以太网PHY芯片移植LwIP协议栈驱动的完整过程。从硬件连接配置、Vivado工程设置到LwIP库的深度修改与调试,分享了解决PHY识别、自协商及性能优化等关键问题的实战经验,助力开发者实现稳定的千兆网络连接。

honey的博客 1106

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

FAST_LIO_ROS2 可以用gazebo仿真吗?

摘要:FAST_LIO_ROS2可与Gazebo仿真环境兼容,通过订阅Gazebo模拟的激光雷达和IMU数据实现SLAM建图。已有开源项目(如pb_rm_simulation)验证了该方案,支持导航模式切换与真实机器人移植。关键步骤包括环境配置、传感器话题匹配及参数调试,需注意版本兼容性与数据噪声处理。相关教程提供了从环境搭建到三维建图的完整流程。

专注 激光SLAM, 目标检测, 欢迎关注, 分享交流! 1138

单智能体篇:Prompt工程艺术

模型在接收到 Prompt 时会自动扮演“角色”,角色定义的好坏直接影响智能体输出的风格和专业性。角色定义的重要性让智能体具备明确身份,例如“你是一位专业理财顾问”或“你是一个幽默的文案创作助手”。角色限定了模型知识输出的范围,使回答更专业、更贴合预期。实战示例模糊Prompt给我一些投资建议。你可以投资股票或者基金。优化Prompt你是一位具有10年经验的专业理财顾问,请给我针对30岁男性,年收入20万,风险偏中等的投资组合建议。

公众号:AI前沿侦探 1304

POJ 3083 Children of the Candy Corn

明白题意,S->E,第一个输出左偏到达的步数,第二个输出右偏到达的步数,第三个输出最短路用BFS 左偏的意思是,开始方向任意定义并且在后面都始终不会变,先看左边的格子是否能走,然后逆时针(就是从左边的第一个格子往右偏). 右偏,开始跟左一样,先看右边的格子是否能走,然后顺时针(说白了就是从右边的第一个格子往左偏), #include #include #include #include

sdut_jean的博客 350

UDF学习(二)基础数据类型与常见宏

thread_loop_c (t,d) 必须与begin_c_loop (c,t)同时使用,但begin_c_loop (c,t)不一定与thread_loop_c (t,d)同时使用。同理thread_loop_f(t,d)必须与begin_f_loop (c,t)同时使用,但begin_f_loop (c,t)不一定与thread_loop_f(t,d)同时使用。begin_c_loop (c,t) 在计算域内对网格单元进行循环。c_face_loop (c,t)遍历网格单元上的所有网格面。

weixin_44386973的博客 856

Poj 3083 Children of the Candy Corn

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

RoronoaDZoro的博客 291

LearningToRank(LTR)排序算法LGBMRanker的原理和使用

1. 写在前面 在最近新闻推荐的比赛中, 接触到了一个排序模型LGBMRanker, 该模型与普通的分类模型LGBMClassifier不太一样, 普通的分类模型在进行推荐的时候, 往往是先预测某个商品或者文章用户会不会点击, 也就是它的目标是预测用户点击某篇文章或者某个商品的概率, 然后根据这个概率值进行排序, 排完之后,把最靠前的几个返回回来给用户进行推荐。 而LGBRanker模型, 它不关心用户点击某篇文章的概率, 而是根据用户点击或者不点击的这个行为, 直接去预测最后商品或者文章的一个相对顺序,

Miracle8070 1万+

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

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

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

闲停 644

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 (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万+

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

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

POJ3083 Children of the Candy Corn BFS+DFS

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

AC_Gibson的专栏 517

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的博客 156

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

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

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 361

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

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
上一篇: POJ 1129 Channel Allocation【四色猜想+DFS染色】
下一篇: POJ Sudoku 【DFS】
Y390d
博客等级 码龄9年 11粉丝 165原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值