#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <set>
using namespace std;
struct node
{
char sta[5][5];
int x, y;
int path;
} now, temp;
int move[8][2] = {{2, 1}, {2, -1}, {-2, 1}, {-2, -1}, {1, 2}, {1, -2}, {-1, 2}, {-1, -2}};
struct cmp
{
bool operator() (node a, node b) const
{
return memcmp(a.sta, b.sta, 25) < 0;
}
};
set<node,cmp> vis;
queue<node>que;
char aim[5][5] =
{
{'1', '1', '1', '1', '1'},
{'0', '1', '1', '1', '1'},
{'0', '0', ' ', '1', '1'},
{'0', '0', '0', '0', '1'},
{'0', '0', '0', '0', '0'}
};
void bfs( )
{
que.push(now);
vis.insert(now);
if(memcmp(now.sta, aim, sizeof(aim)) == 0)
{
printf("Solvable in 0 move(s).\n");
return ;
}
while(!que.empty())
{
now = que.front();
que.pop();
if(now.path > 10)
{
printf("Unsolvable in less than 11 move(s).\n");
return ;
}
if(memcmp(now.sta, aim, sizeof(aim)) == 0)
{
printf("Solvable in %d move(s).\n",now.path);
return ;
}
for( int i = 0; i < 8; i++)
{
temp = now;
temp.x += move[i][0];
temp.y += move[i][1];
temp.path++;
if(temp.x >=0 && temp.x < 5 && temp.y >= 0 && temp.y < 5)
{
char tt;
tt = temp.sta[temp.x][temp.y];
temp.sta[temp.x][temp.y] = temp.sta[now.x][now.y];
temp.sta[now.x][now.y] = tt;
if(!vis.count(temp))
{
vis.insert(temp);
que.push(temp);
}
}
}
}
}
void init()
{
vis.clear();
while(!que.empty())
que.pop();
}
int main()
{
int t, sx, sy;
scanf("%d",&t);
while(t--)
{
init();
for( int i = 0; i < 5; i++)
{
getchar();
for( int j = 0; j < 5; j++)
{
scanf("%c",&now.sta[i][j]);
if(now.sta[i][j] == ' ')
{
now.x = i;
now.y = j;
now.path = 0;
}
}
}
bfs();
}
}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <set>
using namespace std;
struct node
{
char sta[5][5];
int x, y;
int path;
} now, temp;
int move[8][2] = {{2, 1}, {2, -1}, {-2, 1}, {-2, -1}, {1, 2}, {1, -2}, {-1, 2}, {-1, -2}};
struct cmp
{
bool operator() (node a, node b) const
{
return memcmp(a.sta, b.sta, 25) < 0;
}
};
set<node,cmp> vis;
queue<node>que;
char aim[5][5] =
{
{'1', '1', '1', '1', '1'},
{'0', '1', '1', '1', '1'},
{'0', '0', ' ', '1', '1'},
{'0', '0', '0', '0', '1'},
{'0', '0', '0', '0', '0'}
};
void bfs( )
{
que.push(now);
vis.insert(now);
if(memcmp(now.sta, aim, sizeof(aim)) == 0)
{
printf("Solvable in 0 move(s).\n");
return ;
}
while(!que.empty())
{
now = que.front();
que.pop();
if(now.path > 10)
{
printf("Unsolvable in less than 11 move(s).\n");
return ;
}
if(memcmp(now.sta, aim, sizeof(aim)) == 0)
{
printf("Solvable in %d move(s).\n",now.path);
return ;
}
for( int i = 0; i < 8; i++)
{
temp = now;
temp.x += move[i][0];
temp.y += move[i][1];
temp.path++;
if(temp.x >=0 && temp.x < 5 && temp.y >= 0 && temp.y < 5)
{
char tt;
tt = temp.sta[temp.x][temp.y];
temp.sta[temp.x][temp.y] = temp.sta[now.x][now.y];
temp.sta[now.x][now.y] = tt;
if(!vis.count(temp))
{
vis.insert(temp);
que.push(temp);
}
}
}
}
}
void init()
{
vis.clear();
while(!que.empty())
que.pop();
}
int main()
{
int t, sx, sy;
scanf("%d",&t);
while(t--)
{
init();
for( int i = 0; i < 5; i++)
{
getchar();
for( int j = 0; j < 5; j++)
{
scanf("%c",&now.sta[i][j]);
if(now.sta[i][j] == ' ')
{
now.x = i;
now.y = j;
now.path = 0;
}
}
}
bfs();
}
}