Advertisement
Guest User

lines

a guest
Oct 6th, 2015
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. program lines;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. type
  6. PointV = record
  7. X,
  8. Y: integer;
  9. end;
  10.  
  11. var
  12. Delta: array[1..5] of PointV;
  13. //
  14. CS: string;
  15. Found: boolean;
  16. Grid: array[0..42, 0..42] of char;
  17. Used: array[0..42, 0..42] of boolean;
  18. Ancestor: array[1..42, 1..42] of PointV;
  19. Queue: array[1..20000] of PointV;
  20.  
  21. QMStart,
  22. QMFinish: integer;
  23. CurPoint: PointV;
  24. HoldedX, HoldedY: integer;
  25. N, // 2..40
  26. enc,
  27. enc2: integer;
  28.  
  29. function IsOK(X, Y: integer): boolean;
  30. begin
  31. IsOK := true;
  32.  
  33. If (X < 1) or (Y < 1) or (Y > N) or (X > N) then IsOK := false;
  34. If Used[X, Y] then IsOK := false;
  35. If Grid[X, Y] = 'O' then IsOK := false;
  36.  
  37. end;
  38.  
  39. procedure push(X, Y: integer);
  40. begin
  41. Queue[QMFinish].X := X;
  42. Queue[QMFinish].Y := Y;
  43. QMFinish := QMFinish + 1;
  44. end;
  45.  
  46. procedure pop;
  47. begin
  48. Queue[QMStart].X := 0;
  49. Queue[QMStart].Y := 0;
  50. QMStart := QMStart + 1;
  51. end;
  52.  
  53. procedure top;
  54. begin
  55. CurPoint.X := Queue[QMStart].X;
  56. CurPoint.Y := Queue[QMStart].Y;
  57. end;
  58.  
  59. function size: integer;
  60. begin
  61. size := QMFinish - QMStart;
  62. end;
  63.  
  64. begin
  65. // Preset
  66. QMStart := 1;
  67. QMFinish := 1;
  68.  
  69. Delta[1].X := 1;
  70. Delta[1].Y := 0;
  71.  
  72. Delta[2].X := 0;
  73. Delta[2].Y := 1;
  74.  
  75. Delta[3].X := -1;
  76. Delta[3].Y := 0;
  77.  
  78. Delta[4].X := 0;
  79. Delta[4].Y := -1;
  80. Found := false;
  81.  
  82.  
  83. // ALG
  84. ReadLN(N);
  85.  
  86. For enc := 1 to N do
  87. begin
  88. ReadLN(CS);
  89. For enc2 := 1 to N do
  90. begin
  91. Grid[enc, enc2] := CS[enc2];
  92. If Grid[enc, enc2] = '@'
  93. Then
  94. begin
  95. Used[enc, enc2] := true;
  96. push(enc, enc2);
  97. end;
  98. end;
  99.  
  100. end;
  101.  
  102. //
  103.  
  104. While (size > 0) and (Found = false) do
  105. begin
  106. top;
  107. pop;
  108.  
  109. For enc := 1 to 4 do
  110. begin
  111.  
  112. If IsOK(CurPoint.X+Delta[enc].X, CurPoint.Y+Delta[enc].Y)
  113. then
  114. begin
  115. Used[CurPoint.X + Delta[enc].X, CurPoint.Y+Delta[enc].Y] := true;
  116. Push(CurPoint.X + Delta[enc].X, CurPoint.Y+Delta[enc].Y);
  117. Ancestor[CurPoint.X + Delta[enc].X, CurPoint.Y+Delta[enc].Y].X := CurPoint.X;
  118. Ancestor[CurPoint.X + Delta[enc].X, CurPoint.Y+Delta[enc].Y].Y := CurPoint.Y;
  119.  
  120. If Grid[CurPoint.X+Delta[enc].X, CurPoint.Y+Delta[enc].Y] = 'X'
  121. Then
  122. begin
  123. Found := true;
  124. CurPoint.X := CurPoint.X + Delta[enc].X;
  125. CurPoint.Y := CurPoint.Y + Delta[enc].Y;
  126. end;
  127.  
  128. end;
  129.  
  130. end;
  131.  
  132. end;
  133.  
  134.  
  135.  
  136. If Found = false
  137. Then Write('N')
  138. Else WriteLN('Y');
  139.  
  140. If true
  141. Then
  142. begin
  143.  
  144. While true do
  145. begin
  146. Grid[CurPoint.X, CurPoint.Y] := '+';
  147. HoldedX := CurPoint.X;
  148. HoldedY := CurPoint.Y;
  149. CurPoint.X := Ancestor[HoldedX, HoldedY].X;
  150. CurPoint.Y := Ancestor[HoldedX, HoldedY].Y;
  151.  
  152. If Grid[CurPoint.X, CurPoint.Y] = '@' Then
  153. begin
  154. Break;
  155. end;
  156. end;
  157.  
  158. // Print the map
  159. For enc := 1 to N do
  160. begin
  161.  
  162. For enc2 := 1 to N do
  163. begin
  164. Write(Grid[enc, enc2]);
  165. end;
  166.  
  167. If enc < N Then WriteLN('');
  168.  
  169. end;
  170.  
  171. end;
  172.  
  173. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement