PTA L3-037 夺宝大赛

题目

在这里插入图片描述

解析

从大本营进行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;
}
  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值