题目:

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. 

输入:

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. 

输出:

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

样例:

分析:题意是把任一直接移到空格处为一步;

BFS加hash判重

简单来说hash就是把一个状态用一个数来标识,能一一对应最好(比方说康托展开),这样你就可以方便地知道这个状态有没有走过

  1 #include<iostream>
2 #include<sstream>
3 #include<cstdio>
4 #include<cstdlib>
5 #include<string>
6 #include<cstring>
7 #include<algorithm>
8 #include<functional>
9 #include<iomanip>
10 #include<numeric>
11 #include<cmath>
12 #include<queue>
13 #include<vector>
14 #include<set>
15 #include<cctype>
16 #define PI acos(-1.0)
17 const int INF = 0x3f3f3f3f;
18 const int NINF = -INF - 1;
19 typedef long long ll;
20 #define MOD 1000007
21 using namespace std;
22 ll Hash[MOD];
23 struct node
24 {
25 int maze[4][8];
26 int step;//记录步数
27 friend bool operator == (node a, node b)//重载结构体“==”
28 {
29 for (int i = 0; i < 4; ++i)
30 {
31 for (int j = 0; j < 8; ++j)
32 if (a.maze[i][j] != b.maze[i][j]) return false;
33 }
34 return true;
35 }
36 ll gethash()//获取hash值
37 {
38 ll value = 0;
39 for (int i = 0; i < 4; ++i)
40 {
41 for (int j = 0; j < 8; ++j)
42 value += (value<<ll(1)) + (ll)maze[i][j];//随意写
43 }
44 return value;
45 }
46 }st, ed;//起始与结束状态
47 bool verif(ll value)//hash判重函数
48 {
49 int num = value % MOD;//对大质数取余
50 while (Hash[num] != 0 && Hash[num] != value)//处理可能存在的hash冲突
51 {
52 num += 3;//随意写
53 num %= MOD;
54 }
55 if (Hash[num] == 0)
56 {
57 Hash[num] = value;
58 return true;
59 }
60 return false;
61 }
62 void bfs()
63 {
64 queue<node> q;
65 memset(Hash, 0, sizeof(Hash));
66 st.step = 0;
67 q.push(st);
68 verif(st.gethash());
69 while (q.size())
70 {
71 node tmp = q.front();
72 q.pop();
73 for (int i = 0; i < 4; ++i)
74 {
75 for (int j = 0; j < 8; ++j)
76 {
77 if (!tmp.maze[i][j])//寻找空格位置
78 {
79 node cur = tmp;
80 cur.step++;
81 int aim = tmp.maze[i][j - 1] + 1;
82 if (aim == 1 || aim == 8) continue;//空格前为空格或7则跳过
83 int x, y, flag = 0;
84 for (int m = 0; m < 4; ++m)
85 {
86 for (int n = 0; n < 8; ++n)
87 {
88 if (tmp.maze[m][n] == aim)
89 {
90 x = m, y = n;
91 flag = 1;
92 }
93 }
94 }
95 if (flag)
96 {
97 swap(cur.maze[x][y], cur.maze[i][j]);
98 ll value = cur.gethash();
99 //cout << value << endl;
100 if (verif(value))
101 {
102 if (cur == ed)
103 {
104 cout << cur.step << endl;
105 return;
106 }
107 q.push(cur);
108 }
109 }
110 }
111 }
112 }
113 }
114 cout << -1 << endl;
115 }
116 int main()
117 {
118 int T;
119 cin >> T;
120 for (int i = 0; i < 4; ++i)//结束时状态
121 {
122 ed.maze[i][7] = 0;
123 for (int j = 0; j < 7; ++j)
124 ed.maze[i][j] = (i + 1) * 10 + j + 1;
125 }
126 while (T--)
127 {
128 string nul;
129 getline(cin, nul);
130 for (int i = 0; i < 4; ++i)//直接输入时就解决个位数为1的移至行首操作
131 {
132 st.maze[i][0] = (i + 1) * 10 + 1;
133 for (int j = 1; j < 8; ++j)
134 {
135 cin >> st.maze[i][j];
136 if (st.maze[i][j] % 10 == 1) st.maze[i][j] = 0;
137 }
138 }
139 if (st == ed) cout << 0 << endl;
140 else bfs();
141 }
142 return 0;
143 }

HDU1067 Gap的更多相关文章

  1. DG gap sequence修复一例

    环境:Oracle 11.2.0.4 DG 故障现象: 客户在备库告警日志中发现GAP sequence提示信息: Mon Nov 21 09:53:29 2016 Media Recovery Wa ...

  2. 16 On Large-Batch Training for Deep Learning: Generalization Gap and Sharp Minima 1609.04836v1

    Nitish Shirish Keskar, Dheevatsa Mudigere, Jorge Nocedal, Mikhail Smelyanskiy, Ping Tak Peter Tang N ...

  3. 利用增量备份恢复因归档丢失造成的DG gap

    故障现象:data guard归档出现gap,悲剧的是丢失的归档在主库上被rman备份时删除了,丢失的归档大约有20几个,数据库大小约2T,如果重建DG将非常耗时间,因此决定利用增量备份的方式恢复DG ...

  4. (一)GATT Profile和GAP 简介(目前所有的BLE应用都基于GATT,所以也要了解是怎么一回事)-转发

    个人大总结:(先后顺序) 1.GAP协议定义多个角色(其中就有中心设备[GATT客户端](唯一)叫主设备||和外围设备[GATT服务端端](多个)也叫从设备). 2.先经过GAP协议,再有GATT协议 ...

  5. hdu.1067.Gap(bfs+hash)

    Gap Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  6. 【leetcode】Maximum Gap

    Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...

  7. 【leetcode】Maximum Gap(hard)★

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  8. Datagard產生gap

    本文轉載自無雙的小寶的博客:http://www.cnblogs.com/sopost/archive/2010/09/11/2190085.html 有時候因為網路或備份故障等原因,主機所產生的歸檔 ...

  9. [LintCode] Maximum Gap 求最大间距

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

随机推荐

  1. poj1066 线段相交简单应用(解题报告)

    #include<stdio.h> #include<math.h> const double eps=1e-8; int n; struct Point { double x ...

  2. Codeforces Round #666 (Div. 2) Power Sequence、Multiples of Length 思维

    题目链接:Power Sequence 题意: 给你n个数vi,你可以对这个序列进行两种操作 1.可以改变其中任意个vi的位置,无成本 2.可以对vi进行加1或减1,每次操作成本为1 如果操作之后的v ...

  3. Codeforces Round #651 (Div. 2) A Maximum GCD、B GCD Compression、C Number Game、D Odd-Even Subsequence

    A. Maximum GCD 题意: t组输入,然后输入一个n,让你在区间[1,n]之间找出来两个不相等的数a,b.求出来gcd(a,b)(也就是a,b最大公约数).让你求出来最大的gcd(a,b)是 ...

  4. HDU 1173 思路题

    题目大意 有n个地点(坐标为实数)需要挖矿,让选择一个地点,使得在这个地方建造基地,到n个地点的距离和最短,输出基地的坐标. 题解+代码: 1 /* 2 把这个二维分开看(即把所有点投影到x轴上,再把 ...

  5. Python——requests模块

    一.安装模块 pip install requests 二.引用 import requests 三.get方法 #GET访问页面 r = requests.get(url) print(r.text ...

  6. LVS+Keepalived深度理解,阐述你不知道的坑点

    1. LVS简介 1. 什么是LVS? LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统.本项目在1998年5月由章文嵩博士成立,是中国国内 ...

  7. DNS域名解析10步

    第一步:浏览器缓存中检查有没有对应这个域名的解析过的IP地址,如有,结束 第二步:如浏览器缓存没有查到,则访问本地操作系统,window下通过C:\Windows\System32\drivers\e ...

  8. apache 解析漏洞(CVE-2017-15715)

    在p牛博客最近更新的文章,传送门,感觉很有意思,自己在自己本地测试了一下 0x01 正则表达式中的 '$' apache这次解析漏洞的根本原因就是这个 $,正则表达式中,我们都知道$用来匹配字符串结尾 ...

  9. 【哈希表】leetcode454——四数相加II

    编号454:四数相加II 给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0. 为 ...

  10. SQL优化这么做就对了

    目录 前言 SQL优化一般步骤 1.通过慢查日志等定位那些执行效率较低的SQL语句 2.explain 分析SQL的执行计划 3.show profile 分析 4.trace 5.确定问题并采用相应 ...