Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. #define fi first
  4. #define se second
  5. #define ll long long
  6. #define pb push_back
  7. #define mp make_pair
  8. #define mt make_tuple
  9.  
  10. using namespace std;
  11.  
  12. struct struk
  13. {
  14. int x, y, tip, vr;
  15. };
  16. inline bool operator<(const struk& a, const struk& b)
  17. {
  18. return a.vr > b.vr;
  19. }
  20.  
  21. priority_queue<struk> niz;
  22. int mat[510][510];
  23. int vreme[510][510][6];
  24. int obidjeni[510][510][6];
  25.  
  26. int INF = 1000000009;
  27. int n, m;
  28.  
  29. void pred()
  30. {
  31. for(int i = 0; i < n; i++)
  32. for(int j = 0; j < m; j++)
  33. for(int z = 1; z <= 5; z++)
  34. vreme[i][j][z] = INF;
  35. }
  36.  
  37. bool moze(int a, int b)
  38. {
  39. return a >= 0 && a < n && b >= 0 && b < m && mat[a][b] == 0;
  40. }
  41.  
  42. void update(int a, int b, int c, int vred)
  43. {
  44. struk pom;
  45. pom.x = a;
  46. pom.y = b;
  47. pom.tip = c;
  48. pom.vr = vreme[a][b][c];
  49. vreme[a][b][c] = vred;
  50.  
  51.  
  52.  
  53. if(obidjeni[a][b][c] == 0)
  54. {
  55. pom.vr = vred;
  56. niz.push(pom);
  57. }
  58. }
  59.  
  60. void menjaj(struk pom)
  61. {
  62. for(int i = 1; i <= 5; i++)
  63. {
  64. if(i == pom.tip) continue;
  65. int t = vreme[pom.x][pom.y][pom.tip] + i;
  66. if(t < vreme[pom.x][pom.y][i]) update(pom.x, pom.y, i, t);
  67. }
  68. }
  69.  
  70. void pomeri(struk pom, int pi, int pj, int cnt)
  71. {
  72. int i = pom.x + pi;
  73. int j = pom.y + pj;
  74. while(cnt > 0 && moze(i, j))
  75. {
  76. int t = vreme[pom.x][pom.y][pom.tip] + 1;
  77. if(t < vreme[i][j][pom.tip]) update(i, j, pom.tip, t);
  78.  
  79. cnt--;
  80. i += pi;
  81. j += pj;
  82. }
  83.  
  84. }
  85.  
  86. void kralj(struk pom)
  87. {
  88. for(int i = -1; i <= 1; i++)
  89. {
  90. for(int j = -1; j <= 1; j++)
  91. {
  92. if(i == 0 && j == 0) continue;
  93. pomeri(pom, i, j, 1);
  94. }
  95. }
  96. }
  97.  
  98. void lovac(struk pom)
  99. {
  100. for(int i = -1; i <= 1; i++)
  101. {
  102. for(int j = -1; j <= 1; j++)
  103. {
  104. if(i == 0 || j == 0) continue;
  105. pomeri(pom, i, j, 1000);
  106. }
  107. }
  108. }
  109.  
  110. void top(struk pom)
  111. {
  112. for(int i = -1; i <= 1; i++)
  113. {
  114. for(int j = -1; j <= 1; j++)
  115. {
  116. if((i != 0 && j != 0) || (i == 0 && j == 0)) continue;
  117. pomeri(pom, i, j, 1000);
  118. }
  119. }
  120. }
  121. void konj(struk pom)
  122. {
  123. pomeri(pom, 2, 1, 1);
  124. pomeri(pom, 2, -1, 1);
  125. pomeri(pom, -2, 1, 1);
  126. pomeri(pom, -2, -1, 1);
  127. pomeri(pom, 1, 2, 1);
  128. pomeri(pom, 1, -2, 1);
  129. pomeri(pom, -1, 2, 1);
  130. pomeri(pom, -1, -2, 1);
  131. }
  132. void kraljica(struk pom)
  133. {
  134. top(pom);
  135. lovac(pom);
  136. }
  137.  
  138.  
  139. void siri(struk pom)
  140. {
  141. if(pom.tip == 1) kralj(pom);
  142. if(pom.tip == 2) lovac(pom);
  143. if(pom.tip == 3) top(pom);
  144. if(pom.tip == 4) konj(pom);
  145. if(pom.tip == 5) kraljica(pom);
  146. }
  147.  
  148.  
  149.  
  150. int main()
  151. {
  152. ios_base::sync_with_stdio(false);
  153. cin.tie(NULL);
  154.  
  155. cin >> n >> m;
  156. pred();
  157. for(int i = 0; i < n; i++)
  158. {
  159. string s;
  160. cin >> s;
  161. for(int j = 0; j < m; j++)
  162. if(s[j] == '#') mat[i][j] = 1;
  163. else mat[i][j] = 0;
  164. }
  165.  
  166. update(0,0,1,0);
  167.  
  168. int br = 1;
  169. while(niz.size() > 0)
  170. {
  171. struk pom = niz.top();
  172. if(obidjeni[pom.x][pom.y][pom.tip] == 0)
  173. {
  174. br++;
  175. obidjeni[pom.x][pom.y][pom.tip] = 1;
  176. menjaj(pom);
  177. siri(pom);
  178. }
  179.  
  180.  
  181. niz.pop();
  182. }
  183. int best = INF;
  184. for(int z = 1; z <= 5; z++) best = min(best, vreme[n-1][m-1][z]);
  185.  
  186. if(best == INF) cout << -1;
  187. else cout << best;
  188. return 0;
  189. }
  190.  
  191. Petlja.org
  192. Fondacija Petlja
  193. loop@petlja.org
  194. © Copyright 2018. Petlja.org
  195. Grafički dizajn enjoy.ing
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement