B.关鸡(2024牛客训练营)

 

 题目大意:至少添加多少着火点才能困住鸡

思路解析:
题目最终被分为两类情况,一类情况就是在鸡的周围添加三个着火点,另一类情况是在鸡周围添加

四个着火点

(火)(火)(火)(火)

注意添加四个着火点时应使得上下火的曼哈顿距离为1或2

建议使用set<pair<int,int>>v 容器来储存火的坐标,因为可以通过调用类方法count快速判断曼哈顿距离为1或2的火是否存在

废话不多说,代码如下:

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using T = pair<int, int>;
//set<int>S;
//unordered_map<int, int>mp;
const int N = 2e5 + 10;
int a[N];//另一种情况存鸡周围是否有着火点
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int t; cin >> t;
	while (t--) {
		int n; cin >> n;
		int i;
		for (i = 1; i <= 3; ++i)a[i] = 0;//初始化
		set<T>v;//储存着火点的坐标
		int l = 2, r = 2;//初始化假设左右两边另需俩着火点
		for (i = 1; i <= n; ++i) {
			int x, y;
			cin >> x >> y;
			v.insert({ x,y });
			if (y >= 0)r = 1;
			if (y <= 0)l = 1;
			if (x == 1 && y == -1)a[1] = 1;
			if (x == 2 && y == 0)a[2] = 1;
			if (x == 1 && y == 1)a[3] = 1;
		}
		for (auto val : v) {//枚举着火点
			int x = val.first;
			int y = val.second;
			if (x == 1) {
				if (v.count({ 2,y - 1 }) || v.count({ 2,y }) || v.count({ 2,y + 1 })) {
					if (y < 0)l = 0;
					if (y > 0)r = 0;
				}
			}
			else {
				if (v.count({ 1,y - 1 }) || v.count({ 1,y }) || v.count({ 1,y + 1 })) {
					if (y < 0)l = 0;
					if (y > 0)r = 0;
				}
			}
		}
		int ans = min(3 - (a[1] + a[2] + a[3]), l + r);
		cout << ans << endl;
	}
	return 0;
}

  • 9
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值