BFS.
import java.util.*;
class Main{
static class node{
int x;
int y;
int cnt;
String s;
String s1;
public node(int x, int y, int cnt,String s,String s1) {
this.x = x;
this.y = y;
this.cnt = cnt;
this.s=s;
this.s1=s1;
}
}
static int dx[]= {0,0,1,-1};
static int dy[]= {1,-1,0,0};
static char a[][]=new char [3][3];
static String ans=”12345678x”;
static int vis[][][];
static String state=”“;
static boolean flag=true;
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
String s=in.next();
a[i][j]=s.charAt(0);
state+=a[i][j];
}
}
vis=new int [3][3][100];
bfs();
if(flag) {
System.out.println("unsolvable");
}
}
static String p="rldu";
static Set<String> set=new HashSet<String>();
static void bfs() {
Queue<node> q=new ArrayDeque<Main.node>();
int k=0;
for(int i=0;i<state.length();i++) {
if(state.charAt(i)=='x') {
k=i;
}
}
set.add(state);
q.add(new node(k/3,k%3,0,state,""));
vis[k/3][k%3][0]=1;
while(!q.isEmpty()) {
node u=q.peek();
q.poll();
set.add(u.s);
if(u.s.equals(ans)) {
flag=false;
System.out.println(u.s1);
break;
}
for(int i=0;i<dx.length;i++) {
int x=u.x+dx[i];
int y=u.y+dy[i];
if(x>=0&&y>=0&&x<3&&y<3) {
char c[]=u.s.toCharArray();
char t=c[3*x+y];
c[3*x+y]=c[3*u.x+u.y];
c[3*u.x+u.y]=t;
if(set.contains(new String(c))) {
continue;
}
q.add(new node(x,y,u.cnt+1,new String(c),u.s1+p.charAt(i)));
}
}
}
}
}