csp回收站选址

本文介绍了一种基于地图的搜索算法实现方法,通过使用C++语言和标准模板库STL中的map容器来存储地图上的节点信息,并实现了对于指定节点周围邻居节点的搜索计数功能。该算法能够统计出在特定条件下的可达节点数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接
在这里插入图片描述

#include <iostream>
#include <map>

using namespace std;

int n;
int score[5] = {0};

int select(map< pair<int,int> , int>mp,pair<int,int>h){
    int sum = 0;
    pair<int,int>t[4];
    t[0].first = h.first + 1;   t[0].second = h.second;

    t[1].first = h.first;   t[1].second = h.second + 1;

    t[2].first = h.first - 1;   t[2].second = h.second;

    t[3].first = h.first;   t[3].second = h.second - 1;
    if(mp[t[0]] == 1 && mp[t[1]] == 1 && mp[t[2]] == 1 && mp[t[3]] == 1)
    {
        t[0].first = h.first + 1;   t[0].second = h.second + 1;

        t[1].first = h.first + 1;   t[0].second = h.second - 1;

        t[2].first = h.first - 1;   t[2].second = h.second + 1;

        t[3].first = h.first - 1;   t[3].second = h.second - 1;

        for (int i = 0; i < 4; ++i) {
            if(mp[t[i]] == 1)
                sum++;
        }

        return sum;
    }
    return -1;
}


int main() {
    ios::sync_with_stdio(false);
    cin>>n;

    pair<int,int>h[n];
    map<pair<int,int>,int>mp;
    string str;
    for (int i = 0; i < n; ++i) {
        cin>>h[i].first>>h[i].second;
        mp[h[i]] = 1;
    }
    for (int i = 0; i < n; ++i) {
        int res = select(mp,h[i]);
        if (res != -1)
            score[res]++;
    }
    for (int i = 0; i < 5; ++i) {
        cout<<score[i]<<endl;
    }
    return 0;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值