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 the card, and the second digit (from 1 to 7) represents the value of the card. 

First, you shu2e the cards and lay them face up on the table in four rows of seven cards, leaving a space of one card at the extreme left of each row. The following shows an example of initial layout. 


Next, you remove all cards of value 1, and put them in the open space at the left end of the rows: "11" to the top row, "21" to the next, and so on. 

Now you have 28 cards and four spaces, called gaps, in four rows and eight columns. You start moving cards from this layout. 


At each move, you choose one of the four gaps and fill it with the successor of the left neighbor of the gap. The successor of a card is the next card in the same suit, when it exists. For instance the successor of "42" is "43", and "27" has no successor. 

In the above layout, you can move "43" to the gap at the right of "42", or "36" to the gap at the right of "35". If you move "43", a new gap is generated to the right of "16". You cannot move any card to the right of a card of value 7, nor to the right of a gap. 

The goal of the game is, by choosing clever moves, to make four ascending sequences of the same suit, as follows. 


Your task is to find the minimum number of moves to reach the goal layout.

Input

The input starts with a line containing the number of initial layouts that follow. 

Each layout consists of five lines - a blank line and four lines which represent initial layouts of four rows. Each row has seven two-digit numbers which correspond to the cards. 

Output

For each initial layout, produce a line with the minimum number of moves to reach the goal layout. Note that this number should not include the initial four moves of the cards of value 1. If there is no move sequence from the initial layout to the goal layout, produce "-1".

Sample Input

4

12 13 14 15 16 17 21
22 23 24 25 26 27 31
32 33 34 35 36 37 41
42 43 44 45 46 47 11

26 31 13 44 21 24 42
17 45 23 25 41 36 11
46 34 14 12 37 32 47
16 43 27 35 22 33 15

17 12 16 13 15 14 11
27 22 26 23 25 24 21
37 32 36 33 35 34 31
47 42 46 43 45 44 41

27 14 22 35 32 46 33
13 17 36 24 44 21 15
43 16 45 47 23 11 26
25 37 41 34 42 12 31

Sample Output

0
33
60
-1

爆搜+哈希判重。。

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<set>
using std::set;
using std::pair;
using std::swap;
using std::queue;
using std::vector;
using std::multiset;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 1000007;
const int MOD = 100000007;
const int INF = 0x3f3f3f3f;
typedef long long ll;
struct Node {
    int s;
    int mat[4][8];
    inline bool operator==(const Node &x) const {
        rep(i, 4) {
            rep(j, 8) {
                if(mat[i][j] != x.mat[i][j]) return false;
            }
        }
        return true;
    }
    inline int hash() const {
        ll ret = 0, k = 1;
        rep(i, 4) {
            rep(j, 8) {
                ret = (ret + k * mat[i][j]) % MOD;
                k <<= 1;
            }
        }
        return (int)ret;
    }
    inline void read() {
        rep(i, 4) {
            mat[i][0] = 0;
            rep(j, 7) {
                scanf("%d", &mat[i][j + 1]);
            }
        }
        rep(i, 4) {
            rep(j, 8) {
                int v = mat[i][j];
                if(1 == v % 10) {
                    swap(mat[i][j], mat[v / 10 -1][0]);
                }
            }
        }
    }
}start, goal;
struct Hash_Set {
    int tot, A[N], head[N], next[N];
    inline void init() {
        tot = 0, cls(head, -1);
    }
    inline bool insert(const int val) {
        int u = val % N;
        for(int i = head[u]; ~i; i = next[i]) {
            if(A[i] == val) return false;
        }
        A[tot] = val, next[tot] = head[u]; head[u] = tot++;
        return true;
    }
}hash;
void bfs() {
    hash.init();
    queue<Node> q;
    q.push(start);
    hash.insert(start.hash());
    while(!q.empty()) {
        Node x = q.front(); q.pop();
        rep(i, 4) {
            rep(j, 8) {
                if(x.mat[i][j]) continue;
                Node t = x;
                int x1 = 0, y1 = 0, val = x.mat[i][j - 1] + 1;
                if(1 == val || 8 == val % 10) continue;
                rep(k, 4) {
                    rep(l, 8) {
                        if(val == x.mat[k][l]) {
                            x1 = k, y1 = l; k = 4;
                            break;
                        }
                    }
                }
                t.s = x.s + 1;
                swap(t.mat[i][j], t.mat[x1][y1]);
                if(t == goal) {
                    printf("%d\n", t.s);
                    return;
                }
                val = t.hash();
                if(!hash.insert(val)) continue;
                q.push(t);
            }
        }
    }
    puts("-1");
}
void solve() {
    start.read();
    rep(i, 4) {
        rep(j, 7) {
            goal.mat[i][j] = (i + 1) * 10 + (j + 1);
        }
        goal.mat[i][7] = 0;
    }
    if(start == goal) {
        puts("0");
        return;
    }
    bfs();
}
int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w+", stdout);
#endif
    int t;
    scanf("%d", &t);
    while(t--) {
        solve();
    }
    return 0;
}

转载于:https://www.cnblogs.com/GadyPu/p/4773283.html

一种新型多通道锁相放大器的设计 随着科学技术的发展,微弱信号的检测越来越重要, 现已逐渐形成一专门的边缘学科。锁相放大器(Lock-in Amplifier, 简写为LIA)就是检测淹没在噪声中的微弱信号的仪器。它可用于测量微弱交流信号的幅度和位相, 有较强的抑制干扰和噪声的能力, 有极高的灵敏度,在光谱学和环境学的微弱信号探测与采集中有着广泛的应用前景。 立即下载

相关推荐

避开SM30维护视图的坑:自动更新字段+日志记录的5个关键配置步骤

本文深入解析了SAP ABAP开发中SM30维护视图的深度定制,聚焦于实现自动更新字段与日志记录功能。文章详细阐述了从SE54配置起点到核心编码的5个关键步骤,重点讲解了如何利用事件驱动模型(如05创建事件和01保存前事件)安全操作动态数据结构,并提供了避免常见陷阱的实用技巧,帮助开发者构建稳定可靠的自动化维护机制。

plant的博客 623

利用锁相环生成正交参考信号

记得在几年前我还在读书时,第一次用到了锁相放大器。那时就觉得锁相放大器设计的巧妙,一直想能够自己DIY一台来玩玩。中间也有过些自己的设计想法,但总觉得不够理想,因此也就一直没有真正动手。其中如何利用输入的外部参考信号生成一对正交的内部参考信号就是其中难点之一。对此我一直都没有很好的解决方案。 最近在看远坂俊昭写的《测量电子电路设计——模拟篇》,书中刚好给出了一个利用锁相环的解决方案。仔细研究起来

Ivan 的专栏 1万+

开源浏览器插件开发实战:从零搭建多平台内容分发工具

浏览器扩展(Browser Extension)是运行在浏览器上层、通过标准Web技术增强浏览器能力的轻量级程序,其核心价值在于对页面内容的读取、操作与跨应用信息流转。在内容创作与分发场景中,创作者频繁需要在公众号、知乎、优快云、头条等平台间搬运同一篇文章,而传统SaaS分发工具往往受限于免费额度、授权托管和平台适配滞后等问题。基于Manifest V3规范,开发者可以构建一款100%开源、永久免费的浏览器插件,利用content script完成从源文章页提取标题、正文与图片,通过消息通信机制将结构化数

weixin_29882269的博客 295

【电赛信号题必备】基于STM32F4的全数字锁相放大器实现

本文提出了一种基于STM32F407+FPU的全数字双通道锁相放大器方案,解决了传统模拟方案外围电路复杂、相位测量不精确等问题。该方案采用协方差数学修正算法实现零相移,相位差测量精度达0.04°。通过32位相位累加器、CCM RAM查表和线性插值算法提高信号纯度,利用双重ADC和DMA乒乓缓存实现双通道同步。经MATLAB仿真和硬件测试验证,该方案在强噪声环境下仍能稳定提取信号相位,且运算效率高、CPU占用率低。项目提供完整源码和文档支持,适用于电赛仪器、光电检测等领域。

2401_89025946的博客 2474

CoppeliaSim+matlab+simulink七轴机械臂仿真

七轴机械臂vrep+matlab+simulink 仿真

a1233219的博客 3548

Pixhawk+OpenMV实战:如何用Apriltag实现无人机自动降落(附避坑指南)

本文详细介绍了如何利用Pixhawk飞控和OpenMV摄像头,通过Apriltag视觉标签实现无人机的精准降落系统。从硬件搭建、参数配置到视觉处理代码优化,提供了完整的实战指南和避坑建议,帮助开发者快速实现厘米级精度的自动降落功能。

weixin_27076351的博客 481

poj 2046

<br /> GapTime Limit: 5000MS Memory Limit: 65536KTotal Submissions: 1286 Accepted: 592<br />DescriptionLet's play a card game called Gap. <br />You have 28 cards labeled with two-digit numbers. The first digit (from 1 to 4) represents the suit of the card,

志当存高远 1492

手把手教你学Simulink——基于通信协议与标准的场景实例:卫星通信中的网络安全协议(IPSec)仿真

随着卫星通信在国防、金融、应急指挥、能源管网等关键领域的广泛应用,其安全性已成为系统设计的核心要求。然而,卫星链路具有广播特性、长距离传输、地面站分布广等特点,极易遭受窃听、篡改、重放、中间人攻击等网络威胁。传统的“信任链路层安全”已不足以应对现代网络攻击。为此,国际标准组织(如IETF、ETSI、CCSDS)推荐在卫星通信中部署协议,实现端到端的数据机密性、完整性、身份认证和抗重放保护。本文将手把手教你如何在中构建一个符合标准的仿真系统,涵盖从明文传输到加密隧道的完整安全链路建模。本文完整实现了。

MHD0815的博客 149

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的位置分别是

code 1923

手势识别器:实现手部动作识别器

在本篇文章中,我们将使用Python来实现一个简单的手部动作识别器。然后,我们通过循环从摄像头获取每一帧图像,并对每一帧图像进行处理。我们提取手部关键点并计算手势的特征向量,然后将其添加到特征列表和标签列表中。接下来,我们再次从摄像头获取每一帧图像,并进行手势识别。我们提取手部关键点并计算手势的特征向量,然后使用训练好的分类器来预测手势,并在图像上显示出来。在这个实现中,我们将使用MediaPipe库来检测和跟踪手部关键点,然后使用K最近邻(K-Nearest Neighbors)算法对手势进行分类。

BitSlinger的博客 238

AutoSAR系列讲解 - 资料下载

学习资料下载汇总

雪云飞星的博客 4万+

POJ 2046 Gap

/*BFS,利用set来判重.将状态封装到string大大简化了处理过程*/#include #include #include #include using namespace std;set hashSets;struct elem{    int steps;    string state;};queue bfsq;

bobten2008的专栏 1469

高效与灵活相结合:Verilog编写的跨时钟域循环缓冲控制器解析

循环缓冲区,也被称作循环队列、环形缓冲区,是一种数据结构,其主要特性是使用一个固定大小的缓冲区作为数据的存储空间,这个缓冲区在逻辑上被视作是首尾相接的。虽然Verilog是一种常用的硬件描述语言,被广泛应用在FPGA和ASIC设计中,但是在描述循环缓冲区时,它并没有提供一种直接且灵活的方式来指定循环缓冲区的参数,如字长、缓冲区深度、缓冲区数量等。这给硬件设计带来了一定的不便。同时,该控制器的设计也充分考虑了硬件资源的利用率,通过优化的算法和数据结构,使得在实现高性能的同时,也能够有效地节省硬件资源。

qq_38334677的博客 1221

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

woniupengpeng的专栏 453

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

POJ 2046 Gap 搜索- 状态压缩

题目地址: http://poj.org/problem?id=2046 一道搜索状态压缩的题目,关键是怎样hash。 AC代码: #include #include #include #include #include #include #include #include #include #include #include #include #in

lamborghini1993 1498

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

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-

记录成长的点点滴滴. . . . . 1828

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

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

dielucuan8830的博客 109

POJ 2046 Gap BFS+哈希

将4*8的矩阵转成一个64位整数,我是用乘2的方法转成long long的,判断有没有出现过这个状态就用有没有出现这个数字来表示,然后用哈希链地址发解决冲突,x%mod相同的放在一个链表里面,剩下的就是普通的广搜了。 #include #include #include #include using namespace std; typedef __int64 LL; con

嗯。 449

POJ 2046 Gap 搜索- 状态压缩

题目地址:http://poj.org/problem?id=2046 一道搜索状态压缩的题目,关键是怎样hash。 AC代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cs...

weixin_30689307的博客 74

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

qq_37751662的博客 284

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

wust_cyh 459

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

HJY 548
上一篇: poj 1985 Cow Marathon
下一篇: poj 2367 Genealogical tree
dong12276
博客等级 码龄13年 0粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值