hdu5025 Saving Tang Monk BFS

STM32CUBEMX 使用教程5 — DMA配置 & 串口结合DMA实现数据搬运 DMA(Direct Memory Access),直接存储器访问(或数据搬运),是STM32中很实用的一个外设。DMA 传输数据方式不需要 CPU 参与控制传输,也不用中断处理的方式进行数据搬运处理,纯粹通过硬件为 RAM 与 I/O 设备之间开辟一条直接传送数据的通路,使 CPU 的效率大大提高。 阅读详情

Saving Tang Monk

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2360    Accepted Submission(s): 848


Problem Description
《Journey to the West》(also 《Monkey》) is one of the Four Great Classical Novels of Chinese literature. It was written by Wu Cheng'en during the Ming Dynasty. In this novel, Monkey King Sun Wukong, pig Zhu Bajie and Sha Wujing, escorted Tang Monk to India to get sacred Buddhism texts. 

During the journey, Tang Monk was often captured by demons. Most of demons wanted to eat Tang Monk to achieve immortality, but some female demons just wanted to marry him because he was handsome. So, fighting demons and saving Monk Tang is the major job for Sun Wukong to do.

Once, Tang Monk was captured by the demon White Bones. White Bones lived in a palace and she cuffed Tang Monk in a room. Sun Wukong managed to get into the palace. But to rescue Tang Monk, Sun Wukong might need to get some keys and kill some snakes in his way.

The palace can be described as a matrix of characters. Each character stands for a room. In the matrix, 'K' represents the original position of Sun Wukong, 'T' represents the location of Tang Monk and 'S' stands for a room with a snake in it. Please note that there are only one 'K' and one 'T', and at most five snakes in the palace. And, '.' means a clear room as well '#' means a deadly room which Sun Wukong couldn't get in.

There may be some keys of different kinds scattered in the rooms, but there is at most one key in one room. There are at most 9 kinds of keys. A room with a key in it is represented by a digit(from '1' to '9'). For example, '1' means a room with a first kind key, '2' means a room with a second kind key, '3' means a room with a third kind key... etc. To save Tang Monk, Sun Wukong must get ALL kinds of keys(in other words, at least one key for each kind).

For each step, Sun Wukong could move to the adjacent rooms(except deadly rooms) in 4 directions(north, west, south and east), and each step took him one minute. If he entered a room in which a living snake stayed, he must kill the snake. Killing a snake also took one minute. If Sun Wukong entered a room where there is a key of kind N, Sun would get that key if and only if he had already got keys of kind 1,kind 2 ... and kind N-1. In other words, Sun Wukong must get a key of kind N before he could get a key of kind N+1 (N>=1). If Sun Wukong got all keys he needed and entered the room in which Tang Monk was cuffed, the rescue mission is completed. If Sun Wukong didn't get enough keys, he still could pass through Tang Monk's room. Since Sun Wukong was a impatient monkey, he wanted to save Tang Monk as quickly as possible. Please figure out the minimum time Sun Wukong needed to rescue Tang Monk.
 

Input
There are several test cases.

For each case, the first line includes two integers N and M(0 < N <= 100, 0<=M<=9), meaning that the palace is a N×N matrix and Sun Wukong needed M kinds of keys(kind 1, kind 2, ... kind M). 

Then the N × N matrix follows.

The input ends with N = 0 and M = 0.
 

Output
For each test case, print the minimum time (in minutes) Sun Wukong needed to save Tang Monk. If it's impossible for Sun Wukong to complete the mission, print "impossible"(no quotes).
 

Sample Input
3 1 K.S ##1 1#T 3 1 K#T .S# 1#. 3 2 K#T .S. 21. 0 0
 

Sample Output
5 impossible 8



广搜,主要是标记怎么打。

用一个bool vis[105][105][15][40]; //<x, y, key, snake>

表示经过点(x, y)并且这时候收集了最大的key,并且将蛇杀死了哪几条

将每条蛇编上号,snake可以用二进制表示,利用1左移多少位来表示第几条蛇


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

using namespace std;

struct node {
	int x, y;
	int t, key, snake;
	node(int x, int y, int t, int key, int snake) : x(x), y(y), t(t), key(key), snake(snake) {}
	node() {}
};

const int INF = 0x7fffffff;

int n, m, sx, sy;
int dir[4][2] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
char mapp[105][105];
bool vis[105][105][15][40]; //<x, y, key, snake>

bool check(int x, int y) {
	if (x < 0 || x >= n || y < 0 || y >= n || mapp[x][y] == '#')
		return false;
	return true;
}

void bfs() {
	queue<node> Q;
	memset(vis, false, sizeof(vis));
	Q.push(node(sx, sy, 0, 0, 0));
	vis[sx][sy][0][0] = true;
	int ans = INF;
	while (!Q.empty()) {
		node tn = Q.front(); Q.pop();
		if (mapp[tn.x][tn.y] == 'T' && tn.key == m) ans = min(ans, tn.t);
		for (int i = 0; i < 4; i++) {
			int nx, ny, t, key, snake;
			nx = tn.x + dir[i][0], ny = tn.y + dir[i][1];
			t = tn.t; snake = tn.snake; key = tn.key;
			if (check(nx, ny)) {
				if ('A' <= mapp[nx][ny] && mapp[nx][ny] <= 'E') {
					int id = mapp[nx][ny] - 'A';
					if (((1 << id) & snake) == 0) {
						snake |= (1 << id);
						t++;
					}
				}
				else if(mapp[nx][ny] >= '0' && mapp[nx][ny] <= '9') {
					int num = mapp[nx][ny] - '0';
					if (num == key + 1) {
						key++;
					}
				}
				if (!vis[nx][ny][key][snake]) {
					vis[nx][ny][key][snake] = true;
					Q.push(node(nx, ny, t + 1, key, snake));
				}
			}
		}
	}
	if (ans == INF) {
		puts("impossible");
	}
	else {
		printf("%d\n", ans);
	}
}

int main()
{
	while (~scanf("%d%d", &n, &m) && (n || m)) {
		int cnt = 0;
		for (int i = 0; i < n; i++) {
			scanf("%s", mapp[i]);
			for (int j = 0; j < n; j++) {
				if (mapp[i][j] == 'K') {
					sx = i; sy = j;
				}
				if (mapp[i][j] == 'S') {
					mapp[i][j] = 'A' + cnt;
					cnt++;
				}
			}
		}
		bfs();
	}
	return 0;
}



手把手教你用VSCode+SDL在Ubuntu上跑LVGL8.2模拟器(附插件清单) 本文详细介绍了在Ubuntu系统上使用VSCode和SDL库搭建LVGL8.2模拟器环境的完整流程,特别适合嵌入式开发者(如全志F1C100s平台)进行UI设计验证。从环境配置、项目编译到VSCode深度集成,提供了实用技巧和常见问题解决方案,帮助开发者实现'先模拟后真机'的高效开发模式。 阅读详情

相关推荐

Chapter 4 Ripple-Based Control Technique Part I

本文介绍了DC-DC转换器的纹波控制技术,重点分析了迟滞控制、恒定开通时间控制、恒定关断时间控制等方案的工作原理及优缺点。纹波控制无需补偿网络,具有快速瞬态响应和结构简单的优势,特别适合电池供电应用。其中,恒定开通时间控制在轻载时能自动延长关断时间,实现伪定频工作;而恒定关断时间控制轻载效率较低。文章还讨论了纹波注入技术解决Boost转换器右半平面零点问题,并比较了不同控制方案在负载瞬态下的性能表现。最后指出基于纹波的控制技术在提高调节精度和降低EMI方面的改进方向。

2304_76736886的博客 1317

HDU 5025 Saving Tang Monk(bfs+状压) 【openjudge】

Saving Tang Monk 问题分析 本题就是属于拿钥匙去开门的bfsbfsbfs类题目。 题意:孙悟空必须拿到nnn个钥匙才能救出唐僧,但是期间如果遇到蛇怪需要多花一分钟去打败她们,并且在拿到第iii个钥匙之前必须先拿到第i−1i−1i-1个钥匙,问最少的花费时间。 这题就麻烦在三个地方, 一、同种钥匙可能有多个,拿完钥匙的地方仍然可以走,所以需要加一维钥匙的状态来判重。 ...

Eternally831143的博客 836

中兴问天 BE7200MAX SFP 光口用华为猫棒替代光猫完整配置教程

PPPoE 账号密码:若原光猫为 Route 模式,在宽带设置页可见用户名,密码需按 F12 打开浏览器开发者模式,点击密码输入框,查看代码中 “value=” 后的内容即为明文密码。若仍失败,核对 PON 类型是否与运营商网络匹配。PON 认证信息:LOID(逻辑 ID,部分地区含密码)、设备 SN 码(标签或设备信息页)、PON 类型(GPON/EPON,运营商网络类型)。电脑保持与路由器连接,访问路由器管理地址(默认通常为 192.168.5.1,可参考路由器说明书),登录后台。

Gulper_Eel的博客 3511

Save__TangMonk之状态压缩

Saving Tang Monk 总时间限制: 1000ms 内存限制: 65536kB 描述 《Journey to theWest》(also 《Monkey》) is one of the Four Great Classical Novelsof Chinese literature. It was writtenby Wu Cheng’en during th

慕宜的博客 898

Upload Symbols Failed:The archive did not include a dSYM for the LiveKitWebRTC.framework with the UU

你需要从源码构建 LiveKitWebRTC,而不是使用 CocoaPods 安装的预编译 .xcframework。Step 2: 修改 Podfile 使用本地路径引用(不是从源下载的 xcframework)方法 :强制从源码构建 LiveKitWebRTC.framework(推荐)这样它就会编译 WebRTC 并带上正确 UUID 的 dSYM。替换路径为你 clone 下来的 LiveKit 源码位置。Step 1: 克隆源码仓库。

baidu_40537062的博客 2157

微信小程序协同工作和发布

微信小程序协同工作和发布

qq_53114527的博客 1627

英语题目翻译——OJ_ 8436:Saving Tang Monk

题目:8436:Saving Tang Monk(OpenJudge - 8436:Saving Tang Monk) 翻译: 《西游记》(又称《美猴王》)是中国文学四大名著之一。它是吴承恩在明朝时写的。在这部小说中,孙悟空、猪八戒和沙悟净陪同唐僧去印度取经。 途中,唐僧常被妖怪捉住。大多数妖怪为了长生不老想要吃唐僧,但有些女妖怪只是因为唐僧英俊而想嫁给他。所以,孙悟空的主要工作是保护唐僧,帮助唐僧打败妖怪。 有一次,唐僧被白骨精抓走了。白骨精住在宫殿里,她把唐僧铐在房间里。孙悟空设法进入了宫殿。但

m0_64882565的博客 539

生成式引擎优化(GEO)的概念与技术路径

生成式引擎优化(GEO)是针对生成式AI引擎的新型优化策略,旨在提升内容在AI搜索与问答平台中的可见性和可信度。与传统SEO相比,GEO更注重语义理解、知识图谱和多模态生成等技术,通过优化内容的结构化和平台适配性,使品牌信息在生成式AI中更易被采纳和呈现。其核心技术包括语义理解、向量数据库和实时反馈优化等,应用于品牌传播、智能客服和个性化推荐等领域。随着AI技术发展,GEO将向智能化、自动化方向演进,成为内容优化的重要工具。

Baixue5209的博客 1056

hdu 5025 Saving Tang Monk (bfs+状态压缩)

Saving Tang Monk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 41    Accepted Submission(s): 10 Problem Description 《Journey to t

Coding A World 2387

【全开源】教育培训微信小程序源码(FastAdmin+ThinkPHP+Unipp)

教育培训微信小程序源码以其便捷性、互动性、安全性等优势,成为了教育培训行业的新宠。随着移动互联网技术的不断发展,教育培训微信小程序源码将继续引领未来学习趋势,为学员提供更加优质、高效的学习体验。同时,教育培训机构也应积极拥抱小程序技术,利用源码打造具有自身特色的学习平台,提升品牌影响力和市场竞争力。

2401_84414018的博客 2115

Halcon实战:5分钟搞定自定义直线卡尺工具(附完整代码下载)

本文详细介绍了如何使用Halcon快速开发自定义直线卡尺工具,通过交互式ROI绘制、边缘检测和直线拟合三个核心函数,实现高效工业视觉检测。文章包含完整代码下载,帮助开发者在5分钟内掌握这一实用工具的开发技巧,提升直线检测的精度和效率。

perl8的博客 265

NOI openjudge 8436:Saving Tang Monk

8436:Saving Tang Monk 描述 《Journey to theWest》(also 《Monkey》) is one of the Four Great Classical Novelsof Chinese literature. It was writtenby Wu Cheng'en during the Ming Dynasty. In this n

mzyupengju的博客 1429

【Java基础知识 1】Java入门级概述

CSDN最强Java专栏,包含全部Java基础知识点、Java8新特性、Java集合、Java多线程、Java代码实例,理论结合实战,实现Java的轻松学习。

学Java,找哪吒 16万+

hdu5025 Saving Tang Monk 状压bfs

传送门:hdu5025 题意:悟空要去救师傅,必须按顺序拿到规定数目的钥匙,遇到蛇(S)要多花一秒钟,问最短时间。 自己不不知道怎么判定蛇死没死,看题解发现新大陆,用一个数组来进行状态压缩,book[i][j][k][s]标记i,j点收集钥匙的状态(k)和杀死蛇的状态(s),其中最后一维最为神奇,因为一共最多五条蛇,所以给蛇编号以后用五位二进制数来保存五条蛇的状态,1表示已经被杀死了,0表示尚

WA是一笔财富 316

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

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

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

HDU 5025 Saving Tang Monk BFS搜索

题目描述:Description 《Journey to the West》(also 《Monkey》) is one of the Four Great Classical Novels of Chinese literature. It was written by Wu Cheng’en during the Ming Dynasty. In this novel, Monkey King

喵头鹰的博客 445

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

hdu-5025 Saving Tang Monk (BFS + 状态压缩)

该是比较简单的BFS的 。    难点在于多了两个限制条件 : 必须按照顺序捡拾钥匙 ; 杀死蛇消耗2单位时间,蛇只会被杀死一次 。 因为在拿到第n把钥匙前必须拿到第n-1把,所以只需要加一维大小是11的维度来维护当前拿到的钥匙情况就可以完整的表示所有状态了 。 但是还有蛇的条件,所以不妨在结构体里加一个正数,将其压缩成一个集合表示第i只蛇是不是被杀死了 。  所以事先对所有的蛇进行编号就行了

AC_Arthur的专栏 1081

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

hdu 5025 Saving Tang Monk(bfs)

题目连接:hdu 5025 Saving Tang Monk 题目大意:给定一张图,K表示孙悟空的位置,T表示师傅的位置,S表示蛇精,悟空要救师傅的话,需要M把钥匙,走到蛇精的位置需要用3秒的时间把蛇干掉。 解题思路:bfs,因为蛇只有5只,所以dp[x][y][k][s]表示移动到x,y,收集k把钥匙,已经打死了s的蛇(二进制) #include #include #inclu

不慌不忙、不急不躁 723

HDU5025--Saving Tang Monk(BFS)

题目大意:孙悟空去救唐僧,要集齐m把钥匙才能救他,要得到第二把钥匙就必须先拿到第一把,以此类推。路上还有至多五条蛇,必须杀死蛇才能继续前进,每次杀死一条蛇,消耗一个单位的时间。另外,每前行一格,也要耗费一个单位时间。求最短时间。 分析:带限制的BFS。一般做这种题,就是增加vis数组的维数,使其能够正确表示当前的状态。这里需要记录的有是两个坐标,还有当前钥匙的情况,和杀死哪些蛇的情况。所

Apple.Daily的专栏 460

HDU 5025Saving Tang Monk BFS + 二进制枚举状态

3A的题目,第一次TLE,是因为一次BFS起点到终点状态太多爆掉了时间。 第二次WA,是因为没有枚举蛇的状态。 解体思路: 因为蛇的数目是小于5只的,那就首先枚举是否杀死每只蛇即可。 然后多次BFS,先从起点到第一把钥匙,不能往回走,要用VIS数组标记。 第二次从第一把钥匙走到第二把钥匙。 最后一次从最后一把钥匙走到终点即可。 Tips 1: 在每次BFS过程中使用优先队...

weixin_30577801的博客 184

HDU 5025 Saving Tang Monk 记忆化BFS+状态压缩

Problem Description 《Journey to the West》(also 《Monkey》) is one of the Four Great Classical Novels of Chinese literature. It was written by Wu Cheng'en during the Ming Dynasty. In this novel, Monkey

旧时光 1224

hdu5025 Saving Tang Monk bfs+状态压缩

//开一个四维数组,前面两维表示在图中的位置 //后面两维表示到达该位置有多少个钥匙和经过的路线有多少条蛇 //由于时间和步数不同步,所以得用优先队列来做 //或者遍历所有情况得到最大值 //由于钥匙需要按照顺序找,所以能直接记录最后一个钥匙的大小 //而对于经过蛇不是特定的顺序,所以对于蛇的记录可以运用一个二进制数表示 #include #include #include #i

ijbuhv的专栏 660

hdu 5025Saving Tang Monk(BFS)

点击打开链接 题意:从K走到T,S为怪,走的时候就多花费一秒,走到T时收集m把不同的钥匙,但是规定收集n之前,必须1~n-1全部收集完毕,怪最多有5个 思路:怪最多就有5个,然后钥匙是1~9把,我们每个点的状态就不会很多,在BFS时每个点的状态进行标记就行了,5个怪状态压缩着判断,因为这个怪在第二次经过的时候已经死了,不用花费时间去杀死它 // // main.cp

The_star_is_at的博客 375

HDU 5025 Saving Tang Monk bfs

Saving Tang Monk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 616    Accepted Submission(s): 229 Problem Description 《Journey to

Clearlemon 616

Saving Tang Monk HDU - 5025 (状压+bfs)

《Journey to the West》(also 《Monkey》) is one of the Four Great Classical Novels of Chinese literature. It was written by Wu Cheng’en during the Ming Dynasty. In this novel, Monkey King Sun Wukong, pig Z

退役的铜牌狗 363

HDU-#5025 Saving Tang Monk(BFS+状态压缩)

题目大意:给出一个迷宫,其中起点为K,终点为T,而S为蛇,第一次经过会多消耗1个时间来杀死蛇,还有一些数字的钥匙,必须依次收集,即拿了钥匙1,才能拿钥匙2.问最低消费时间为多少步?        解题思路:求最短的时间的花费,很明显是BFS来求解。但是由于要判断钥匙以及蛇的状态,这里采用状态压缩来进行状态的存储。code给出了详细的注释,理解了状态压缩,题就很简单了。详见长code。    

VoidSun 539

HDU 5025 Saving Tang Monk (复杂BFS)

Saving Tang Monk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 48    Accepted Submission(s): 16 Problem Description 《Journey to

Tc的专栏 2394
上一篇: hdu5000 Clone dp
下一篇: hdu5047 Sawtooth 规律 + JAVA大数
Daemoonn
博客等级 码龄11年 42粉丝 198原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址