Advertisement
WadeRollins2710

Recursion

Nov 30th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. Program Dq;
  2. Uses crt;
  3. Const
  4. InputFile = 'DQ.INP';
  5. OutputFile = 'DQ.OUT';
  6. Var
  7. f: Text;
  8. A: Array [1..100,1..100] of Integer;
  9. X, Y: Array [1..4] of Integer;
  10. G, H: Array [1..10000] of Integer;
  11. S: Array [1..10000] of String;
  12. n, m, count: Integer;
  13. Procedure Input;
  14. Var
  15. i, j: Integer;
  16. Begin
  17. Assign(f,InputFile);
  18. Reset(f);
  19. Readln(f,n,m);
  20. count:=0;
  21. X[1]:=-1; X[2]:=0; X[3]:=1; X[4]:=0;
  22. Y[1]:=0; Y[2]:=1; Y[3]:=0; Y[4]:=-1;
  23. For i:=1 to n do
  24. Begin
  25. For j:=1 to m do
  26. Read(f,A[i,j]);
  27. Readln(f);
  28. End;
  29. Close(f);
  30. End;
  31. Procedure Output(v: Integer);
  32. Var
  33. i: Integer;
  34. Begin
  35. Assign(f,OutputFile);
  36. Rewrite(f);
  37. For i:=1 to v do
  38. Writeln(f,G[i],' ',H[i],' ',S[i]);
  39. Close(f);
  40. Halt;
  41. End;
  42. Procedure Go(v: Integer);
  43. Var
  44. i, j, k: Integer;
  45. Begin
  46. If Count=1 then Output(v)
  47. else
  48. For i:=1 to n do
  49. For j:=1 to m do
  50. If A[i,j]=1 then
  51. For k:=1 to 4 do
  52. If (i+2*X[k]>=1) and (j+2*Y[k]>=1) and (i+2*X[k]<=n) and (j+2*Y[k]<=m) then
  53. If (A[i+X[k],j+Y[k]]=1) and (A[i+2*X[k],j+2*Y[k]]=0) then
  54. Begin
  55. A[i,j]:=0;
  56. A[i+X[k],j+Y[k]]:=0;
  57. A[i+2*X[k],j+2*Y[k]]:=1;
  58. dec(count);
  59. G[v]:=i;
  60. H[v]:=j;
  61. Case k of
  62. 1: S[v]:='Up';
  63. 2: S[v]:='Right';
  64. 3: S[v]:='Down';
  65. 4: S[v]:='Left';
  66. End;
  67. Go(v+1);
  68. A[i,j]:=1;
  69. A[i+X[k],j+Y[k]]:=1;
  70. A[i+2*X[k],j+2*Y[k]]:=0;
  71. inc(count);
  72. End;
  73. End;
  74. Procedure Process;
  75. Var
  76. i, j: Integer;
  77. Begin
  78. For i:=1 to n do
  79. For j:=1 to m do
  80. If A[i,j]=1 then inc(count);
  81. Go(1);
  82. End;
  83. Begin
  84. Input;
  85. Process;
  86. End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement