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; }
|
近期评论