Topic Connection
http://poj.org/problem?id=2046
Gapdescription
Let ' s play a card game called Gap.
You have the 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's one card at t He extreme left of each row. The following shows an example of initial layout.
Next, you remove any cards of value 1, and put them in the open space at the end of the rows: "One" to the top row, "2 1 "To the next, and so on.
Now you have the cards and four spaces, the called gaps, the four rows and eight columns. You start moving cards from the This layout.
At each move, you choose one of the four gaps and fill it with the successor of the ' 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 "A" is "a", and "" "has no successor.
In the above layout, you can move "a" to the gap at the right of "a", or "the gap" at the right of "35". If you move "$", 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 have 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
Search + hash weight.
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include < cstdio> #include <vector> #include <queue> #include <set>using std::set;using std::p air;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;cons t 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) {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) 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) * + (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;}
Poj 2046 Gap