本蒟蒻自认为码风还行
#include <bits/stdc++.h>
using namespace std;
const char endp[6][6]={
{'0', '0', '0', '0', '0', '0'},
{'0', '1', '1', '1', '1', '1'},
{'0', '0', '1', '1', '1', '1'},
{'0', '0', '0', '*', '1', '1'},
{'0', '0', '0', '0', '0', '1'},
{'0', '0', '0', '0', '0', '0'}
};
char pan[6][6];
int n=5,ans;
int mx[8] = { 1, 1, 2, 2,-2,-2,-1,-1};
int my[8] = { 2,-2, 1,-1, 1,-1, 2,-2};
inline int jia(){
int cnt = 0;
for (int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
if(pan[i][j] != endp[i][j]){
cnt++;
}
}
}
return cnt;
}
inline bool ok(int x,int y){
if(x<0||x>n)return false;
if(y<0||y>n)return false;
return true;
}
void dfs(int step, int x, int y, int fx, int fy) {
if(step >= ans) return;
int gu = jia();
if(gu + step > 16) return;
if(gu==0){
ans = min(ans, step);
return;
}
for(int i = 0; i < 8; i++){
int gox = x + mx[i], goy = y + my[i];
if(!ok(gox, goy)) continue;
if(fx != gox || fy != gox){
swap(pan[x][y], pan[gox][goy]);
dfs(step + 1, gox, goy, x , y);
swap(pan[x][y], pan[gox][goy]);
}
}
}
int main() {
int wx, wy, t;
string bline;
cin >> t;
while(t--){
for (int i = 1; i <= n; i++) {
cin >> bline;
for(int j = 1; j <= n; j++) {
pan[i][j] = bline[j - 1];
if(pan[i][j] == '*') {
wx = i;wy = j;
}
}
}
ans=100;
dfs(0, wx, wy, 0, 0);
if(ans == 100){
cout << "-1\n";
}
else{
cout << ans << "\n";
}
}
return 0;
}
本蒟蒻第一次做蓝题,错的可能比较多,体谅下呗OvO
囧rz