B. Vika and the Bridge

该文描述了一个算法问题,给定n个格子和k种颜色,每个格子有特定颜色,允许改变一次颜色。目标是找出改变哪个格子的颜色能使跨越不同颜色格子的数目达到最小。解决方案利用优先队列来存储每个颜色格子的距离,并计算最小最大跨越数。
摘要由CSDN通过智能技术生成

 题意:给出n个格子,k种颜色,只能走相同颜色的格子,有1次机会改变任意格子颜色。

输入:分别输入n和k,后输入格子代表颜色1<=Ci<=k。

输出:选哪种格子,跨越木板数目最大值最少

思路:采用优先队列,将选择每种格子所跨越木板数目存入优先队列,映射一下,这里注意队列的大小,是否为空,当时因为这个一直wa2.
 

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e18;
const int N = 1e6 + 100;
int n, m, t, k, ans;
int a[N], b[N];
priority_queue<int>c[N];
signed main()
{
	cin >> t;
	while (t--)
	{
		cin >> n >> k;
		for (int i = 0; i <= k; i++)
		{
			while (!c[i].empty()) c[i].pop();
		}
		ans = INF;
		for (int i = 1; i <= n; i++)
		{
			cin >> a[i];
			if (c[a[i]].empty())
			{
				c[a[i]].push(i - 1);
				b[a[i]] = i;
				ans++;
			}
			else
			{
				c[a[i]].push(i - b[a[i]] - 1);
				b[a[i]] = i;
			}
		}
		for (int i = 1; i <= k; i++) c[i].push(n - b[i]);
		for (int i = 1; i <= k; i++)
		{
			if (c[i].size() > 1)//一定要判断队列大小
			{
				int ad = c[i].top();
				c[i].pop();
				int bd = c[i].top();
				ans = min(ans, max(ad / 2, bd));
			}
		}
		cout << ans << endl;
	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用深度优先搜索(DFS)来解决这个问题。首先,我们可以将问题抽象为一个图,其中每个房间都是一个节点,相邻房间之间有边相连。 首先,我们需要定义一个辅助函数,用于检查给定的坐标是否在合法的范围内: ```cpp bool isValid(int x, int y, int n, int m) { return (x >= 1 && x <= n && y >= 1 && y <= m); } ``` 接下来,我们可以使用DFS来遍历所有可能的移动方式。我们从Vika的起始位置开始,依次向四个方向移动,然后递归地对每个朋友进行相同的操作。如果在递归过程中,任何一个朋友与Vika在同一个房间,则说明Vika会被抓住。 以下是完整的C++代码实现: ```cpp #include <iostream> #include <vector> using namespace std; bool isValid(int x, int y, int n, int m) { return (x >= 1 && x <= n && y >= 1 && y <= m); } bool dfs(int vx, int vy, vector<pair<int, int>>& friends, vector<vector<bool>>& visited, int n, int m) { if (vx == 0 && vy == 0) { // Vika has successfully escaped return true; } visited[vx][vy] = true; // Possible neighbors' coordinates vector<pair<int, int>> neighbors = {{vx-1, vy}, {vx+1, vy}, {vx, vy-1}, {vx, vy+1}}; for (auto neighbor : neighbors) { int nx = neighbor.first; int ny = neighbor.second; if (isValid(nx, ny, n, m) && !visited[nx][ny]) { bool caught = false; for (auto friendCoord : friends) { int fx = friendCoord.first; int fy = friendCoord.second; if (nx == fx && ny == fy) { caught = true; break; } } if (!caught && dfs(nx, ny, friends, visited, n, m)) { return true; } } } return false; } string canVikaEscape(int n, int m, int vx, int vy, vector<pair<int, int>>& friends) { vector<vector<bool>> visited(n+1, vector<bool>(m+1, false)); if (dfs(vx, vy, friends, visited, n, m)) { return "Yes"; } else { return "No"; } } int main() { int n, m, vx, vy, k; cin >> n >> m >> vx >> vy >> k; vector<pair<int, int>> friends(k); for (int i = 0; i < k; i++) { cin >> friends[i].first >> friends[i].second; } cout << canVikaEscape(n, m, vx, vy, friends) << endl; return 0; } ``` 该代码首先读取输入的n,m,vx,vy和k值。接下来,它读取k个朋友的坐标,并调用canVikaEscape函数来判断Vika是否能逃脱。最后,它输出结果。 希望这可以帮助到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值