-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path12445 - Happy 12.cpp
143 lines (131 loc) · 3.15 KB
/
12445 - Happy 12.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=3876
//#tag hash map
//#tag bfs busca em largura de dois niveis
//#sol
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#include <vector>
#include <queue>
#include <list>
#include <stack>
#include <map>
// #include <unordered_map>
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define pdd pair<double,double>
#define F first
#define S second
#define fr(i,j,k) for(int (i)=(j);(i)<(k);++(i))
#define rep(i,n) for(int (i)=0;(i)<(n);++(i))
#define pb push_back
#define PI acos(-1)
#define db(x) cerr << #x << " = " << x << endl;
#define _ << ", " <<
#define mp make_pair
#define cl(x) memset(x,0,sizeof(x))
using namespace std;
ll prime = 4200187, prime2 = 3214567;
int hash(ll in){
int ret = (int) (in % prime);
return ret;
}
template<class K, class V> struct HashMap {
vector<pair<K, V> > tab;
vector<char> used;
HashMap(int maxsize) : tab(maxsize), used(maxsize, 0) {}
V *lookup(const K &key, bool insert) {
for (int i = hash(key) % tab.size();;) {
if (used[i]) {
if (tab[i].first == key) return &tab[i].second;
} else {
if (!insert) return NULL;
used[i] = 1;
tab[i].first = key;
return &tab[i].second;
}
if (++i == tab.size()) i = 0;
}
}
V &operator[](const K &key) { return *lookup(key, true); }
bool contains(const K &key) { return lookup(key, false) != NULL; }
};
HashMap<ll, pii> mapa(4200187);
// map<ll, pii> mapa;
// unordered_map<ll, pii> mapa;
int passo=0,n,m,t;
ll target = 0;
ll pot[13];
bool seq;
ll mov[] = { 2,3,4,5,6,12,7,8,9,10,11,1,
12,1,2,3,4,5,7,8,9,10,11,6,
1,2,3,4,5,7,8,9,10,11,12,6,
1,2,3,4,5,12,6,7,8,9,10,11,
2,3,4,5,6,7,8,9,10,11,12,1,
12,1,2,3,4,5,6,7,8,9,10,11 };
int state = -1;
int bfs(ll init){
queue< pair<ll,int> > fila;
fila.push( pair<ll,int>(init,0) );
pair<ll,int> x;
int ret = -1;
while(!fila.empty()){
x = fila.front(); fila.pop();
if( x.F == target && !seq){
ret = x.S; break;
}
if( mapa[x.F].F == passo ) continue;
else if( seq && mapa[x.F].F == (passo-1)) {
ret = (x.S + mapa[x.F].S); break;
}
mapa[x.F] = pii(passo,x.S);
if( x.S < 9){
fr(i,0,6){
ll aux = x.F, next = 15;
fr(j,0,12){
next += (aux % (1<<4)) * mov[(i*12)+j];
aux >>= 4;
}
fila.push( mp(next, (x.S + 1)));
}
}
}
return ret;
}
int main(){
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
cin >> t;
ll cur,a,b; pot[0] =1;
fr(i,1,12) pot[i] = pot[i-1] << 4;
for(int i=11; i>=0;i--) {
fr(j,0,6){
mov[(j*12)+i] = pot[mov[(j*12)+i]-1];
}
target += (i+1) * pot[i];
}
while(t--){
passo++; cur = 0; seq = false;
fr(i,0,12){
cin >> a;
cur += a * pot[i];
}
// cout << cur << " <<>> " << target << endl;
ll aux = cur;
int ret = bfs(cur);
if( ret == -1) {
passo++;
seq = true;
ret = bfs(target);
}
if( ret == -1) ret = 19;
printf("%d\n", ret);
}
return 0;
}