Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #include <algorithm>
- #include <iostream>
- #include <iostream>
- #include <vector>
- #include <queue>
- using namespace std;
- int main()
- {
- ios_base::sync_with_stdio(0);
- cin.tie(0); cout.tie(0);
- int t;
- cin >> t;
- while (t--) {
- int n = 0, k = 0;
- cin >> n >> k;
- int arr[202][2] = { 0 };
- for (int i = 0; i < n; i++) {
- int x, y;
- cin >> x >> y;
- arr[i][0] = x;
- arr[i][1] = y;
- }
- vector<int> mine[202];
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- if (j != i) {
- int dis = abs(arr[i][0] - arr[j][0]) + abs(arr[i][1] - arr[j][1]);
- if (dis <= k) {
- mine[i].push_back(j);
- }
- }
- }
- }
- int min = 1000000;
- for (int i = 0; i < n; i++) {
- int visited[201] = { 0 };
- queue<pair<int, int>> q;
- int count = 0;
- pair<int, int> pa;
- pa.first = i;
- pa.second = 0;
- q.push(pa);
- visited[i] = 1;
- pair<int, int> pa2;
- while (!q.empty()) {
- pa2= q.front();
- q.pop();
- count++;
- int len =mine[pa2.first].size();
- for (auto it = mine[pa2.first].begin();
- it != mine[pa2.first].end(); it++) {
- if (visited[*it] == 0) {
- pair<int, int> pa3;
- pa3.first = *it;
- pa3.second = pa2.second + 1;
- q.push(pa3);
- visited[*it] = 1;
- }
- }
- }
- if (count == n) {
- if (pa2.second < min) {
- min = pa2.second;
- }
- }
- }
- if (min == 1000000) {
- cout << "-1\n";
- }
- else {
- cout << min << "\n";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement