Korotkodul

ОИ-2 bfs лабиринт mkG()

Jan 17th, 2022 (edited)
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <string>
  7. #include <stack>
  8. #include <set>
  9. #include <map>
  10. #define pii pair <int,int>
  11. #define vec vector
  12. using namespace std;
  13. using ll = long long;
  14. using ld = long double;
  15. using db = double;
  16. void cv(vector <int> &v){
  17. for (auto x: v) cout<<x<<' ';
  18. cout<<"\n";
  19. }
  20.  
  21. void cvl(vector <ll> &v){
  22. for (auto x: v) cout<<x<<' ';
  23. cout<<"\n";
  24. }
  25.  
  26.  
  27. void cvv(vector <vector <int> > &v){
  28. for (auto x: v) cv(x);
  29. cout<<"\n";
  30. }
  31.  
  32.  
  33. vector < vector < vector<pii> > > G;
  34. //vector[0] - слева - пара (x,y)
  35. //vec[1] - справа
  36. //vec[2] - сверху
  37. //vec[3] - снизу
  38. vector <vector<int>> dsk;
  39. vector <vector <int> > dist;
  40.  
  41. const int inf = 2e9;
  42.  
  43. int n,m;
  44. bool gd(pii x){
  45. return x.first >= 0 && x.first < n && x.second >= 0 && x.second < m && dsk[x.first][x.second] != 1;
  46. }
  47.  
  48. void shG(){
  49. for (int i=0;i<n;++i){
  50. for (int j=0;j<m;++j){
  51. cout<<"("<<i<<","<<j<<") \n";
  52. for (int k = 0;k<4;++k){
  53. cout<<G[i][j][k].first<<' '<<G[i][j][k].second<<"\n";
  54. }
  55. }
  56.  
  57. }cout<<"\n";
  58. }
  59.  
  60.  
  61. //
  62. void shdsk(){
  63. for (int i=0;i<n;++i){
  64. for (int j=0;j<m;++j){
  65. cout<<dsk[i][j]<<' ';
  66. }cout<<"\n";
  67. }cout<<"\n";
  68. }
  69.  
  70. void mkG(){
  71. //NB!!! учитывай, что если мы приходили в вершину v или u другим путем, то там НЕ НАДО изменять G(v)/G(u)!!!
  72. //добавб: не только if G(v)[direction] == {-1, -1} -> G(v)[direction] = u
  73. //НО И if G(u)[dir] == {-1, -1} -> G(u)[direction] = v;
  74. //vector[0] - слева - пара (x,y)
  75. //vec[1] - справа
  76. //vec[2] - сверху
  77. //vec[3] - снизу
  78. queue <pii> go;
  79. go.push({0,0});
  80. pii v, u;
  81. while (!go.empty()){
  82. v = go.front();
  83. if (G[v.first][v.second][0] == pii {-1, -1}){
  84. //слева
  85. while (gd({u.first, u.second-1})){
  86. u.second--;
  87. }
  88. if (u != v){
  89. G[v.first][v.second][0] = u;
  90. if (G[u.first][u.second][1] == pii {-1,-1}) G[u.first][u.second][1] = v;
  91. go.push(u);
  92. }
  93. }
  94. if (G[v.first][v.second][1] == pii {-1, -1}){
  95. //справа
  96. u = v;
  97. while (gd({u.first, u.second+1})){
  98. u.second++;
  99. }
  100. if (u != v){
  101. G[v.first][v.second][1] = u;
  102. if (G[u.first][u.second][0] == pii {-1,-1}) G[u.first][u.second][0] = v;
  103. go.push(u);
  104. }
  105. }
  106. if (G[v.first][v.second][2] == pii {-1, -1}){
  107. //сверху
  108. u = v;
  109. while (gd({u.first-1, u.second})){
  110. u.first--;
  111. }
  112. if (u != v){
  113. G[v.first][v.second][2] = u;
  114. if (G[u.first][u.second][3] == pii {-1,-1}) G[u.first][u.second][3] = v;
  115. go.push(u);
  116. }
  117. }
  118. if (G[v.first][v.second][3] == pii {-1, -1}){
  119. //снизу
  120. u = v;
  121. while (gd({u.first+1, u.second})){
  122. u.first++;
  123. }
  124. if (u != v){
  125. G[v.first][v.second][3] = u;
  126. if (G[u.first][u.second][2] == pii {-1,-1}) G[u.first][u.second][2] = v;
  127. go.push(u);
  128. }
  129.  
  130. }
  131. go.pop();
  132. }
  133. }
  134.  
  135. int main()
  136. {
  137. ios::sync_with_stdio(0);
  138. cin.tie(0);
  139. cout.tie(0);
  140.  
  141. //cin>>n>>m;
  142. n = 18;
  143. m = 14;
  144. dist.resize(n, vector <int> (m , inf));
  145. dsk.resize(n, vector <int>(m,0));
  146. for (int i=0;i<n;++i){
  147. for (int j=0;j<m;++j) continue ;//cin>>dsk[i][j];
  148. }
  149. //строим G
  150. //for (int i = 1; i <= 7;++i) dsk[i][0] = 1;
  151. for (int j = 5; j<=9;++j) dsk[0][j] = 1;
  152. for (int i=0;i<=7;++i) dsk[i][7] = 1;
  153. for (int i = 1;i<=3;++i) dsk[i][10] =1;
  154. for (int j = 1;j<=6;++j) dsk[9][j] = 1;
  155. for (int i=5;i<=9;++i) dsk[i][10] = 1;
  156. for (int j=8;j<=10;++j) dsk[9][j] = 1;
  157. for (int i=4;i<=8;++i) dsk[i][12] = 1;
  158. for (int j=3;j<=4;++j) dsk[13][j] = 1;
  159. vector <pii> pnt = { {14, 2}, {14, 5}, {15, 4}, {16, 6}, {12, 12}, {13, 11}, {14, 10}, {15, 9}, {15, 12}, {16, 11} };
  160. for (int i=0;i<pnt.size();++i){
  161. dsk[pnt[i].first][pnt[i].second] = 1;
  162. }
  163. shdsk();
  164. G.resize(n, vector <vector <pii> > (m, vector <pii> (4, {-1, -1})));
  165. mkG();
  166. shG();
  167.  
  168. }
  169. /*
  170. 4 5
  171. 0 0 0 0 1
  172. 0 1 1 0 2
  173. 0 2 1 2 0
  174. 0 0 1 0 0
  175. */
  176.  
Add Comment
Please, Sign In to add comment