Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. typedef pair<int, int> Pair;
  9. typedef pair<pair<int, int>, int> GP_Pair;
  10.  
  11. const int idx1[] = { 0,0,1,-1 };
  12. const int idx2[] = { 1,-1,0,0 };
  13.  
  14. int N, M, land_cnt, answer;
  15. int board[11][11];
  16. int visited[11][11];
  17.  
  18. int parent[7];
  19. vector<GP_Pair> bridges;
  20.  
  21. bool comp(const GP_Pair& p1, const GP_Pair& p2)
  22. {
  23. return p1.second < p2.second ? true : false;
  24. }
  25.  
  26. void init()
  27. {
  28. scanf("%d %d", &N, &M);
  29.  
  30. for (int i = 1; i <= 6; i++)
  31. parent[i] = i;
  32.  
  33. for (int i = 1; i <= N; i++)
  34. for (int j = 1; j <= M; j++)
  35. scanf("%d", &board[i][j]);
  36. }
  37.  
  38. void bfs(Pair start, int number)
  39. {
  40. queue<Pair>q;
  41. q.push(start);
  42. visited[start.first][start.second] = number;
  43.  
  44. while (q.empty() != true)
  45. {
  46. Pair pos = q.front();
  47. q.pop();
  48.  
  49. for (int i = 0; i < 4; i++)
  50. {
  51. Pair next = Pair(pos.first + idx1[i], pos.second + idx2[i]);
  52.  
  53. if (next.first<1 || next.first>N || next.second<1 || next.second>M)
  54. continue;
  55.  
  56. if (!visited[next.first][next.second] && board[next.first][next.second])
  57. {
  58. visited[next.first][next.second] = number;
  59. q.push(next);
  60. }
  61. }
  62. }
  63. }
  64.  
  65. void setLandNumber()
  66. {
  67. for (int i = 1; i <= N; i++)
  68. {
  69. for (int j = 1; j <= M; j++)
  70. {
  71. if (!visited[i][j] && board[i][j])
  72. bfs(Pair(i, j), ++land_cnt);
  73. }
  74. }
  75. }
  76.  
  77. void makeBridge(Pair pos)
  78. {
  79. for (int idx = 0; idx < 4; idx++)
  80. {
  81. Pair next = pos;
  82. int step = 0;
  83.  
  84. for (int j = 1; j <= 10; j++)
  85. {
  86. next = Pair(next.first + idx1[idx], next.second + idx2[idx]);
  87. step += 1;
  88.  
  89. if (next.first<1 || next.first>N || next.second<1 || next.second>M)
  90. break;
  91.  
  92. if (visited[next.first][next.second])
  93. {
  94. if (step > 2 && visited[next.first][next.second] != visited[pos.first][pos.second])
  95. {
  96. bridges.push_back(GP_Pair(make_pair(visited[pos.first][pos.second], visited[next.first][next.second]), step - 1));
  97. }
  98. break;
  99. }
  100. }
  101. }
  102. }
  103.  
  104. void setBridges()
  105. {
  106. for (int i = 1; i <= N; i++)
  107. {
  108. for (int j = 1; j <= M; j++)
  109. {
  110. if (board[i][j] != 0)
  111. makeBridge(Pair(i, j));
  112. }
  113. }
  114. }
  115.  
  116. int find_Parent(int idx)
  117. {
  118. if (parent[idx] == idx)
  119. return parent[idx];
  120.  
  121. return find_Parent(parent[idx]);
  122. }
  123.  
  124. void merge(int n1, int n2)
  125. {
  126. int p1 = find_Parent(n1);
  127. int p2 = find_Parent(n2);
  128.  
  129. if (p1 < p2)
  130. parent[p2] = p1;
  131. else
  132. parent[p1] = p2;
  133. }
  134.  
  135. bool setMST()
  136. {
  137. sort(bridges.begin(), bridges.end(), comp);
  138.  
  139. for (auto bridge : bridges)
  140. {
  141. int start = bridge.first.first;
  142. int end = bridge.first.second;
  143. int len = bridge.second;
  144.  
  145. if (find_Parent(start) != find_Parent(end))
  146. {
  147. merge(start, end);
  148. answer += len;
  149. }
  150. }
  151.  
  152. for (int i = 1; i <= land_cnt; i++)
  153. {
  154. if (find_Parent(i) != 1)
  155. return false;
  156. }
  157. return true;
  158. }
  159.  
  160. int main()
  161. {
  162. init();
  163. setLandNumber();
  164. setBridges();
  165.  
  166. if (setMST())
  167. printf("%d\n", answer);
  168. else
  169. printf("-1\n");
  170. return 0;
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement