-
Notifications
You must be signed in to change notification settings - Fork 10
/
CHESSCBG.cpp
103 lines (84 loc) 路 1.89 KB
/
CHESSCBG.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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef unsigned long long ull;
#define X first
#define Y second
#define pb push_back
#define mp make_pair
#define ep emplace_back
#define EL printf("\n")
#define sz(A) (int) A.size()
#define FOR(i,l,r) for (int i=l;i<=r;i++)
#define FOD(i,r,l) for (int i=r;i>=l;i--)
#define fillchar(a,x) memset(a, x, sizeof (a))
#define faster ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
struct data {
int a[4][4];
};
const int N = 70000;
int dd[N];
data a;
queue<data> st;
int getCode(data a) {
int ans = 0;
FOR(i,0,3) FOR(j,0,3) ans = ans * 2 + a.a[i][j];
return ans;
}
data read() {
data a;
FOR(i,0,3) {
string s;
getline(cin, s);
FOR(j,0,3) a.a[i][j] = s[j] - '0';
}
return a;
}
void push(data a, int ord) {
st.push(a);
dd[getCode(a)] = ord;
}
void up(int i, int j, data a, int ord) {
if (i == 0 || a.a[i-1][j] == 1) return ;
swap(a.a[i][j], a.a[i-1][j]);
if (!dd[getCode(a)]) push(a, ord);
}
void down(int i, int j, data a, int ord) {
if (i == 3 || a.a[i+1][j] == 1) return ;
swap(a.a[i][j], a.a[i+1][j]);
if (!dd[getCode(a)]) push(a, ord);
}
void left(int i, int j, data a, int ord) {
if (j == 0 || a.a[i][j-1] == 1) return ;
swap(a.a[i][j], a.a[i][j-1]);
if (!dd[getCode(a)]) push(a, ord);
}
void right(int i, int j, data a, int ord) {
if (j == 3 || a.a[i][j+1] == 1) return ;
swap(a.a[i][j], a.a[i][j+1]);
if (!dd[getCode(a)]) push(a, ord);
}
int main() {
// freopen("INP.TXT", "r", stdin);
// freopen("OUT.TXT", "w", stdout);
a = read();
push(a, 1);
a = read();
while (! st.empty()) {
data x = st.front();
st.pop();
if (getCode(x) == getCode(a)) break;
int ord = dd[getCode(x)] + 1;
FOR(i,0,3)
FOR(j,0,3)
if (x.a[i][j] == 1) {
up(i,j,x,ord);
down(i,j,x,ord);
left(i,j,x,ord);
right(i,j,x,ord);
}
}
printf("%d\n", dd[getCode(a)] - 1);
return 0;
}