Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <queue>
  5. using namespace std;
  6.  
  7. const int MAX_N = 1000;
  8. //vector<vector<int>> graph(MAX_N, vector<int>());
  9. int trees[MAX_N][MAX_N];
  10. int visited[MAX_N][MAX_N];
  11. int arr[MAX_N*MAX_N];
  12. int n,d;
  13.  
  14. void clearVisited()
  15. {
  16. for(int i = 0; i < n; ++i)
  17. {
  18. for(int j = 0; j < n; ++j)
  19. {
  20. visited[i][j] = 0;
  21. }
  22. }
  23. }
  24.  
  25. bool canBuild(int oldest)
  26. {
  27. queue<pair<int,int>> Q;
  28. queue<int> Q2;
  29.  
  30. for(int i = 0; i < n; ++i)
  31. {
  32. for(int j = 0; j < n; ++j)
  33. {
  34. if(!visited[i][j])
  35. {
  36. visited[i][j] = 1;
  37. Q.push(pair<int,int>(i,j));
  38. Q2.push(1);
  39.  
  40. while(!Q.empty())
  41. {
  42. pair<int,int> coords = Q.front();
  43. Q.pop();
  44. int volume = Q2.front();
  45. Q2.pop();
  46.  
  47. if(volume >= d)
  48. {
  49. clearVisited();
  50. return 1;
  51. }
  52.  
  53. if(coords.first > 0)
  54. {
  55. if(trees[coords.first-1][coords.second] <= oldest && !visited[coords.first-1][coords.second])
  56. {
  57. visited[coords.first-1][coords.second] = 1;
  58. Q.push(pair<int,int>(coords.first-1, coords.second));
  59. Q2.push(volume+1);
  60. }
  61. }
  62.  
  63. if(coords.first < n-1)
  64. {
  65. if(trees[coords.first+1][coords.second] <= oldest && !visited[coords.first+1][coords.second])
  66. {
  67. visited[coords.first+1][coords.second] = 1;
  68. Q.push(pair<int,int>(coords.first+1, coords.second));
  69. Q2.push(volume+1);
  70. }
  71. }
  72.  
  73. if(coords.second > 0)
  74. {
  75. if(trees[coords.first][coords.second-1] <= oldest && !visited[coords.first][coords.second-1])
  76. {
  77. visited[coords.first][coords.second-1] = 1;
  78. Q.push(pair<int,int>(coords.first, coords.second-1));
  79. Q2.push(volume+1);
  80. }
  81. }
  82.  
  83. if(coords.second < n-1)
  84. {
  85. if(trees[coords.first][coords.second+1] <= oldest && !visited[coords.first][coords.second-1])
  86. {
  87. visited[coords.first][coords.second+1] = 1;
  88. Q.push(pair<int,int>(coords.first, coords.second+1));
  89. Q2.push(volume+1);
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }
  96.  
  97. clearVisited();
  98. return 0;
  99. }
  100.  
  101. int main() {
  102. int w;
  103. cin >> n >> d;
  104. for(int i = 0; i < n; ++i)
  105. {
  106. for(int j = 0; j < n; ++j)
  107. {
  108. cin >> w;
  109. trees[i][j] = w;
  110. arr[i*n+j] = w;
  111. }
  112. }
  113.  
  114. sort(arr, arr + n*n);
  115.  
  116. int l = 0, r = n*n-1, mid;
  117. int res;
  118.  
  119. while(l <= r)
  120. {
  121. mid = (l+r)/2;
  122.  
  123. if(canBuild(mid))
  124. {
  125. r = mid - 1;
  126. res = mid;
  127. }
  128. else
  129. {
  130. l = mid + 1;
  131. }
  132. }
  133.  
  134. cout << mid;
  135.  
  136. return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement