/*
BFS,利用set来判重.将状态封装到string大大简化了处理过程
*/
#include <iostream>
#include <string>
#include <queue>
#include <set>
using namespace std;
set<string> hashSets;
struct elem
{
int steps;
string state;
};
queue<elem> bfsq;
//最终状态++表示空格
string finalState = "11121314151617++21222324252627++31323334353637++41424344454647++";
//记录初始状态
string firstState = "";
int minSteps;
//在当前状态中寻找二位数num
int findNumPos(const string &state, const string &num)
{
for(int i = 0; i < 32; i++)
{
if(state[i * 2] == num[0] && state[i * 2 + 1] == num[1])
return i * 2;
}
return -1;
}
//广搜,需要判重
void bfs()
{
hashSets.clear();
hashSets.insert(firstState);
while(!bfsq.empty()) bfsq.pop();
elem curelem, newelem;
curelem.steps = 0;
curelem.state = firstState;
bfsq.push(curelem);
string curstate, newstate, num1, num2;
int p1, p2, cursteps, i;
while(!bfsq.empty())
{
curelem = bfsq.front();
bfsq.pop();
cursteps = curelem.steps;
curstate = curelem.state;
//得到最优解
if(curstate == finalState)
{
minSteps = cursteps;
return;
}
int startPos = 0;
//从四个空格处分别取
for(i = 0; i < 4; i++)
{
p1 = curstate.find_first_of('+', startPos);
num1 = curstate[p1 - 2];
num1 += curstate[p1 - 1];
//当前空格可取的条件是有数字邻居且数字邻居的第二位不是7
if(num1[0] != '+' && num1[1] != '7')
{
num2 = num1;
num2[1]++;
//找到后继的位置
p2 = findNumPos(curstate, num2);
newstate = curstate;
//交换后继与空格
newstate[p1] = curstate[p2];
newstate[p1 + 1] = curstate[p2 + 1];
newstate[p2] = newstate[p2 + 1] = '+';
//判断当前状态是否需要进队列
if(hashSets.find(newstate) == hashSets.end() && cursteps + 1 < minSteps)
{
hashSets.insert(newstate);
newelem.steps = cursteps + 1;
newelem.state = newstate;
bfsq.push(newelem);
}
}
startPos = p1 + 2;
}
}
}
int main()
{
int caseN, i, j;
string temp;
cin>>caseN;
while(caseN--)
{
//初始化
minSteps = INT_MAX;
firstState = finalState;
//将初始状态的四行的初始列置为空格
firstState[0] = firstState[1] = firstState[16] = firstState[17] = firstState[32] = firstState[33] = firstState[48] = firstState[49] = '+';
for(i = 0; i < 4; i++)
{
for(j = 1; j <= 7; j++)
{
cin>>temp;
//如果是尾1二位数,需要放到对应行的首部
if(temp[1] == '1')
{
int pos = (int(temp[0] - '0') - 1) * 16;
firstState[pos] = temp[0];
firstState[pos + 1] = temp[1];
firstState[i * 16 + j * 2] = firstState[i * 16 + j * 2 + 1] = '+';
}
else
{
firstState[i * 16 + j * 2] = temp[0];
firstState[i * 16 + j * 2 + 1] = temp[1];
}
}
}
bfs();
if(minSteps == INT_MAX) cout<<"-1"<<endl;
else cout<<minSteps<<endl;
}
return 0;
}
相关推荐
【论文导读】ICDM2018|SASRec---基于自注意力机制的序列推荐(召回)
前言本次分享2018年发表在ICDM上的论文----「Self-Attentive Sequential Recommendation」。主要是应用self-attention机制来做一...
POJ-2046---Gap (bfs+hash)
Gap Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 1943 Accepted: 889 Description Let's play a card game called Gap. You have 28 cards labeled with two-di
VS2022+Qt6保姆级环境配置指南:从插件安装到项目互通全流程
本文提供VS2022与Qt6深度整合的全流程指南,涵盖环境配置、插件安装、项目互通等关键步骤。详细解析编译器版本匹配、跨IDE协作方案及效能优化技巧,帮助开发者高效搭建C++界面设计开发环境,实现软件项目无缝互通。
POJ 2046 Power Strings(连续重复串重复次数--后缀数组)
连续重复串:如果一个字符串L 是由某个字符串S 重复R 次而得到的,则称L 是一个连续重复串。R 是这个字符串的重复次数。 题意:求连续重复串的最大重复次数R 思路:当然可以用kmp。 用后缀数组的话,穷举字符串S 的长度k,然后判断是否满足。判断的时候,先看字符串L 的长度能否被k 整除,再看suffix(0) suffix(k)的最长公共前缀是否等于n-k。在询问最长公共前缀
湿敏电阻HR202的两种驱动方案实测:IO充放电法 vs. 交流方波ADC法,哪个更适合你?
本文深入评测湿敏电阻HR202的两种驱动方案:IO充放电法和交流方波ADC法。通过原理图分析、精度实测和资源占用对比,揭示交流方波ADC法在精度和稳定性上的优势,特别适合长期监测场景。同时提供IO充放电法在资源受限情况下的应用建议,帮助工程师根据项目需求做出最优选择。
SYSU-MM01数据集评估避坑指南:从RGB/IR图像理解到Python评估脚本调优
本文详细解析了SYSU-MM01数据集在跨模态行人重识别(ReID)评估中的关键技巧与常见陷阱。从RGB/IR图像理解到Python评估脚本优化,提供了数据评估模式选择、single-shot/multi-shot差异分析、特征文件处理规范等实用指南,帮助研究者避开指标异常问题,提升评估效率与准确性。
POJ 2046 Gap BFS+哈希
将4*8的矩阵转成一个64位整数,我是用乘2的方法转成long long的,判断有没有出现过这个状态就用有没有出现这个数字来表示,然后用哈希链地址发解决冲突,x%mod相同的放在一个链表里面,剩下的就是普通的广搜了。 #include #include #include #include using namespace std; typedef __int64 LL; con
软解调详解
在这个示例中,生成了一个随机的比特序列,将其调制为QPSK信号,添加了高斯白噪声,然后计算了每个比特的LLR,并根据LLR的符号进行了解调判决。软解调是一种基于接收信号的概率估计进行解调的方法。与传统的硬解调相比,软解调不仅考虑了接收信号的幅度和相位信息,还利用统计特性计算每个比特为0或1的概率,从而提高了解调性能。软解调过程中涉及的主要公式包括对数似然比(LLR)的计算公式、信号的概率分布函数以及解调判决公式等。软解调的计算处理流程包括信号接收、预处理、相位估计、LLR计算、解调判决和输出等步骤。
poj-2046-循环子串问题
问题题目:[poj-2046]思路这个题目有个结论: 若s存在循环子串,当且仅当,len可以被len - prefix[len-1]整除。连接次数为len / len-prefix[len-1]. 充分性我不会证明,我就证明下必要性吧。 不放假设s = pattern * n; len = strlen(s); 显然prefix[ len - 1 ] = pattern*(n-1),这个是最
视觉Transformer实战 | Vision Transformer(ViT)详解与实现
在计算机视觉领域,卷积神经网络 (CNN) 长期以来一直是处理图像任务的主流架构。然而,随着 Transformer 在自然语言处理领域的巨大成功,研究人员开始探索将这种基于自注意力机制的架构应用于视觉任务。Vision Transformer (ViT) 是这一探索的重要里程碑,它首次证明了纯 Transformer 架构在图像分类任务上可以超越最先进的 CNN 模型。本文将详细介绍 ViT 的技术原理,并使用 PyTorch 从零开始构建 ViT 模型用于图像分类任务。
MATLAB GUI 水果成熟度分级系统设计与实现
本文基于 MATLAB GUI 设计并实现了一款水果成熟度分级系统,能够准确地评估水果的成熟程度。在实现过程中,我们运用了 MATLAB 中的函数实现对图像的处理和分析,同时使用 GUI 界面增加了系统的易用性。值得一提的是,在实际操作中,我们应该结合实际需求对系统进行相应的改进与完善,以满足不同领域和应用场景下的需求。实验结果表明,本系统可以有效地检测出水果的成熟度,并对其进行精准的评分。因此,本文设计并实现了一款基于 MATLAB GUI 的水果成熟度分级系统。三、MATLAB 代码实现。
poj 2046Power Strings
View Code 1 #include<stdio.h> 2 #include<string.h> 3 char str[1000010]; 4 int next[1000010]; 5 int get_next(int x) 6 { 7 int i=0,j=-1; 8 next[0]=-1; 9 whil...
Python计算时间差的方法
使用datetime模块是Python内置的方法,使用timedelta对象可以更精确地计算时间差,而使用dateutil库则提供了更为灵活和方便的操作方式。我们可以使用datetime模块中的datetime类来表示一个具体的日期和时间,并进行相应的运算。在Python中,我们经常需要计算日期和时间之间的差异,例如计算两个日期之间的天数、小时数、分钟数等。Python提供了多种方法来计算时间差,下面我们将介绍其中的几种常用方法,并附上相应的源代码。在上面的代码中,我们使用了dateutil库中的。
poj 2046 Gap
题目连接 http://poj.org/problem?id=2046 Gap Description Let's play a card game called Gap.You have 28 cards labeled with two-digit numbers. The first digit (from 1 to 4) represents the suit of...
jdk-8u144-linux-x64.tar.gz.
该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包
大中型超市收银系统(可直接运行)
光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开
POJ 2046 Gap(BFS+hash判重)
POJ 2046 Gap(BFS+hash判重) http://poj.org/problem?id=2046 题意:4*8的方格有28个数字,按照给定的规则移动,现在要你从初始状态移动到终结状态,问你最少几步. 分析:依然是BFS. 首先我们每个状态要用一个32位的char数组s保存32个方格中的每个数字,然后我们用e[4]数组保存32个方格中(有4个0)4个0的位置分别是
Devexpress16.2.6破解工具
Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具
POJ 2046 Gap 笔记
<span title="Description ">描述 <span title="Let's play a card game called Gap. ">让我们玩一个名为Gap的纸牌游戏。 您有28张卡标有两位数字。<span title="The first digit (from 1 to 4) represents the suit of the card, and the
pscad IEEE标准系统搭建说明
pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。
POJ 2046 Gap 搜索- 状态压缩
题目地址: http://poj.org/problem?id=2046 一道搜索状态压缩的题目,关键是怎样hash。 AC代码: #include #include #include #include #include #include #include #include #include #include #include #include #in
poj 2046 Gap(dfs&hash&壮压)
Gap Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 1595 Accepted: 724 Description Let's play a card game called Gap. You have 28 cards labeled with two-
poj 2046 Gap(bfs+hash)
Description Let's play a card game called Gap. You have 28 cards labeled with two-digit numbers. The first digit (from 1 to 4) represents the suit of the card, and the second digit (from 1...
POJ 2046 Gap 搜索- 状态压缩
题目地址:http://poj.org/problem?id=2046 一道搜索状态压缩的题目,关键是怎样hash。 AC代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cs...
POJ2046 Gap(BFS+hash判重)
Let's play a card game called Gap. You have 28 cards labeled with two-digit numbers. The first digit (from 1 to 4) represents the suit of the card, and the second digit (from 1 to 7) represents the v...
POJ 2046 Gap(bfs+状态压缩+map)
Let's play a card game called Gap. You have 28 cards labeled with two-digit numbers. The first digit (from 1 to 4) represents the suit of the card, and the second digit (from 1 to 7) represents the value of the card. First, you shu2e the cards and lay t
8033



被折叠的 条评论
为什么被折叠?



