B.关鸡-牛客寒假训练营(一)

https://ac.nowcoder.com/acm/contest/67741/B

本题使用set<pair<int,int>>st来存放数据可以做到高效查找
考虑以下两种情况

情况一、火鸡左右分别被包围最多4堆火

在这里插入图片描述
或者上下两堆火间距不超过1
在这里插入图片描述

情况二、火鸡左右下被包住,最多3堆火

在这里插入图片描述

还有一种特殊情况,一侧被包围,加上(2,0)位置有火,这种情况特殊考虑,仅需加入一堆火即可,ans=min(1,ans)
在这里插入图片描述

各种情况的代码实现看以下注释

#include<iostream>
#include<set>
using namespace std;
void solve(){
    int n;cin>>n;
    set<pair<int,int>>st;
    //情况一,共有4堆火
    int l=2,r=2;
    for(int i=1;i<=n;i++){
        int x,y;cin>>x>>y;
        st.insert({x,y});
        if(y<0) l=1;
        if(y>0) r=1;
    }

    for(auto x:st){
        if(x.first==1){
            if(x.second<0){
                if(st.count({2,x.second})) l=0;
                if(st.count({2,x.second-1})) l=0;
                if(st.count({2,x.second+1})) l=0;
            }else if(x.second>0){
                if(st.count({2,x.second})) r=0;
                if(st.count({2,x.second-1})) r=0;
                if(st.count({2,x.second+1})) r=0;
            }
            
        }else{
            if(x.second<0){
                if(st.count({1,x.second})) l=0;
                if(st.count({1,x.second-1})) l=0;
                if(st.count({1,x.second+1})) l=0;
            }else if(x.second>0){
                if(st.count({1,x.second})) r=0;
                if(st.count({1,x.second-1})) r=0;
                if(st.count({1,x.second+1})) r=0;
            }
        }
    }
    int ans1=l+r;
    /***********************************************/
    //情况2,最多3堆火
    int ans2=3;
    if(st.count({2,0})) {
    //特殊情况***
        ans2--;
        if(l==0||r==0) ans1=min(ans1,1);
    }/*********/
    if(st.count({1,-1})) ans2--;
    if(st.count({1,1})) ans2--;
	/***********************************************/
	//输出两种情况的最小值即可
    cout<<min(ans1,ans2)<<endl;
}
int main(){
    int T;cin>>T;
    while(T--){
        solve();
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值