回溯法+奇偶剪枝——Hdu 1010 Tempter of the Bone

基于MediaPipe的智能俯卧撑计数与姿势矫正系统 在现代健身和体能训练中,俯卧撑是最基础也是最有效的自重训练动作之一。然而,许多人在进行俯卧撑训练时常常存在姿势不正确、计数不准确等问题。本文将介绍如何利用计算机视觉和姿态估计技术,开发一个智能的俯卧撑计数与姿势矫正系统。 阅读详情

1)   题目

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 47967    Accepted Submission(s): 12905


Problem Description

The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.

The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.

 

 

Input

The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:

'X': a block of wall, which the doggie cannot enter; 
'S': the start point of the doggie; 
'D': the Door; or
'.': an empty block.

The input is terminated with three 0's. This test case is not to be processed.

 

 

Output

For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.

 

 

Sample Input

4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0

 

 

Sample Output

NO
YES

 

2)    题意

在一个n行m列的迷宫中,每一步只能向上、下、左、右中任意方向走一格,迷宫中有围墙的地方是无法到达的。从起点s开始,能否刚好走t步,到达e。


3)    数据范围

迷宫大小最大为6*6,测试数据组数最大为50组。感觉上数据量很小,可实际上,如果在6*6的迷宫中,枚举所有可能路径的话,时间复杂度是指数级的,所以剪枝是关键。


4)    算法

先用BFS判断s到e是否有路径,以及这条最短路径长度是否小于等于t,然后再进行回溯法+奇偶剪枝。

迷宫中回溯法的剪枝——奇偶剪枝


5)    代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

#define MAXSIZE 10

int dir[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};

char maze[MAXSIZE][MAXSIZE];
bool vis[MAXSIZE][MAXSIZE];
int dx, dy;
bool finished;

void InitMaze(int m, int n)
{
	int i;
	for (i = 0; i < m+2; i++)
	{
		maze[i][0] = maze[i][n+1] = 'X';
	}
	for (i = 0; i < n+2; i++)
	{
		maze[0][i] = maze[m+1][i] = 'X';
	}
}

void Backtrack(int x, int y, int rt)
{
	vis[x][y] = true;
	if (x == dx && y == dy)
	{
		if (rt == 0)
		{
			finished = true;
		}
	}
	else
	{
		int i;
		for (i = 0; i < 4; i++)
		{
			int nextx = x + dir[i][0];
			int nexty = y + dir[i][1];
			if (!vis[nextx][nexty] && maze[nextx][nexty] != 'X')
			{
				Backtrack(nextx, nexty, rt-1);
				if (finished)
				{
					return;
				}
			}
		}
	}
	vis[x][y] = false;
}

struct Position
{
	int x, y;
	int nsteps;
};

int BFS(int x, int y)
{
	queue<Position> next;
	Position currPos = {x, y, 0};
	next.push(currPos);
	vis[x][y] = true;

	while (!next.empty())
	{
		currPos = next.front();
		next.pop();

		if (currPos.x == dx && currPos.y == dy)
		{
			return currPos.nsteps;
		}

		int i;
		for (i = 0; i < 4; i++)
		{
			Position nextPos = currPos;
			nextPos.x += dir[i][0];
			nextPos.y += dir[i][1];
			if (!vis[nextPos.x][nextPos.y] && maze[nextPos.x][nextPos.y] != 'X')
			{
				nextPos.nsteps++;
				next.push(nextPos);
				vis[nextPos.x][nextPos.y] = true;
			}
		}
	}
	return -1;
}

int Dist(int x, int y)
{
	return abs(dx-x)+abs(dy-y);
}

int main(void)
{
	int m, n, t;
	while (scanf("%d%d%d", &m, &n, &t) != EOF)
	{
		getchar();
		if (m == 0 && n == 0 && t == 0)
		{
			break;
		}

		InitMaze(m, n);
		int sx, sy;
		int i, j;
		for (i = 1; i <= m; i++)
		{
			for (j = 1; j <= n; j++)
			{
				maze[i][j] = getchar();
				if (maze[i][j] == 'S')
				{
					sx = i;
					sy = j;
				}
				if (maze[i][j] == 'D')
				{
					dx = i;
					dy = j;
				}
			}
			getchar();
		}
		
		finished = false;
		if (Dist(sx, sy) % 2 == t % 2) //奇偶剪枝
		{
			memset(vis, false, sizeof(vis));
			int minNSteps = BFS(sx, sy);
			if (minNSteps != -1 && minNSteps <= t)
			{
				memset(vis, false, sizeof(vis));
				Backtrack(sx, sy, t);
			}
		}
		if (finished)
		{
			puts("YES");
		}
		else
		{
			puts("NO");
		}
	}
	return 0;
}


6)    测试数据

4 4 5

S.X.

..X.

..XD

....

3 4 5

S.X.

..X.

...D

5 5 9

...D.

X.XX.

.XX..

SX...

....X

7)    提交结果


【实战项目开发技术分享】如何设置机器人禁行区/虚拟墙 在现代机器人技术的日益发展中,路径规划是使机器人在复杂环境中安全、高效移动的关键部分。其中一个重要的问题是如何划定机器人的禁行区,以确保机器人不会进入危险区域或无法到达的地方。在本文中,我们将探讨一些关键策略,帮助机器人算法工程师更好地划定禁行区。代价地图可以通过多层叠加来实现,一般包括以下几个层级:静态地图层(Static Map Layer):由SLAM(Simultaneous Localization and Mapping)构建的静态地图。该层包含了环境中固定不变的地物信息,如墙壁、家具等。 阅读详情

相关推荐

【知识点总结】电路原理 第一讲

大家好,从今天起,我将开始更新自上大学以来所学到的各种知识,之所以选择更新它们是因为最近我一直在收拾整理自己自上大学以来所收获到的各种知识,看着那厚厚一沓的书本心里不由得犯愁。如果我想找到特定的知识,那岂不是得翻到猴年马月。俗话说得好,下雨天打鞋子,闲着也是闲着,为此我设置一个专栏,将知识点一一分类总结,目的在于更好的整理所学的知识。本人学艺不精,有一些知识点地方可能存在瑕疵,希望各位大佬可以多多指教。以下是本篇文章正文内容虚断:一般通用性运算放大器的正反向输入端之间的电阻非常非常的大。

weixin_51130221的博客 4万+

杭电OJ——Tempter of the Bone

Tempter of the Bone                         Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Description The doggie found a bone in an ancient m

李树花开,风中摇曳 1401

PSM-DID实战指南:Stata代码解析与案例应用

本文详细解析PSM-DID方法在Stata中的实战应用,从理论到代码实现全面覆盖。通过倾向得分匹配(PSM)和双重差分(DID)的结合,有效解决政策评估中的选择偏差问题。文章提供完整的Stata命令代码、样例数据处理技巧和案例应用,帮助研究者准确评估政策效果。

vulkan6gpu的博客 791

DFS 中奇偶 剪枝优化代码

编辑分类 奇偶剪枝是数据结构的搜索中,剪枝的一种特殊小技巧。 基本信息 中文名称 奇偶剪枝 用于 数据结构的搜索   定义 剪枝的一种特殊小技巧 属于 数学定理 目录 1描述 2结论 3原理补充 折叠编辑本段描述 现假设起点为(sx,sy),终点为(ex,ey),...

alxbaj的博客 214

Docker网络教程(四)macvlan 网络教程

在这种网络下,Docker 宿主机在它的IP之上,允许接受多个 MAC 地址的请求,然后再将这些请求路由至对应的容器里。流通,Docker 会用它的 MAC 地址,把流量路由进你的容器里。对于网络上的网络设备来说,你的容器似乎是物理上连接到网络上的。),Docker使用其MAC地址将流量路由到你的容器。对于网络上的网络设备来说,你的容器似乎是物理上连接到网络上的。你需要 3.9 或者更高版本的 Linux 内核,4.0 以及以上的版本是更好的。如果你的设备叫别的名字,用对应的名字代替。

weixin_42445065的博客 4132

C++ 核电站

问题描述 一个核电站有n个放核物质的坑,排列在一条直线上。如果连续m个坑中放入核物质就会发生爆炸,所以某些坑中不能放核物质。 本题的任务:对于给定的n和m,求不发生爆炸的放置核物质的方案总数。 输入格式 第一行两个正整数n和m,之间用一个空格隔开。2≤n≤50,2≤m≤5。 输出格式 一行一个正整数,表示方案总数。 输入样例 4 3 输出样例 13 数据规模与约定 2≤n≤50,2≤m≤5 CODE #include<bits/stdc++.h> using namespace

HybridCOW_HORSE的博客 1803

DFS 奇偶剪枝 HDU 1010

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 120609    Accepted Submission(s): 32558 Problem Description The

Singular__point的博客 348

手把手教你用OpenCalib完成激光雷达与相机联合标定:从数据准备到结果可视化

本文详细介绍了使用OpenCalib工具链中的lidar2camera模块完成激光雷达与相机联合标定的全流程,包括数据准备、标定命令执行、结果验证与可视化。通过实战指南,帮助读者掌握工业级标定技术,提升自动驾驶和机器人领域的传感器融合精度。

weixin_30872499的博客 398

Tempter of the Bone--hdu1010--zoj2110

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 89873Accepted Submission(s): 24438 Problem Description The doggi...

aoao2005的博客 157

搬运机器人系列编程:KUKA KR QUANTEC_(2).KUKA KRL语言编程基础

定义一个函数END!调用函数PRINT "结果: ", result!定义一个过程PRINT msgEND!调用过程printMessage("欢迎使用 KUKA 机器人")!定义搬运零件的起始位置和目标位置!定义焊接路径的各个点!定义喷涂路径的各个点KUKA Robot Language (KRL) 是一种专门为工业机器人设计的编程语言,具有结构化编程、丰富的库函数、实时性以及集成性等特点。

zhubeibei168的博客 1246

PostgreSQL的学习心得和知识总结(一百二十六)|深入理解PostgreSQL数据库SQL执行时间计算 \timing 和 log_duration的使用和原理

1、参考书籍:《PostgreSQL数据库内核分析》2、参考书籍:《数据库事务处理的艺术:事务管理与并发控制》3、

孤傲小二~阿沐的博客 984

迷宫中回溯法的剪枝——奇偶剪枝(hdu 1010 Tempter of the Bone)

转自:http://blog.youkuaiyun.com/code_pang/article/details/8839432 问题描述 在一个n行m列的迷宫中,每一步只能向上、下、左、右中任意方向走一格,迷宫中有围墙的地方是无法到达的。从起点s开始,能否刚好走t步,到达e。 例如在下面5行5列的迷宫中,能否恰好经过9步,从s走到e。初始位置在s上,#是围墙。 ...

dengjing1200的博客 153

2026 Claude Code 标准化前端UI SKILL——Frontend Design

是 Anthropic 官方开源的技能包,在 Claude 官方插件站中排名第一,超过 52k 的下载量。场景推荐方案快速体验批量安装官方 Skills深度设计策略叠加完整打磨流程集成终端跑claude一句话总结:Frontend Design 不是给 AI 加新能力,而是给 AI 加审美标准。它让 AI 从“能画”变成“会设计”——先想清楚风格,再动手写代码。

十年老程序猿,AI 疯狂研究专家,前沿硬核知识分享 3606

【hdoj_1010】Tempter of the Bone(迷宫+剪枝)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意:给出一个迷宫(含起点和终点),要求找出一条路径,这条路径的长度必须为某个规定的长度. 在做本题之前,先学习了一下迷宫问题:http://blog.youkuaiyun.com/ten_sory/article/details/66975811 在理解迷宫问题的基础上,再做本题.本

tensory的博客 1165

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

IMSL CNL 7.0.0 (x86-64)Crack

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

HDU.1010 Tempter of the Bone

文章目录一、题目解读1、原题2、分类3、题意4、输入输出格式5、数据范围二、题解参考1、总体思路2、思路①(1).分析(2).AC代码三、评价与后话1、评价2、奇偶剪枝0-1示图绘制代码(C++——EasyX)3、后话 一、题目解读 1、原题 HDU.1010 Tempter of the Bone 2、分类 回溯法 3、题意 给定一个N×MN \times MN×M的迷宫让一只狗去走,迷宫中有一...

God-Excious的博客 808

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

HDU 1010 - Tempter of the Bone 奇偶剪枝+深搜

HDU 1010 - Tempter of the Bone 题意: 在一个迷宫中,小狗的位置为 S ,出口的位置在 D ,出口只有在第 t 秒时才会开门让小狗出去。x 为不能走的的位置(小狗在任意一个位置停留时间不能大于等于 1 秒,因为到达1秒那个位置会下降)。求小狗能否走出迷宫 思路: 简单的 dfs+奇偶剪枝 第一次学奇偶剪枝。。。不会的可以看这篇博客:迷宫中回溯法的剪枝——奇偶...

