hysbz-1085

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
using namespace std;
const int kDx[] = {-2, 2, -1, 1, -2, 2, -1, 1};
const int kDy[] = {1, 1, 2, 2, -1, -1, -2, -2};
char kBoard[6][6];
const char kAns[6][6] = {"11111",
"01111",
"00*11",
"00001",
"00000"};
int kT, kRes;
int (){
int res = 0;
for(int i = 0; i < 5; ++i){
for(int j = 0; j < 5; ++j){
if(kBoard[i][j] != kAns[i][j]){
++res;
}
}
}
return res;
}
bool InBoard(int x, int y){
if(x < 0 || x >=5 || y < 0 || y >= 5){
return false;
}
return true;
}
void Input(){
for(int i = 0; i < 5; ++i)
scanf("%s", kBoard[i]);
kRes = 0;
for(int i = 0; i < 5; ++i){
for(int j = 0; j < 5; ++j){
if(kBoard[i][j] != kAns[i][j])
kRes += 1;
}
}
}
void GetXY(int& x, int& y){
x = y = -1;
for(int i = 0; i < 5; ++i){
for(int j = 0; j < 5; ++j){
if(kBoard[i][j] == '*'){
x = i, y = j;
break;
}
}
if(x != -1)break;
}
}
bool Dfs(int depth){
int h = kH();
if(h == 0) return true;
if(h - 1 + depth <= kRes){
int x, y;
GetXY(x, y);
for(int i = 0; i < 8; ++i){
int nx = x + kDx[i];
int ny = y + kDy[i];
if(InBoard(nx, ny)){
swap(kBoard[x][y], kBoard[nx][ny]);
if(Dfs(depth + 1))
return true;
swap(kBoard[x][y], kBoard[nx][ny]);
}
}
}
return false;
}
int main(){
scanf("%d", &kT);
while(kT--){
Input();
kRes -= 1;
while(!Dfs(0) && kRes <= 15)
++kRes;
if(kRes <= 15)
printf("%dn", kRes);
else
puts("-1");
}
return 0;
}