题目
解析
从大本营进行bfs,记录到每个点的距离,记录每个参赛队到达大本营的距离,有相同距离的参赛队不为获胜者,具有单一距离且最小的参赛队为赢家
代码
#include <bits/stdc++.h>
using i64 = long long;
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int m, n;
cin >> m >> n;
int sx, sy;
vector<vector<int>> g(m, vector<int> (n));
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cin >> g[i][j];
if (g[i][j] == 2) {
sx = i, sy = j;
}
}
}
int dx[] = {0, 0, -1, 1}, dy[] = {1, -1, 0, 0};
vector<vector<int>> d(m, vector<int> (n, n + m));
d[sx][sy] = 0;
queue<pair<int, int>> Q;
Q.push({sx, sy});
while (Q.size()) {
auto t = Q.front();
Q.pop();
int x = t.first, y = t.second;
for (int i = 0; i < 4; i++) {
int tx = x + dx[i], ty = y + dy[i];
if (tx >= 0 && tx < m && ty >= 0 && ty < n && d[tx][ty] == n + m && g[tx][ty] == 1) {
d[tx][ty] = d[x][y] + 1;
Q.push({tx, ty});
}
}
}
int k;
cin >> k;
int t = -1, dis = -1;
vector<int> cnt(n + m + 1);
cnt[n + m] = 1;
vector<array<int, 2>> team(k + 1);
for (int i = 1; i <= k; i++) {
cin >> team[i][0] >> team[i][1];
team[i][0]--;
team[i][1]--;
auto o = team[i];
cnt[d[o[1]][o[0]]]++;
}
for (int i = 1; i <= k; i++) {
auto o = team[i];
if (cnt[d[o[1]][o[0]]] == 1 && (dis == -1 || dis > d[o[1]][o[0]])) {
t = i;
dis = d[o[1]][o[0]];
}
}
if (t == -1) {
cout << "No winner.";
} else {
cout << t << " " << dis;
}
return 0;
}