a53

liceu

a53
Aug 23rd, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.02 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. struct liceu
  4. {
  5. int x1, y1, x2, y2;
  6. }pozscrt[15501];
  7. struct liceu1
  8. {
  9. int x, y, val;
  10. bool operator <(const liceu1& other) const
  11. {
  12. return val > other.val;
  13. }
  14. }aux, aux1, aux2;
  15. struct liceu2
  16. {
  17. int x, y;
  18. }traseumag[160001], traseumagclas[160001], traseuclasa[160001];
  19. priority_queue <liceu1> q;
  20. int n, z, s, t, i, x, y, xm, ym, dismag, d, minpasi, k, dclasa, dmagclasa;
  21. int j, ii, jj, d1;
  22. int a[405][405], scurtaturi[401][401];
  23. const int dx[] = {-1, 0, 1, 0};
  24. const int dy[] = {0, 1, 0, -1};
  25. void lee(int x1, int y1, int x2, int y2)
  26. {
  27. aux.x = x1;
  28. aux.y = y1;
  29. aux.val = 1;
  30. a[aux.x][aux.y] = 1;
  31. q.push(aux);
  32. while(!q.empty())
  33. {
  34. aux = q.top();
  35. q.pop();
  36. for(k = 0; k < 4; k++)
  37. {
  38. aux1.x = aux.x + dx[k];
  39. aux1.y = aux.y + dy[k];
  40. if(a[aux1.x][aux1.y] == 0)
  41. {
  42. a[aux1.x][aux1.y] = a[aux.x][aux.y] + 1;
  43. aux1.val = a[aux1.x][aux1.y];
  44. if(scurtaturi[aux1.x][aux1.y] != 0)
  45. {
  46. aux2.x = pozscrt[scurtaturi[aux1.x][aux1.y]].x1;
  47. aux2.y = pozscrt[scurtaturi[aux1.x][aux1.y]].y1;
  48. if(a[aux2.x][aux2.y] == 0)
  49. {
  50. a[aux2.x][aux2.y] = aux2.val = a[aux.x][aux.y] + 2;
  51. q.push(aux2);
  52. }
  53. else q.push(aux1);
  54. aux2.x = pozscrt[scurtaturi[aux1.x][aux1.y]].x2;
  55. aux2.y = pozscrt[scurtaturi[aux1.x][aux1.y]].y2;
  56. if(a[aux2.x][aux2.y] == 0)
  57. {
  58. a[aux2.x][aux2.y] = aux2.val = a[aux.x][aux.y] + 2;
  59. q.push(aux2);
  60. }
  61. else q.push(aux1);
  62. }
  63. else q.push(aux1);
  64. }
  65. }
  66. }
  67. }
  68. int main()
  69. {
  70. ifstream f("liceu.in");
  71. ofstream g("liceu.out");
  72. f >> n >> z >> s >> t;
  73. for(i = 1; i <= z; i++)
  74. {
  75. f >> x >> y;
  76. a[x][y] = -1;
  77. }
  78. for(i = 1; i <= s; i++)
  79. {
  80. f >> pozscrt[i].x1 >> pozscrt[i].y1 >> pozscrt[i].x2 >> pozscrt[i].y2;
  81. scurtaturi[pozscrt[i].x1][pozscrt[i].y1] = scurtaturi[pozscrt[i].x2][pozscrt[i].y2] = i;
  82. }
  83. f >> xm >> ym;
  84. for(i = 0; i <= n + 1; i++)
  85. a[0][i] = a[n + 1][i] = a[i][0] = a[i][n + 1] = -1;
  86. lee(1, 1, xm, ym);
  87. dismag = d = a[xm][ym];
  88. traseumag[d].x = xm;
  89. traseumag[d].y = ym;
  90. x = xm;
  91. y = ym;
  92. d--;
  93. while(d > 0)
  94. {
  95. d1 = d;
  96. for(k = 0; k < 4; k++)
  97. {
  98. ii = x + dx[k];
  99. jj = y + dy[k];
  100. if(a[ii][jj] == d)
  101. {
  102. traseumag[d].x = ii;
  103. traseumag[d].y = jj;
  104. x = ii;
  105. y = jj;
  106. d--;
  107. break;
  108. }
  109. }
  110. if(d1 == d)
  111. {
  112. if(pozscrt[scurtaturi[x][y]].x1 == x && pozscrt[scurtaturi[x][y]].y1 == y)
  113. {
  114. traseumag[d].x = ii = pozscrt[scurtaturi[x][y]].x2;
  115. traseumag[d].y = jj = pozscrt[scurtaturi[x][y]].y2;
  116. x = ii;
  117. y = jj;
  118. d--;
  119. }
  120. else
  121. {
  122. traseumag[d].x = ii = pozscrt[scurtaturi[x][y]].x1;
  123. traseumag[d].y = jj = pozscrt[scurtaturi[x][y]].y1;
  124. x = ii;
  125. y = jj;
  126. d--;
  127. }
  128. }
  129. }
  130. for(i = 1; i <= n; i++)
  131. for(j = 1; j <= n; j++)
  132. if(a[i][j] != -1) a[i][j] = 0;
  133. lee(xm, ym, n, n);
  134. dmagclasa = d = a[n][n];
  135. if(dmagclasa + dismag - 1 > t || dismag == 0)
  136. {
  137. for(i = 1; i <= n; i++)
  138. for(j = 1; j <= n; j++)
  139. if(a[i][j] != -1) a[i][j] = 0;
  140. lee(1, 1, n, n);
  141. dclasa = d = a[n][n];
  142. traseuclasa[d].x = n;
  143. traseuclasa[d].y = n;
  144. x = n;
  145. y = n;
  146. d--;
  147. while(d > 0)
  148. {
  149. d1 = d;
  150. for(k = 0; k < 4; k++)
  151. {
  152. ii = x + dx[k];
  153. jj = y + dy[k];
  154. if(a[ii][jj] == d)
  155. {
  156. traseuclasa[d].x = ii;
  157. traseuclasa[d].y = jj;
  158. x = ii;
  159. y = jj;
  160. d--;
  161. break;
  162. }
  163. }
  164. if(d1 == d)
  165. {
  166. if(pozscrt[scurtaturi[x][y]].x1 == x && pozscrt[scurtaturi[x][y]].y1 == y)
  167. {
  168. traseuclasa[d].x = ii = pozscrt[scurtaturi[x][y]].x2;
  169. traseuclasa[d].y = jj = pozscrt[scurtaturi[x][y]].y2;
  170. x = ii;
  171. y = jj;
  172. d--;
  173. }
  174. else
  175. {
  176. traseuclasa[d].x = ii = pozscrt[scurtaturi[x][y]].x1;
  177. traseuclasa[d].y = jj = pozscrt[scurtaturi[x][y]].y1;
  178. x = ii;
  179. y = jj;
  180. d--;
  181. }
  182. }
  183. }
  184. g << dclasa << "\n";
  185. for(i = 1; i <= dclasa; i++)
  186. g << traseuclasa[i].x << " " << traseuclasa[i].y << "\n";
  187. }
  188. else
  189. {
  190. g << dismag + dmagclasa - 1 << "\n";
  191. for(i = 1; i <= dismag; i++)
  192. g << traseumag[i].x << " " << traseumag[i].y << "\n";
  193. dmagclasa = d = a[n][n];
  194. traseumagclas[d].x = n;
  195. traseumagclas[d].y = n;
  196. x = n;
  197. y = n;
  198. d--;
  199. while(d > 0)
  200. {
  201. d1 = d;
  202. for(k = 0; k < 4; k++)
  203. {
  204. ii = x + dx[k];
  205. jj = y + dy[k];
  206. if(a[ii][jj] == d)
  207. {
  208. traseumagclas[d].x = ii;
  209. traseumagclas[d].y = jj;
  210. x = ii;
  211. y = jj;
  212. d--;
  213. break;
  214. }
  215. }
  216. if(d1 == d)
  217. {
  218. if(pozscrt[scurtaturi[x][y]].x1 == x && pozscrt[scurtaturi[x][y]].y1 == y)
  219. {
  220. traseumagclas[d].x = ii = pozscrt[scurtaturi[x][y]].x2;
  221. traseumagclas[d].y = jj = pozscrt[scurtaturi[x][y]].y2;
  222. x = ii;
  223. y = jj;
  224. d--;
  225. }
  226. else
  227. {
  228. traseumagclas[d].x = ii = pozscrt[scurtaturi[x][y]].x1;
  229. traseumagclas[d].y = jj = pozscrt[scurtaturi[x][y]].y1;
  230. x = ii;
  231. y = jj;
  232. d--;
  233. }
  234. }
  235. }
  236. for(i = 2; i <= dmagclasa; i++)
  237. g << traseumagclas[i].x << " " << traseumagclas[i].y << "\n";
  238. }
  239. return 0;
  240. }
Add Comment
Please, Sign In to add comment