hdu 6171 Admiral 双向bfs+hash

CFturbo 2024保姆级教程:从流量扬程到三维叶轮,手把手搞定离心泵水力设计 本文提供CFturbo 2024离心泵水力设计的全流程实战指南,从流量扬程参数输入到三维叶轮模型导出,详细解析设计步骤与优化技巧。涵盖叶轮尺寸确定、子午面型线设计、叶片角度调整等关键环节,帮助工程师高效完成CAE辅助的离心泵水力设计,提升设计质量与效率。 阅读详情


Admiral

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 153428/153428 K (Java/Others)
Total Submission(s): 206    Accepted Submission(s): 54


Problem Description
Suppose that you are an admiral of a famous naval troop. Our naval forces have got 21 battleships. There are 6 types of battleships.
First, we have got one flagship in which the admiral must be and it is denoted by number 0. Others are denoted by number from 1 to 5, each of them has 2, 3, 4, 5, 6 ships of its kind. So, we have got 21 battleships in total and we must take a giant battle against the enemy. Hence, the correct strategy of how to arrange each type of battleships is very important to us.
The shape of the battlefield is like the picture that is shown below.
To simplify the problem, we consider all battleships have the same rectangular shape.

Fortunately, we have already known the optimal state of battleships.
As you can see, the battlefield consists of 6 rows. And we have 6 types of battleship, so the optimal state is that all the battleships denoted by number i are located at the i-th row. Hence, each type of battleship corresponds to different color.
You are given the initial state of battlefield as input. You can change the state of battlefield by changing the position of flagship with adjacent battleship.
Two battleships are considered adjacent if and only if they are not in the same row and share parts of their edges. For example, if we denote the cell which is at i-th row and j-th position from the left as (i,j), then the cell (2,1) is adjacent to the cells (1,0), (1,1), (3,1), (3,2).
Your task is to change the position of the battleships minimum times so as to reach the optimal state.
Note: All the coordinates are 0-base indexed.
 

Input
The first line of input contains an integer T (1 <= T <= 10), the number of test cases. 
Each test case consists of 6 lines. The i-th line of each test case contains i integers, denoting the type of battleships at i-th row of battlefield, from left to right.
 

Output
For each test case, if you can’t reach the goal in no more than 20 moves, you must output “too difficult” in one line. Otherwise, you must output the answer in one line.
 

Sample Input
  
1 1 2 0 2 1 2 3 3 3 3 4 4 4 4 4 5 5 5 5 5 5
 

Sample Output
  
3
 

Source
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   6181  6180  6179  6178  6177 
 

Statistic |  Submit |  Discuss |  Note
题意:给你一个塔,要你弄成第一行全是0的,第二行全是1,第三行全是2,以此类推,求最小步数,每一步只能把0移动到上方、左上方、下方、右下方。

思路:现场码了半天还是得10多s才能出答案。看了题解,双向bfs。。。感觉我又被套路了。在知道起点和终点的情况下,两边同时bfs能剪掉n多的枝,这个想一下还是想得通的。所以这题就把图hash成字符串(当然你也可以hash成数,6进制不会爆LL,只不过可能写起来不好写),然后两边同时bfs其中一方找到另一方时结束。可能会觉得我的代码风格有点奇怪,因为。。有一些是我叫队友码的,留接口给我用。下面给代码:

#pragma comment(linker, "/STACK:102400000,102400000") 
#include<iostream>  
#include<cmath>  
#include<queue>  
#include<cstdio>  
#include<queue>  
#include<algorithm>  
#include<cstring>  
#include<string>  
#include<utility>
#include<set>
#include<map>
#include<stack>
#include<vector>
#define maxn 200005
#define inf  0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long LL;
const double eps = 1e-5;
const int mod = 1e9+7;
int up[] = { -1, 0, -1, 1, 2, -1, 3, 4, 5, -1, 6, 7, 8, 9, -1, 10, 11, 12, 13, 14, -1 };
int upleft[] = { -1, -1, 0, -1, 1, 2, -1, 3, 4, 5, -1, 6, 7, 8, 9, -1, 10, 11, 12, 13, 14 };
int down[] = { 1, 3, 4, 6, 7, 8, 10, 11, 12, 13, 15, 16, 17, 18, 19, -1, -1, -1, -1, -1, -1 };
int downright[] = { 2, 4, 5, 7, 8, 9, 11, 12, 13, 14, 16, 17, 18, 19, 20, -1, -1, -1, -1, -1, -1 };
map<string, int>m;
string ori = "011222333344444555555";
queue<string>q;
int solve(string next,string now,int nextpos,int pos){
	if (~nextpos){
		swap(next[pos], next[nextpos]);
		next[21] = nextpos / 10 + '0';
		next[22] = nextpos % 10 + '0';
		int cnt1 = m[next];
		int cnt2 = m[now];
		if (!cnt1){
			m[next] = m[now] + 1;
			q.push(next);
		}
		else if (cnt2 >= 20 && cnt1 <= 11 || (cnt2 <= 11 && cnt1 >= 20))
			return cnt1 + cnt2 - 20;
		swap(next[pos], next[nextpos]);
	}
	return 0;
}
int bfs(string x){
	while (!q.empty())
		q.pop();
	m.clear();
	int index;
	for (int i = 0; i < 21;i++)
		if (x[i] == '0'){
			index = i;
			break;
		}
	char s[5];
	s[0] = index / 10 + '0';
	s[1] = index % 10 + '0';
	s[2] = '\0';
	q.push(x += s);
	q.push("01122233334444455555500");
	m[x] = 20;
	m["01122233334444455555500"] = 1;
	while (!q.empty()){
		string now = q.front();
		q.pop();
		int num = m[now];
		if (num == 11 || num == 30)
			continue;
		int pos = (now[21] - '0') * 10 + now[22] - '0';
		int nextpos = up[pos];
		string next = now;
		int ans = solve(next, now, nextpos, pos);
		if (ans)
			return ans;
		nextpos = upleft[pos];
		ans = solve(next, now, nextpos, pos);
		if (ans)
			return ans;
		nextpos = down[pos];
		ans = solve(next, now, nextpos, pos);
		if (ans)
			return ans;
		nextpos = downright[pos];
		ans = solve(next, now, nextpos, pos);
		if (ans)
			return ans;
	}
	return -1;
}
string input()
{
	int cnt = 21;
	string s = "", t;
	while (cnt--) cin >> t, s += t;
	return s;
}
int main(){
	int t;
	scanf("%d", &t);
	while (t--){
		string now = input();
		if (now == ori){
			printf("0\n");
			continue;
		}
		int ans = bfs(now);
		if (~ans)
			printf("%d\n", ans);
		else
			printf("too difficult\n");
	}
}


单细胞分析避坑指南:SCTransform和Harmony参数设置详解 本文深入解析了单细胞分析中SCTransform标准化与Harmony批次校正的关键参数设置。针对10x Genomics等数据,详细探讨了如何通过调整`vars.to.regress`、`theta`等核心参数,在有效去除技术噪音的同时,避免过度校正以保留关键的生物学差异,从而提升分析结果的可靠性与可解释性。 阅读详情

相关推荐

DRM驱动架构浅析-上(DRM基础概要与U-Boot阶段驱动解析)

本文主要探讨了Rockchip平台的DRM显示驱动架构及其在U-Boot阶段的实现。

一路寒风身如絮,命海沉浮客独行 1765

hdu 6171

http://acm.hdu.edu.cn/showproblem.php?pid=6171 题意:有一个塔,这个塔一共有6层,第i-1层有i个数字,数字为i-1。 现在给你一个塔,要你恢复成原来的那样,但是你每次只能移动0这个数字,可移动的方向为左上,上,下,右下。 然后问你多少步可以到达 思路:如果简单的dfs的话,那么肯定会超时,那么我们考虑双向dfs,也就是正向反向都跑b...

A1823085974的博客 245

uniapp原生安卓插件开发之路

声明: 我的功能需求:全局悬浮框,也就是在其他应用上依旧能悬浮。查遍uni文档,无法实现,甚至native.js也无法实现,无奈只能走上原生插件开发这条不归路了。(吐槽:天天写前端js,突然写了下android简直要炸,public static String啥的,var不香嘛,不过别人严谨肯定还是有别人道理的,加油吧,争取能入个门!) 本人不懂安卓开发,完全安卓小白,所以安卓方面可能表达的不是很好,安卓代码可能写的不优美,但我功能最终还是实现了。但是为了需求,能力总能被“逼”出来。有描述错误的还望指正

Jone's Area 1万+

HDU 6171 Admiral (DFS + 剪枝)

题意: 给出原始阵列 0 1 1 2 2 2 3 3 3 3 4 4 4 4 4 5 5 5 5 5 5 问对于给出的某种阵列,能否通过每步只移动 0 至(i - 1 , j)or ( i - 1 , j - 1)  or ( i + 1 ,  j ) or ( i + 1 , j  + 1 ) ,在20步之内移动到原始阵列。 思路: 考虑到只要求20步,直接搜索即可。 但

DT2131 638

告别Visual Studio!用MSYS2+GCC+CMake在Windows上打造Linux式C++开发环境(保姆级避坑)

本文详细介绍了如何在Windows上使用MSYS2+GCC+CMake搭建Linux式C++开发环境,替代Visual Studio。从MSYS2的安装配置、MinGW-w64 GCC工具链的部署,到CMake项目的创建与构建,提供了保姆级教程和常见问题解决方案,帮助开发者高效过渡到命令行开发模式。

weixin_30298497的博客 325

【数字图像处理第四版课后习题答案】第2章 数字图像基础(含英文原版)

如图 P2.3 所示,视网膜图像中与点相对应的直径 x 是由类似的三角形得到的。即得出 x = 0.085d。根据第 2.1 节中的讨论,并结合一些自由解释,我们可以将眼窝视为一个方形传感器阵列,拥有大约 337,000 个元素,这相当于一个大约 580×580 个元素的阵列。假设元素间距相等,则在 1.5 毫米长的直线上有 580 个元素和 579 个空间,共计 1,159 个。换句话说,如果一个点的直径 d<1.3×10-6 m,或者 d<15.3×10 m,眼睛就无法检测到这个点。

weixin_56463218的博客 1万+

HDU 6171 Admiral(双向BFS+队列)题解

思路:最大步骤有20,直接BFS会超时。因为知道开始情况和结果所以可以用双向BFS,每个BFS规定最大步骤为10,这样相加肯定小于20。这里要保存每个状态搜索到的最小步骤,用Hash储存。当发现现有状态已经在另一路出现了,那么就输出两者相加的步骤和。代码:#include&lt;iostream&gt; #include&lt;algorithm&gt; #include&lt;queue&gt;...

努力,奋斗 366

机器学习预测股票收益(一)之随机森林模型

机器学习预测股票收益(一)前言一、导入库和数据二、处理数据以及计算特征变量三、使用随机森林回归预测股票收益1.构建训练集和测试集2.查看预测结果四、根据预测结果构建long-short投资组合1.定义投资组合函数2.定义绩效分析函数五、引入Fama-French Factor六、投资组合表现1.超额收益表现2.多因子回归总结 前言 本文将使用Python整理1927-2020年所有美国上市公司股票数据。根据历史收益以及交易量,使用随机森林,支持向量机以及神经网络等机器学习方法预测股票收益。最优结果构建的资产

weixin_47174968的博客 1万+

Admiral HDU - 6171

这题很像那道八数码的题目,可以把21个位置每个位置有6种数,那么总共有21的6次方种状态,那么是可以用一个longlong来表示状态,用set来判重的。但是这样还是不够快。我们可以利用A星算法来优化搜索过程A星算法的知识,A星算法中有两个重要的值,已走步数和到终点估计值,已走步数这个很容易得到,到终点的估计值这个我们可以估计每个不合法的点直接挪过去他该去的那一行该走的步数。估价值h(n)<=

qq965194745的博客 357

BEVFormer与TensorRT的完美结合:如何实现4倍推理加速?

本文详细解析了如何通过TensorRT的量化技术和自定义插件实现BEVFormer模型的4倍推理加速,同时保持精度损失小于1%。文章从模型架构与部署挑战入手,深入探讨了计算图优化、自定义插件开发和量化校准策略,并提供了完整的部署流程和性能调优技巧,为自动驾驶领域的实时3D感知提供了高效解决方案。

bean的博客 221

基于Simulink的间接功率控制的矩阵变换器

通过上述步骤,我们成功设计并实现了基于 Simulink 的间接功率控制的矩阵变换器仿真模型。该模型能够通过控制输入电流和输出电压,实现对功率的间接控制,适用于电机驱动、可再生能源系统、电力传输等领域。通过虚拟场景仿真、参数优化和性能评估,验证了系统的稳定性和高效性,并通过调整 PI 控制器参数和优化调制算法,进一步提升了系统的性能。

MHD0815的博客 296

hdu 6171 ida*

题意:一个数塔,类似八数码的样子。。 思路:正解双向搜索。。时间复杂度非常科学。。不过数据弱ida*搞了过去。。。。(大力出奇迹?) 代码; #include using namespace std; #define X first #define Y second #define PB push_back #define MP make_pair #define MEM(x,y) mems

zxl博客 438

【温室气体数据集】CAMS 发布的温室气体(GHG)数据集总结

【温室气体数据集】CAMS 发布的温室气体(GHG)数据集总结

WW、forever的博客 555

【多校训练】hdu 6171 Admiral 双向bfs+hash

Problem Description Suppose that you are an admiral of a famous naval troop. Our naval forces have got 21 battleships. There are 6 types of battleships. First, we have got one flagship in which th

弱菜zc 600

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

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

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

HDU 6171 Admiral [双向bfs+hash]

HDU 6171 Admiral [双向bfs+hash]

ACTerminate的博客 537

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

HDU 6171 Admiral(hash+双向bfs)

Description 给出一个六层的地图,从上到下第iii层有iii个块,以(i,j)(i,j)(i,j)表示第i+1i+1i+1层从左到右第j+1j+1j+1个块的坐标,一个块可以和与其相邻的块交换,两个块相邻当且仅当其某维坐标相同,第iii层块编号均为i−1i−1i-1,现在给出一个地图初始状态,要把编号为000的块移动到(0,0)(0,0)(0,0)处,问最少步数 Input 第一行...

ZSQ 384

WINCC7.3破解文件

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

Hdu 6171 Admiral【双向Bfs+字符串哈希】

Admiral Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 153428/153428 K (Java/Others) Total Submission(s): 203    Accepted Submission(s): 52 Problem Description Suppose that you are an

mengxiang000000 981

2017多校联合第十场/HDU 6171 Admiral 双向bfs + 哈希

Admiral Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 153428/153428 K (Java/Others) Total Submission(s): 334    Accepted Submission(s): 105 Problem Description Suppose that you a

The_star_is_at的博客 858

HDU 6171 Admiral 双向搜索(meet in the middle) + 哈希

传送门:HDU6171 题意:给你一个高度为6的数塔,问最少多少步能走成如下状态。 每次只能将值为0的点与其左上方,上方,下方,右下方四个方向的一个点进行交换。  0  1 1  2 2 2  3 3 3 3  4 4 4 4 4  5 5 5 5 5 5 大于20步可以直接输出“too difficult” 思路:比赛的时候智障的想了一个多少小时的A* 和 IDA* ,还

WA是一笔财富 972

2017多校第10场 HDU 6171 Admiral 双向BFS或者A*搜索

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6171 题意: 给你一个高度为6的塔形数组,你每次只能将0与他上下相邻的某个数交换,问最少交换多少次可以变为初始状态,若需要的步数大于20,直接输出too difficult,初始状态为: 0 1 1 2 2 2 3 3 3 3 4 4 4 4 4 5 5 5 5 5 ...

aishangg437965的博客 134

HDU 6171 Admiral

HDU 6171 Admiral搜索,双向宽搜题意自己读题吧。不过有两点注意:每次只能把0数字和其他数字交换;每个数字的可交换范围是左上,正上,正下,右下。思路不会搜索啊。。。%%% 因为起始状态与终止状态都很明确,考虑双向宽搜。关于状态的表示与检查:作为6进制数哈希,用个map记录状态与达到该状态对应的步数。因为是双向搜索,所以开两个map记录。双向宽搜就是从初始状态搜,记录到map1;从终止状

P.H.S 479

2017 Multi-University Training Contest 10 1001 Admiral HDU 6171 (双向搜索 哈希)

题意: 给你一个高度为6的塔形数组,你每次只能将0与他上下相邻的某个数交换,问最少交换多少次可以变为初始状态,若需要的步数大于20,直接输出too difficult,初始状态为: 0  1 1  2 2 2  3 3 3 3  4 4 4 4 4  5 5 5 5 5 5 思路:因为最多走20步,当存搜索时间复杂度4^20, 所以我们可以分别从当前状态和初始状态进行双向

CillyB的博客 615

【双向bfs】2017多校训练十 HDU 6171 Admiral

【题意】 现在给出一个三角矩阵,如果0编号的在点(x,y)的话,可以和(x+1,y),(x-1,y),(x+1,y+1),(x-1,y-1)这些点进行交换。 我们每一次只能对0点和其他点进行交换。问最少步数,使得最终变成: 0 1 1 2 2 2 3 3 3 3 4 4 4 4 4 5 5 5 5 5 5 【思路】 【AC】 1 #include<...

weixin_30466039的博客 77

hdu6171 Admiral 双向宽搜

Admiral Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 153428/153428 K (Java/Others) Total Submission(s): 82    Accepted Submission(s): 19 Problem Description Suppose that you are

abutoto的博客 461

hdu6171 双向bfs

传送门 题意:给一个金字型塔的数列,第一行一个0,第二行两个1,。。。第6行6个5,现在金字塔数字打乱了,你只能移动0这个数字,只能向左上右上左下右下走,问在20步内能否回到原来状态。 一共21个数,每个有6种状态,没办法用一个 [21][6]维的数组记录状态,所以我们要用Hash记录状态,每个位置6种状态,那么最大是6^21的数字,long long可以存的下的。我们用bfs进行搜索,给两个起

猿的进化 560

hdu6171 双向搜索+hash

Suppose that you are an admiral of a famous naval troop. Our naval forces have got 21 battleships. There are 6 types of battleships. First, we have got one flagship in which the admiral must be and i

扬帆起航 675
上一篇: hdu 6166 Senior Pan 最短路
下一篇: 高斯消元模板
overload1997
博客等级 码龄11年 328粉丝 210原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值