Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <queue>
  4. #include <stack>
  5. #include <algorithm>
  6. #define nmax 205
  7. #define maxim 42000
  8. #define inf 1000000000
  9. using namespace std;
  10.  
  11. int n, m, p, cartx, carty, k, gal, gal1;
  12. int ferma[nmax][nmax]; ///zona "agricola"
  13. ifstream f("cartite.in");
  14. ofstream g("cartite.out");
  15. queue <pair <int, int> > q;
  16. const int dx[]={0, 1, 0, -1};
  17. const int dy[]={1, 0, -1, 0};
  18. vector <vector <int> > G(maxim);
  19. vector <int> sol;
  20. stack <int> eul;
  21.  
  22. void add_vulpe(const int &x, const int &y, const int &r)
  23. { /// se marcheaza cu -1 zona cu vulpi
  24. if(!r)
  25. {
  26. ferma[x][y] = -1;
  27. }
  28. else
  29. if(r==1)
  30. {
  31. ferma[x][y] = -1;
  32. ferma[x][y+1] = -1;
  33. ferma[x][y-1] = -1;
  34. ferma[x+1][y] = -1;
  35. ferma[x-1][y] = -1;
  36. }
  37. if(r==2)
  38. {
  39. ferma[x][y] = -1;
  40. ferma[x][y+1] = -1;
  41. ferma[x][y-1] = -1;
  42. ferma[x+1][y] = -1;
  43. ferma[x-1][y] = -1;
  44.  
  45. if(y+2<=n+1)
  46. ferma[x][y+2] = -1;
  47. if(y-2>=0)
  48. ferma[x][y-2] = -1;
  49. if(x+2<=m+1)
  50. ferma[x+2][y] = -1;
  51. if(x-2>=0)
  52. ferma[x-2][y] = -1;
  53.  
  54. ferma[x+1][y+1] = -1;
  55. ferma[x+1][y-1] = -1;
  56. ferma[x-1][y-1] = -1;
  57. ferma[x-1][y+1] = -1;
  58. }
  59.  
  60. }
  61.  
  62. void read()
  63. {
  64. f >> p >> m >> n;
  65. f >> cartx >> carty;
  66. f >> k;
  67. for(int i=0, xc, yc, raza; i<k; ++i)
  68. {
  69. f >> xc >> yc >> raza;
  70. add_vulpe(xc, yc, raza);
  71. }
  72. f >> gal;
  73. for(int i=0, x1, y1, x2, y2, nod1, nod2; i<gal; ++i)
  74. {
  75. f >> x1 >> y1 >> x2 >> y2;
  76. nod1 = x1 * n - n + y1; ///transf poz (x,y) --> nr (nod)
  77. nod2 = x2 * n - n + y2;
  78. if(ferma[x1][y1]!=-1) gal1=nod1; ///de unde incep sa parcurg galeria
  79. G[nod1].push_back(nod2); ///lista cu galeriile (intrari/iesiri)
  80. G[nod2].push_back(nod1);
  81. if(ferma[x1][y1] != -1) ///marchez cu o val mare intrarea in galerii
  82. ferma[x1][y1] = inf; ///daca nu sunt in zona cu vulpi
  83. if(ferma[x1][y1] != -1)
  84. ferma[x2][y2] = inf;
  85. }
  86. f.close();
  87. }
  88.  
  89. void cer_a()
  90. {
  91. pair <int, int> xx, yy;
  92. int xf, yf, dist=1;
  93. for(int i=0; i<=m+1; ++i) ///bordez cu -1
  94. ferma[i][0] = -1, ferma[i][n+1] = -1;
  95. for(int i=0; i<=n+1; ++i)
  96. ferma[0][i] = -1, ferma[m+1][i] = -1;
  97.  
  98. q.push(make_pair(cartx, carty)); ///retin in coada traseu
  99.  
  100. if(ferma[cartx][carty] == inf)
  101. {
  102. g << cartx << ' ' << carty << ' ' << dist-1;
  103. g.close();
  104. return;
  105. }
  106.  
  107. ferma[cartx][carty] = 1;
  108. while(!q.empty())
  109. {
  110. xx = q.front(); q.pop(); ///caut cu Lee o galerie
  111. for(int k=0; k<4; ++k)
  112. {
  113. yy.first = xx.first + dx[k];
  114. yy.second = xx.second + dy[k];
  115. if(ferma[yy.first][yy.second] == inf) ///am gasit o galerie
  116. {
  117. xf = yy.first;
  118. yf = yy.second;
  119. dist = ferma[xx.first][xx.second] + 1;
  120. while(!q.empty()) q.pop();
  121. }
  122. if(ferma[yy.first][yy.second] == 0) ///pot inainta
  123. {
  124. ferma[yy.first][yy.second] = ferma[xx.first][xx.second] + 1;
  125. q.push(make_pair(yy.first, yy.second));
  126. }
  127. }
  128. }
  129. g << xf << ' ' << yf << ' ' << dist-1 << '\n'; ///coord galeriei
  130. g.close();
  131. }
  132.  
  133. void cer_b()
  134. {
  135. int xx, yy;
  136. xx = gal1;
  137. eul.push(xx); ///se construieste un ciclu eulerian
  138. while(!eul.empty())
  139. {
  140. xx = eul.top();
  141. if(G[xx].size())
  142. {
  143. yy = G[xx].back();
  144. G[xx].pop_back();
  145. G[yy].erase(find(G[yy].begin(), G[yy].end(), xx));
  146. eul.push(yy);
  147. }
  148. else
  149. {
  150. xx=eul.top();
  151. sol.push_back(xx);
  152. eul.pop();
  153. }
  154. }
  155. for(int i=0, a, b; i<sol.size(); ++i)
  156. {
  157. if(sol[i]%n==0) {
  158. a = sol[i]/n;
  159. b = n;
  160. g << a << ' ' << b << '\n';
  161. }
  162. else {
  163. a = sol[i]/n + 1;
  164. b = sol[i]%n;
  165. g << a << ' ' << b << '\n';
  166. }
  167. }
  168. g.close();
  169.  
  170. }
  171.  
  172. int main()
  173. {
  174. read();
  175. if(p==1)
  176. cer_a();
  177. else
  178. cer_b();
  179. return 0;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement