#pragma comment (linker, "/STACK:64000000");
#include<iostream>
#include<string>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
const int N=3;
int a[N][N], t, b[N*N];
int X,Y;
string M="";
map<string,bool> map1;
vector<string> answers;
bool chek(int b[N][N])
{
int k=1;
int c=0;
if(b[N-1][N-1]!=0)return false;
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
if(b[i][j]==k)
c++;
k++;
}
}
if(c==N*N-1)return true;
return false;
}
void findZero(int a[N][N])
{
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
if(a[i][j] == 0){
X=i;Y=j;
}
}
}
}
void solve(int a[N][N], string s)
{
string x="";
/* for(int i=0;i<N;i++){
for(int j=0;j<N;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
system("pause");*/
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
x=x+char(a[i][j]+'0');
int c[N][N];
if(chek(a)==1)
{
if(M=="")M=s;
if(s.length()<M.length())M=s;
cout << "Jol = " << s << endl;
system("pause");
}
else if(map1[x]==0)
{
map1[x]=1;
findZero(a);
int l=s.length();
l--;
//i-1,j
if(X-1 >= 0){
for(int i1=0;i1<N;i1++)
for(int j1=0;j1<N;j1++)
c[i1][j1] = a[i1][j1];
swap(c[X][Y],c[X-1][Y]);
solve(c, s+char(c[X][Y]+'0'));
}
findZero(a);
x="";
//i+1,j
if(X+1 < N && s[l]!=char(a[X+1][Y]+'0')){
for(int i1=0;i1<N;i1++)
for(int j1=0;j1<N;j1++)
c[i1][j1] = a[i1][j1];
swap(c[X][Y],c[X+1][Y]);
solve(c, s+char(c[X][Y]+'0'));
}
//i,j-1
findZero(a);
x="";
if(Y-1 >= 0 && s[l]!=char(a[X][Y-1]+'0')){
for(int i1=0;i1<N;i1++)
for(int j1=0;j1<N;j1++)
c[i1][j1] = a[i1][j1];
swap(c[X][Y],c[X][Y-1]);
solve(c, s+char(c[X][Y]+'0'));
}
//i,j+1
findZero(a);
x="";
if(Y+1 < N && s[l]!=char(a[X][Y+1]+'0')){
for(int i1=0;i1<N;i1++)
for(int j1=0;j1<N;j1++)
c[i1][j1] = a[i1][j1];
swap(c[X][Y],c[X][Y+1]);
solve(c, s+char(c[X][Y]+'0'));
}
}
}
int main()
{
srand(time(0));
ifstream cin("input.txt");
/* for(int i=0;i<N;i++)
{
for(int j=0; j<N;j++)
{
while(1)
{
t = rand()%(N*N);
if(b[t]==0)
{
b[t]=1;
a[i][j] = t;
break;
}
}
cout << a[i][j] <<" ";
}
cout << endl;
}*/
for(int i=0;i<N;i++)
for(int j=0; j<N;j++)
cin>>a[i][j];
cout<<"-----------------------\n";
for(int i=0;i<N;i++){
for(int j=0;j<N;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
cout<<"-----------------------\n";
/* for(int i=1;;i++)
for(int j=0;j<answers.length();j++)
if(answers[j].length()==1){
cout<<answers[j]<<endl;
system("pause");
}*/
solve(a,"");
cout<<M<<endl;
system("pause");
}
I3ByYWdtYSBjb21tZW50IChsaW5rZXIsICIvU1RBQ0s6NjQwMDAwMDAiKTsKI2luY2x1ZGU8aW9zdHJlYW0+CiNpbmNsdWRlPHN0cmluZz4KI2luY2x1ZGUgPGZzdHJlYW0+CiNpbmNsdWRlIDxjdGltZT4KI2luY2x1ZGUgPGNzdGRsaWI+CiNpbmNsdWRlIDxhbGdvcml0aG0+CiNpbmNsdWRlIDxtYXA+CiNpbmNsdWRlIDx2ZWN0b3I+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmNvbnN0IGludCBOPTM7CmludCBhW05dW05dLCB0LCBiW04qTl07CmludCBYLFk7CnN0cmluZyBNPSIiOwptYXA8c3RyaW5nLGJvb2w+IG1hcDE7CnZlY3RvcjxzdHJpbmc+ICBhbnN3ZXJzOwpib29sIGNoZWsoaW50IGJbTl1bTl0pCnsKICAgIGludCBrPTE7CiAgICBpbnQgYz0wOwogICAgaWYoYltOLTFdW04tMV0hPTApcmV0dXJuIGZhbHNlOwogICAgZm9yKGludCBpPTA7aTxOO2krKykKICAgIHsKICAgICAgICBmb3IoaW50IGo9MDtqPE47aisrKQogICAgICAgIHsKICAgICAgICAgICAgICAgCiAgICAgICAgICAgIGlmKGJbaV1bal09PWspIAogICAgICAgICAgICAgICAgYysrOwogICAgICAgICAgCiAgICAgICAgICAgIGsrKzsKICAgICAgICB9CiAgICB9CiAgICAKICAgIGlmKGM9PU4qTi0xKXJldHVybiB0cnVlOwogICAgcmV0dXJuIGZhbHNlOwp9CnZvaWQgZmluZFplcm8oaW50IGFbTl1bTl0pCnsKICAgIGZvcihpbnQgaT0wO2k8TjtpKyspCiAgICB7CiAgICAgICAgZm9yKGludCBqPTA7ajxOO2orKykKICAgICAgICB7CiAgICAgICAgICAgaWYoYVtpXVtqXSA9PSAwKXsKICAgICAgICAgICAgICAgWD1pO1k9ajsKICAgICAgICAgICB9ICAgICAgICAgICAgICAgCiAgICAgICAgfQogICAgfQp9CnZvaWQgc29sdmUoaW50IGFbTl1bTl0sIHN0cmluZyBzKQp7CiAgICAgc3RyaW5nIHg9IiI7Ci8qICBmb3IoaW50IGk9MDtpPE47aSsrKXsKICAgICBmb3IoaW50IGo9MDtqPE47aisrKQogICAgIGNvdXQ8PGFbaV1bal08PCIgIjsKICAgICBjb3V0PDxlbmRsOwogICAgIH0KICAgIHN5c3RlbSgicGF1c2UiKTsqLwogCiAgICBmb3IoaW50IGk9MDtpPE47aSsrKQogICAgIGZvcihpbnQgaj0wO2o8TjtqKyspCiAgICAgICB4PXgrY2hhcihhW2ldW2pdKycwJyk7CiAgICBpbnQgY1tOXVtOXTsKCgogICAgaWYoY2hlayhhKT09MSkKICAgIHsKICAgIGlmKE09PSIiKU09czsKICAgIGlmKHMubGVuZ3RoKCk8TS5sZW5ndGgoKSlNPXM7CiAgICAgICAgY291dCA8PCAiSm9sID0gIiA8PCBzIDw8IGVuZGw7CiAgICAgICAgc3lzdGVtKCJwYXVzZSIpOwogICAgfQogICAgCiAgICBlbHNlIGlmKG1hcDFbeF09PTApCiAgICB7CiAgICAgICAgIG1hcDFbeF09MTsKICAgICAgICBmaW5kWmVybyhhKTsKICAgICAgICBpbnQgbD1zLmxlbmd0aCgpOwogICAgICAgIGwtLTsKICAgICAgICAvL2ktMSxqCiAgICAgICAgaWYoWC0xID49IDApewogICAgICAgIGZvcihpbnQgaTE9MDtpMTxOO2kxKyspCiAgICAgICAgICAgIGZvcihpbnQgajE9MDtqMTxOO2oxKyspCiAgICAgICAgICAgICAgICBjW2kxXVtqMV0gPSBhW2kxXVtqMV07CiAgICAgICAgc3dhcChjW1hdW1ldLGNbWC0xXVtZXSk7ICAgIAogICAgIApzb2x2ZShjLCBzK2NoYXIoY1tYXVtZXSsnMCcpKTsKICAgICAgICB9CiAgICAgICAgZmluZFplcm8oYSk7CiAgICAgICAgeD0iIjsKICAgICAgICAgIC8vaSsxLGoKICAgICAgICBpZihYKzEgPCBOICYmIHNbbF0hPWNoYXIoYVtYKzFdW1ldKycwJykpewogICAgICAgIGZvcihpbnQgaTE9MDtpMTxOO2kxKyspCiAgICAgICAgICAgIGZvcihpbnQgajE9MDtqMTxOO2oxKyspCiAgICAgICAgICAgICAgICBjW2kxXVtqMV0gPSBhW2kxXVtqMV07CiAgICAgICAgc3dhcChjW1hdW1ldLGNbWCsxXVtZXSk7ICAgICAgCiAgICAgIApzb2x2ZShjLCBzK2NoYXIoY1tYXVtZXSsnMCcpKTsKICAgICAgICB9CiAgICAgICAgICAvL2ksai0xCiAgICAgICAgIGZpbmRaZXJvKGEpOyAKICAgICAgICAgeD0iIjsKICAgICAgICBpZihZLTEgPj0gMCAmJiBzW2xdIT1jaGFyKGFbWF1bWS0xXSsnMCcpKXsKICAgICAgICBmb3IoaW50IGkxPTA7aTE8TjtpMSsrKQogICAgICAgICAgICBmb3IoaW50IGoxPTA7ajE8TjtqMSsrKQogICAgICAgICAgICAgICAgY1tpMV1bajFdID0gYVtpMV1bajFdOwogICAgICAgIHN3YXAoY1tYXVtZXSxjW1hdW1ktMV0pOyAgICAgICAgCiAgICAgCiAgICAgICBzb2x2ZShjLCBzK2NoYXIoY1tYXVtZXSsnMCcpKTsgICAgIAogICAgICAgIH0KICAgICAgICAgICAvL2ksaisxCiAgICAgICAgZmluZFplcm8oYSk7ICAgCiAgICAgICAgeD0iIjsKICAgICAgICBpZihZKzEgPCBOICYmIHNbbF0hPWNoYXIoYVtYXVtZKzFdKycwJykpewogICAgICAgIGZvcihpbnQgaTE9MDtpMTxOO2kxKyspCiAgICAgICAgICAgIGZvcihpbnQgajE9MDtqMTxOO2oxKyspCiAgICAgICAgICAgICAgICBjW2kxXVtqMV0gPSBhW2kxXVtqMV07CiAgICAgICAgc3dhcChjW1hdW1ldLGNbWF1bWSsxXSk7ICAgICAgICAKICAgICAgIAogICAgICAgc29sdmUoYywgcytjaGFyKGNbWF1bWV0rJzAnKSk7ICAgICAKICAgICAgICB9CiAgICAgICAgCiAgICB9CiAgICAKfQoKaW50IG1haW4oKQp7CiAgICBzcmFuZCh0aW1lKDApKTsKICAgIGlmc3RyZWFtIGNpbigiaW5wdXQudHh0Iik7CiAgLyogIGZvcihpbnQgaT0wO2k8TjtpKyspCiAgICB7CiAgICAgICAgZm9yKGludCBqPTA7IGo8TjtqKyspCiAgICAgICAgewogICAgICAgICAgICB3aGlsZSgxKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICB0ID0gcmFuZCgpJShOKk4pOwogICAgICAgICAgICAgICAgaWYoYlt0XT09MCkKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICBiW3RdPTE7CiAgICAgICAgICAgICAgICAgICAgICAgIGFbaV1bal0gPSB0OwogICAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgY291dCA8PCBhW2ldW2pdIDw8IiAiOwogICAgICAgIH0KICAgICAgICAKICAgICAgICBjb3V0IDw8IGVuZGw7CiAgICB9Ki8KCgogICAgICBmb3IoaW50IGk9MDtpPE47aSsrKQogICAgICAgICAgICBmb3IoaW50IGo9MDsgajxOO2orKykKICAgICAgICAgICAgIGNpbj4+YVtpXVtqXTsKICAgIGNvdXQ8PCItLS0tLS0tLS0tLS0tLS0tLS0tLS0tLVxuIjsKICAgICBmb3IoaW50IGk9MDtpPE47aSsrKXsKICAgICBmb3IoaW50IGo9MDtqPE47aisrKQogICAgICAgY291dDw8YVtpXVtqXTw8IiAiOwogICAgICAgY291dDw8ZW5kbDsKICAgICB9CiAgICAgICAgIGNvdXQ8PCItLS0tLS0tLS0tLS0tLS0tLS0tLS0tLVxuIjsKICAvKiAgIGZvcihpbnQgaT0xOztpKyspCiAgICAgIGZvcihpbnQgaj0wO2o8YW5zd2Vycy5sZW5ndGgoKTtqKyspCiAgICAgICBpZihhbnN3ZXJzW2pdLmxlbmd0aCgpPT0xKXsKICAgICAgICBjb3V0PDxhbnN3ZXJzW2pdPDxlbmRsOwogICAgICAgICAgICBzeXN0ZW0oInBhdXNlIik7CiAgICAgICAgfSovCiAgICBzb2x2ZShhLCIiKTsKICAgIGNvdXQ8PE08PGVuZGw7CiAgICBzeXN0ZW0oInBhdXNlIik7CiAgICAKIH0=