Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. program test;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6. SysUtils;
  7.  
  8. const
  9. MAXN = 80;
  10. INF = 1000000000;
  11.  
  12. var
  13. n, m, i, j, x, y, f, x1, y1, ans: integer;
  14. c: char;
  15. used: array[0..MAXN, 0..MAXN] of boolean;
  16. a: array[0..MAXN, 0..MAXN] of record
  17. north, south, east, west: boolean;
  18. end;
  19.  
  20. procedure dfs (x, y, p, q: integer);
  21. begin
  22. if (x = x1) and (y = y1) and (p < ans) then begin
  23. ans := p;
  24. exit;
  25. end;
  26. if (q = f) then exit;
  27. used[x, y] := true;
  28. if (x < n) and not (used[x + 1, y]) and (a[x, y].north) then
  29. dfs(x + 1, y, p, q + 1)
  30. else if (x < n) and not (used[x + 1, y]) then
  31. dfs(x + 1, y, p + 1, q + 1);
  32. if (x > 0) and not (used[x - 1, y]) and (a[x, y].south) then
  33. dfs(x - 1, y, p, q + 1)
  34. else if (x > 0) and not (used[x - 1, y]) then
  35. dfs(x - 1, y, p + 1, q + 1);
  36. if (y < m) and not (used[x, y + 1]) and (a[x, y].east) then
  37. dfs(x, y + 1, p, q + 1)
  38. else if (y < m) and not (used[x, y + 1]) then
  39. dfs(x, y + 1, p + 1, q + 1);
  40. if (y > 0) and not (used[x, y - 1]) and (a[x, y].west) then
  41. dfs(x, y - 1, p, q + 1)
  42. else if (y > 0) and not (used[x, y - 1]) then
  43. dfs(x, y - 1, p + 1, q + 1);
  44. used[x, y] := false;
  45. end;
  46.  
  47. begin
  48. readln(n, m);
  49. readln(x, y, f);
  50. readln(x1, y1);
  51. for i := n downto 0 do begin
  52. for j := 0 to m do with a[i, j] do begin
  53. used[i, j] := false;
  54. north := false;
  55. south := false;
  56. east := false;
  57. west := false;
  58. c := '=';
  59. while not ((c = ' ') or eoln) do begin
  60. read(c);
  61. case (c) of
  62. 'N': north := true;
  63. 'S': south := true;
  64. 'E': east := true;
  65. 'W': west := true;
  66. end;
  67. end;
  68. end;
  69. readln;
  70. end;
  71. ans := INF;
  72. dfs(x, y, 0, 0);
  73. writeln(ans);
  74. readln;
  75. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement