CSP_201912-2_回收站选址

该程序读取垃圾放置点的坐标,检查每个点的上下左右是否有其他点,如果四个方向都有,计算其周围左上、右上、左下、右下的点数并记录得分。

问题描述

在这里插入图片描述

思路

完整代码

#include<iostream>
using namespace std;

int main()
{
    int n;
    cin >> n;

    int pos[n][2];  //垃圾放置点
    for( int i = 0; i < n; i++)
    {
        cin >> pos[i][0] >> pos[i][1];
    }

    int score = 0;      //满足条件的选址的分数0~4
    int result[5] = {0};
    bool up = false;
    bool down = false;
    bool left = false;
    bool right = false;

    for( int i = 0; i < n; i++ )
    {
        int x = pos[i][0];
        int y = pos[i][1];

        score = 0;
        up = false;
        down = false;
        left = false;
        right = false;

        for( int j = 0; j < n; j++)
        {
            if( pos[j][0] == x && pos[j][1] == y+1)
                up = true;
            if( pos[j][0] == x && pos[j][1] == y-1)
                down = true;
            if( pos[j][0] == x-1 && pos[j][1] == y)
                left = true;
            if( pos[j][0] == x+1 && pos[j][1] == y)
                right = true;
        }

        if( up && down && left && right )
        {
            for( int j = 0; j < n; j++)
            {
                if( pos[j][0] == x-1 && pos[j][1] == y+1)   //左上
                    score++;
                if( pos[j][0] == x+1 && pos[j][1] == y+1)   //右上
                    score++;
                if( pos[j][0] == x-1 && pos[j][1] == y-1)   //左下
                    score++;
                if( pos[j][0] == x+1 && pos[j][1] == y-1)   //右下
                    score++;
            }
            result[score]++;
        }
    }

    for( int i = 0; i < 5; i++)
        cout << result[i] << endl;

    return 0;
}

提交截图

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值