Advertisement
Dennnhhhickk

Untitled

Jan 22nd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. program Project2;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6. SysUtils;
  7.  
  8. const
  9. MAXIN = 1000;
  10.  
  11. type
  12. mas = record
  13. n1, s1, e1, w1: boolean;
  14. data, colour, go: integer;
  15. end;
  16.  
  17. var
  18. ans, last, i, j, n, m, x, y, f, a1, b, z: integer;
  19. a, c: array [-1..MAXIN, -1..MAXIN] of integer;
  20. p: array [-1 ..MAXIN, -1..MAXIN] of mas;
  21. dp: array [-1..4000, -1..100, -1..100] of integer;
  22.  
  23. s, time: string;
  24.  
  25. function min(a, b: integer): integer;
  26. begin
  27. if (a < b) then
  28. min := a
  29. else
  30. min := b;
  31. end;
  32.  
  33. begin
  34. last := 0;
  35. readln(m, n);
  36. readln(y, x, f);
  37. readln(b, a1);
  38.  
  39. for i := 0 to n do
  40. for j := 0 to m do
  41. begin
  42. p[i][j].n1 := false;
  43. p[i][j].s1 := false;
  44. p[i][j].w1 := false;
  45. p[i][j].e1 := false;
  46. for z := 0 to f do
  47. dp[z][i][j] := MAXINT - 10;
  48. end;
  49.  
  50. for i := n downto 0 do
  51. begin
  52. readln(s);
  53. s := s + ' ';
  54. j := 0;
  55. while (length(s) <> 0) do
  56. begin
  57. time := copy(s, 1, pos(' ', s));
  58. delete(s, 1, pos(' ', s));
  59.  
  60. if (pos('N', time) <> 0) then
  61. p[i][j].n1 := true;
  62.  
  63. if (pos('S', time) <> 0) then
  64. p[i][j].s1 := true;
  65.  
  66. if (pos('E', time) <> 0) then
  67. p[i][j].e1 := true;
  68.  
  69. if (pos('W', time) <> 0) then
  70. p[i][j].w1 := true;
  71. inc(j);
  72. end;
  73. end;
  74.  
  75. dp[f][x][y] := 0;
  76.  
  77. for z := f downto 1 do
  78. for i := 0 to n do
  79. for j := 0 to m do
  80. begin
  81. if (p[i][j].n1) then
  82. dp[z - 1][i + 1][j] := min(dp[z - 1][i + 1][j], dp[z][i][j])
  83. else
  84. dp[z - 1][i + 1][j] := min(dp[z - 1][i + 1][j], dp[z][i][j] + 1);
  85.  
  86. if (p[i][j].s1) then
  87. dp[z - 1][i - 1][j] := min(dp[z - 1][i - 1][j], dp[z][i][j])
  88. else
  89. dp[z - 1][i - 1][j] := min(dp[z - 1][i - 1][j], dp[z][i][j] + 1);
  90.  
  91. if (p[i][j].w1) then
  92. dp[z - 1][i][j - 1] := min(dp[z - 1][i][j - 1], dp[z][i][j])
  93. else
  94. dp[z - 1][i][j - 1] := min(dp[z - 1][i][j - 1], dp[z][i][j] + 1);
  95.  
  96. if (p[i][j].e1) then
  97. dp[z - 1][i][j + 1] := min(dp[z - 1][i][j + 1], dp[z][i][j])
  98. else
  99. dp[z - 1][i][j + 1] := min(dp[z - 1][i][j + 1], dp[z][i][j] + 1);
  100. end;
  101.  
  102. ans := MAXINT - 10;
  103. for z := 0 to f do
  104. if (dp[z][a1][b] < ans) then
  105. ans := dp[z][a1][b];
  106.  
  107. {for i := 0 to n do
  108. begin
  109. for j := 0 to m do
  110. write(dp[4][i][j]:10, ' ');
  111. writeln;
  112. end;}
  113. if (ans = MAXINT - 10) then
  114. writeln(-1)
  115. else
  116. writeln(ans);
  117. readln;
  118. readln;
  119. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement