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
| #include<bits/stdc++.h> using namespace std; const int n = 5; string final = "111110111100*110000100000"; int dx[] = {1, 1, 2, 2, -2, -2, -1, -1}; int dy[] = {2, -2, 1, -1, 1, -1, 2, -2}; int dif(string s){ int res = 0; for(int i = 0; i < n*n; ++i){ if(s[i] != final[i]) res++; } return res; } string s; int lim; bool suc = 0; void dfs(int cur, int last){ if(cur == lim){ if(dif(s) == 0){ suc = 1; } return; } if(suc) return; if(cur + max(0, dif(s) - 1) > lim) return; int f = s.find('*'); int x = f/n, y = f%n; int xx, yy; for(int i = 0; i < 8; ++i){ if(i + last == 7) continue; xx = x + dx[i]; yy = y + dy[i]; if(xx >= 0 && xx < n && yy >= 0 && yy < n){ swap(s[xx*n + yy], s[x*n + y]); dfs(cur+1, i); swap(s[xx*n + yy], s[x*n + y]); } } } int main(){ int T; cin >> T; while(T--){ s = ""; string tmp; for(int i = 1; i <= n; ++i){ cin >> tmp; s += tmp; } if(dif(s) == 0) cout << 0 << endl; else{ suc = 0; for(int i = 1; i <= 15; ++i){ lim = i; dfs(0, 9); if(suc){ cout << i << endl; break; } } if(!suc) cout << -1 << endl; } } return 0; }
|