没有梦想何以远行 240

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

HDU1010 Tempter of the Bone(DFS+奇偶剪枝)

题目来源 HDU1010 Tempter of the Bone Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie c...

菜的只能随便写写博客 232

HDU1010 Tempter of the Bone DFS+剪枝

题目描述 The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the ...

qq_43598944的博客 228

HDU 1010 Tempter of the Bone

题目地址:点击打开链接 思路:普通回溯要超时,用到奇偶减枝,网上资料比较多,说我的理解:判断起始点到终点的最短步数,无论偏移几格,偏移的步数一定为偶数,所以最短走的路和要走的路奇偶性相同,所以他们的和或差就相同,不相同就可以直接回溯; AC代码: #include #include #include #include using namespace std; char lol[8][8]

Java识堂,一个有干货的微信公众号 442

Tempter of the Bone解析(回溯算法)hdu1010 zoj2110

Tempter of the Bone<br /><br />Problem DescriptionThe doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a

qq943574281的专栏 1438

HDU 1010 Tempter of the Bone 【DFS】

思路:        注意:题目意思不是要求在T秒之前到达门口,不能用BFS,所以要求到达门口的所有情况中刚好在T时刻到达门口的一种情况。    AC代码:    #include #include using namespace std; char map[8][8]; int viste[8][8]; int fx[4]={1,-1,0,0},fy[4]={0,0,1,-1}

Job_yi的专栏 752

HDU 1010 Tempter of the Bone (DFS+奇偶剪枝,一个起点一个终点)

Description给定一个图的起点begin和终点end以及所需要的时间,X 不能通过,. 可以通过,S 是开始坐标,D 结束坐标,问是否恰好在T秒到达终点。InputThe input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1

sunlanchang的博客 629

HDU - 1010 Tempter of the Bone(奇偶性剪枝DFS)

题目链接 一开始审题不认真,以为是BFS.认真看了之后其实应该是DFS.想不TLE还得会奇偶性剪枝.关于奇偶性剪枝这个博客讲的很不错. 附上代码(好久没敲搜索题了,不是很简洁) #define LL long long #define pb push_back #include <iostream> #include <algorithm> #include <str...

weixin_45590210的博客 113

hdu1010-Tempter of the Bone-(dfs+奇偶剪枝)

http://acm.hdu.edu.cn/showproblem.php?pid=1010 翻译:有只狗被困了,S是起点,D是门,W是墙不能走,‘ . ’是可以走的路,走一次就会在1秒内坍塌,也就是不能停留也不能走回头路,门只在第T秒打开,问是否能逃命? 解题:一开始以为是三维bfs,但是地图上的时间维度是错误的,因为走过一次地面坍塌,只能有一个时间维度。百度找了居然一个bfs都没...

dengpan1889的博客 151

HDU-1010-Tempter of the Bone(深搜+奇偶剪枝)

Tempter of the Bone 翻译: 暑假的时候,小明和朋友去迷宫中寻宝。然而,当他拿到宝贝时,迷宫开始剧烈震动,他感到地面正在下沉,他们意识到这是一个陷阱!他们想尽一切办法逃出去。 迷宫是一个大小为 N*M 的长方形,迷宫中有一扇门。一开始,门是关着的,他会在第 t 秒的时间打开。因为,小明和朋友必须在第 t 秒到大门口。每一秒,他都可以向上下左右四个方向移动一个点。一旦他移动了,他刚...

一直努力 338

HDU 1010 Tempter of the Bone(DFS回溯+剪枝)

HDU 1010 Tempter of the Bone(DFS回溯+剪枝) http://acm.hdu.edu.cn/showproblem.php?pid=1010 题意:给你一个N*M的迷宫和一个时间T,迷宫中为X的格子是不能走的,现在给你起点和终点.问你能不能在正好T秒的时候从起点到达终点,且不能走回头路. 分析:我感觉本题用BFS应该也挺好解决的,不过要注意利用代价函数h()来剪

code 866
上一篇: 迷宫中回溯法的剪枝——奇偶剪枝
下一篇: 第5章 回溯法,n皇后问题
庞老板
博客等级 码龄15年 127粉丝 66原创
评论 6
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

庞老板

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值