fork download
  1. var
  2. i,j,a,y,min:longint;
  3. s:string;
  4. m:array[0..5,0..5] of boolean;
  5. o:array[0..65535] of longint;
  6.  
  7. function js:longint;
  8. var
  9. i,j,a:longint;
  10. begin
  11. a:=0;
  12. for i:=1 to 4 do
  13. for j:=1 to 4 do
  14. if m[i,j] then a:=(a shl 1)+1 else a:=a shl 1;
  15. exit(a);
  16. end;
  17.  
  18. procedure bfs(x,t:longint);
  19. var
  20. i,j:longint;
  21. begin
  22. if t>=min then exit;
  23. if x=y then
  24. begin
  25. min:=t;
  26. exit;
  27. end;
  28. if t>=o[x] then exit;
  29. o[x]:=t;
  30. for i:=1 to 4 do
  31. for j:=1 to 4 do
  32. if m[i,j] then
  33. begin
  34. if m[i-1,j]=false then
  35. begin
  36. m[i-1,j]:=true;
  37. m[i,j]:=false;
  38. bfs(js,t+1);
  39. m[i,j]:=true;
  40. m[i-1,j]:=false;
  41. end;
  42. if m[i+1,j]=false then
  43. begin
  44. m[i+1,j]:=true;
  45. m[i,j]:=false;
  46. bfs(js,t+1);
  47. m[i,j]:=true;
  48. m[i+1,j]:=false;
  49. end;
  50. if m[i,j-1]=false then
  51. begin
  52. m[i,j-1]:=true;
  53. m[i,j]:=false;
  54. bfs(js,t+1);
  55. m[i,j]:=true;
  56. m[i,j-1]:=false;
  57. end;
  58. if m[i,j+1]=false then
  59. begin
  60. m[i,j+1]:=true;
  61. m[i,j]:=false;
  62. bfs(js,t+1);
  63. m[i,j]:=true;
  64. m[i,j+1]:=false;
  65. end;
  66. end;
  67. end;
  68.  
  69. begin
  70. min:=100;
  71. for i:=0 to 5 do
  72. begin
  73. m[i,0]:=true;
  74. m[i,5]:=true;
  75. m[0,i]:=true;
  76. m[5,i]:=true;
  77. end;
  78. for i:=0 to 65535 do o[i]:=1 shl 31-1;
  79. for i:=1 to 4 do
  80. begin
  81. readln(s);
  82. for j:=1 to 4 do
  83. if s[j]='1' then m[i,j]:=true else m[i,j]:=false;
  84. end;
  85. readln;
  86. for i:=1 to 4 do
  87. begin
  88. readln(s);
  89. for j:=1 to 4 do
  90. if s[j]='1' then y:=(y shl 1)+1 else y:=y shl 1;
  91. end;
  92. bfs(js,0);
  93. writeln(min);
  94. end.
Success #stdin #stdout 0.39s 488KB
stdin
1111
0000
1110
0010

1010
0101
1010
0101
stdout
4