CCF2019-12-2回收站选址-STL扩展思路

// map实现回收站选址.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
//#include<bits/stdc++.h>
#include <iostream>
#include<set>
using namespace std;
struct node {
    int x, y;
    //有参构造函数
    node(int _x, int _y) :x(_x), y(_y) { };
    //重载小于运算符
    bool operator <(const node& a)const {
        if (x != a.x)return x < a.x;
        else return y < a.y;
    }
};
int n;
int num[5] = { 0 };
set<node>a;
//判断set中是否储存了相关元素
bool is(int x, int y)
{
    if (a.find(node(x, y)) != a.end())return true;
    else return false;
}
int main()
{
    cin >> n;
    int b, c;
    while (n)
    {
        cin >> b >> c;
        //直接把节点插入
        a.insert(node(b, c));
        n--;
    }
    //判断是否为回收站选址点
    //用迭代器进行set的遍历
    set<node>::iterator it;
    for (it = a.begin(); it != a.end(); it++)
    {
        int x = (*it).x, y = (*it).y;
        if (is(x, y + 1) && is(x, y - 1) && is(x + 1, y) && is(x - 1, y))
        {
            int nu = 0;
            if (is(x + 1, y + 1))nu++;
            if (is(x + 1, y - 1))nu++;
            if (is(x - 1, y + 1))nu++;
            if (is(x - 1, y - 1))nu++;
            num[nu]++;
        }
    }
    for (int i = 0; i < 5; i++)
    {
        cout << num[i] << endl;
    }
    return 0;
}


// 回收站选址.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
using namespace std;
const int MAXV = 1010;
int a[MAXV][2];
int num[5] = { 0 };
int n;
bool exist(int x, int y)
{
    for (int i = 0; i < n; i++)
    {
        if( a[i][0]==x&&  a[i][1]==y) return true;
    }
    return false;
}
bool judge(int n1,int n2){
    if (exist(n1, n2 + 1) && exist(n1, n2 - 1) && exist(n1 + 1, n2) && exist(n1 - 1, n2))return true;
    else return false;
}
int number(int n3,int n4 ){
    int num1 = 0;
    if (exist(n3 + 1, n4 + 1))num1++;
    if (exist(n3 + 1, n4 - 1))num1++;
    if (exist(n3 - 1, n4 + 1))num1++;
    if (exist(n3 - 1, n4 - 1))num1++;
    return num1;
}
int main()
{
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin>>a[i][0]>>a[i][1];
    }
    for (int i = 0; i < n; i++)
    {
        if (judge(a[i][0], a[i][1]))
        {
            num[number(a[i][0], a[i][1])]++;
       }
    }
    for (int i = 0; i < 5; i++)
    {
        if (i <= 3)cout << num[i] << endl;
        else cout << num[i];
    }
    return 0;

}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值