1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
| import java.io.*; import java.util.*; import java.util.concurrent.LinkedBlockingQueue; public class Main { static int result; public static void main(String[] args) { Scanner sc=new Scanner(System.in); PrintWriter pw= new PrintWriter(System.out); while(sc.hasNext()){ String s=sc.next(); String e=sc.next(); if(bfs(s,e))pw.println(result); else pw.println(-1); pw.flush(); } } static boolean bfs(String s,String e){ LinkedBlockingQueue<Node>queues=new LinkedBlockingQueue(); HashMap<String,Integer>maps=new HashMap<String,Integer>(); HashMap<String,Integer>mape=new HashMap<String,Integer>(); LinkedBlockingQueue<Node>queuee=new LinkedBlockingQueue(); queues.add(new Node(s,0)); queuee.add(new Node(e,0)); maps.put(s, 0); mape.put(e, 0); while(!queues.isEmpty()||!queuee.isEmpty()){ StringBuilder sb; if(!queues.isEmpty()){ Node temps=queues.poll(); if(mape.containsKey(temps.s)){ result=mape.get(temps.s)+temps.count; return true; } int indexs=-1; for(int i=0;i<temps.s.length();i++){ if(temps.s.charAt(i)=='.') indexs=i; } int left=left(indexs); int right=right(indexs); int up=up(indexs); int down=down(indexs); if(left!=-1){ sb=new StringBuilder(temps.s); sb.setCharAt(indexs,temps.s.charAt(left)); sb.setCharAt(left,'.'); String t=sb.toString(); if(!maps.containsKey(t)){ queues.add(new Node(t,temps.count+1)); maps.put(t,temps.count+1); } } if(right!=-1){ sb=new StringBuilder(temps.s); sb.setCharAt(indexs,temps.s.charAt(right)); sb.setCharAt(right,'.'); String t=sb.toString(); if(!maps.containsKey(t)){ queues.add(new Node(t,temps.count+1)); maps.put(t,temps.count+1); } } if(up!=-1){ sb=new StringBuilder(temps.s); sb.setCharAt(indexs,temps.s.charAt(up)); sb.setCharAt(up,'.'); String t=sb.toString(); if(!maps.containsKey(t)){ queues.add(new Node(t,temps.count+1)); maps.put(t,temps.count+1); } } if(down!=-1){ sb=new StringBuilder(temps.s); sb.setCharAt(indexs,temps.s.charAt(down)); sb.setCharAt(down,'.'); String t=sb.toString(); if(!maps.containsKey(t)){ queues.add(new Node(t,temps.count+1)); maps.put(t,temps.count+1); } } } if(!queuee.isEmpty()){ Node tempe=queuee.poll(); if(maps.containsKey(tempe.s)){ result=maps.get(tempe.s)+tempe.count; return true; } int indexe=-1; for(int i=0;i<tempe.s.length();i++){ if(tempe.s.charAt(i)=='.') indexe=i; } int lefte=left(indexe); int righte=right(indexe); int upe=up(indexe); int downe=down(indexe); if(lefte!=-1){ sb=new StringBuilder(tempe.s); sb.setCharAt(indexe,tempe.s.charAt(lefte)); sb.setCharAt(lefte,'.'); String t=sb.toString(); if(!mape.containsKey(t)){ queuee.add(new Node(t,tempe.count+1)); mape.put(t,tempe.count+1); } } if(righte!=-1){ sb=new StringBuilder(tempe.s); sb.setCharAt(indexe,tempe.s.charAt(righte)); sb.setCharAt(righte,'.'); String t=sb.toString(); if(!mape.containsKey(t)){ queuee.add(new Node(t,tempe.count+1)); mape.put(t,tempe.count+1); } } if(upe!=-1){ sb=new StringBuilder(tempe.s); sb.setCharAt(indexe,tempe.s.charAt(upe)); sb.setCharAt(upe,'.'); String t=sb.toString(); if(!mape.containsKey(t)){ queuee.add(new Node(t,tempe.count+1)); mape.put(t,tempe.count+1); } } if(downe!=-1){ sb=new StringBuilder(tempe.s); sb.setCharAt(indexe,tempe.s.charAt(downe)); sb.setCharAt(downe,'.'); String t=sb.toString(); if(!mape.containsKey(t)){ queuee.add(new Node(t,tempe.count+1)); mape.put(t,tempe.count+1); } } } } return false; } static int left(int index){ if(index==0||index==3||index==6) return -1; else return index-1; } static int up(int index){ if(index==0||index==1||index==2) return -1; else return index-3; } static int right(int index){ if(index==2||index==5||index==8) return -1; else return index+1; } static int down(int index){ if(index==6||index==7||index==8) return -1; else return index+3; } } class Node{ String s; int count; Node(String s,int t){ this.s=s; this.count=t; } }
|