Advertisement
Guest User

Follow Up version B 2

a guest
Dec 7th, 2020
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. #include <math.h>
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <iostream>
  5. #include <vector>
  6. #include <queue>
  7. using namespace std;
  8. int main()
  9. {
  10. ios_base::sync_with_stdio(0);
  11. cin.tie(0); cout.tie(0);
  12. int t;
  13. cin >> t;
  14. while (t--) {
  15. int n = 0, k = 0;
  16. cin >> n >> k;
  17. int arr[202][2] = { 0 };
  18. for (int i = 0; i < n; i++) {
  19. int x, y;
  20. cin >> x >> y;
  21. arr[i][0] = x;
  22. arr[i][1] = y;
  23. }
  24. vector<int> mine[202];
  25.  
  26. for (int i = 0; i < n; i++) {
  27. for (int j = 0; j < n; j++) {
  28. if (j != i) {
  29. int dis = abs(arr[i][0] - arr[j][0]) + abs(arr[i][1] - arr[j][1]);
  30. if (dis <= k) {
  31. mine[i].push_back(j);
  32. }
  33. }
  34. }
  35. }
  36. int min = 1000000;
  37. for (int i = 0; i < n; i++) {
  38. int visited[201] = { 0 };
  39. queue<pair<int, int>> q;
  40. int count = 0;
  41. pair<int, int> pa;
  42. pa.first = i;
  43. pa.second = 0;
  44. q.push(pa);
  45. visited[i] = 1;
  46. pair<int, int> pa2;
  47. while (!q.empty()) {
  48. pa2= q.front();
  49. q.pop();
  50. count++;
  51. int len =mine[pa2.first].size();
  52. for (auto it = mine[pa2.first].begin();
  53. it != mine[pa2.first].end(); it++) {
  54. if (visited[*it] == 0) {
  55. pair<int, int> pa3;
  56. pa3.first = *it;
  57. pa3.second = pa2.second + 1;
  58. q.push(pa3);
  59. visited[*it] = 1;
  60. }
  61. }
  62. }
  63. if (count == n) {
  64. if (pa2.second < min) {
  65. min = pa2.second;
  66. }
  67. }
  68. }
  69. if (min == 1000000) {
  70. cout << "-1\n";
  71. }
  72. else {
  73. cout << min << "\n";
  74. }
  75. }
  76. }
  77.  
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement