CSP_201912-2_回收站选址

本文探讨2019年CSP中一道回收站选址问题的解决方案,强调使用直观算法而非STL,即便涉及最多1000个数据点。提供了一段满分且易于理解的C++代码实现。
摘要由CSDN通过智能技术生成

面对这道题,要保持自信。不用STL也可以解决,估量一下循环的代价是大还是小,不大的话就大胆做,这道题最多也就1000个点,不能怂。

满分代码(十分易懂):

#define _CRT_SECURE_NO_WARNINGS //如果scanf问题没处理好就加上
#include <bits/stdc++.h>

typedef struct {
	long x;
	long y;
	int count;
	int score;
}locate;

locate trash[1010];
int n;

void isUp(int j, long x,long y)
{
	if (x==0||y==0)
		return ;
	for (int i = 0; i < n; i++)
	{
		if (i != j) 
		{
			if (trash[i].x == x)
			{
				if (trash[i].y == y + 1)
				{
					trash[j].count += 1;
					return;
				}
			}
		}
	}
	return ;

}
void isDown(int j, long x, long y)
{
	if (x == 0 || y == 0)
		return;
	for (int i = 0; i < n; i++)
	{
		if (i != j)
		{
			if (trash[i].x == x)
			{
				if (trash[i].y == y - 1)
				{
					trash[j].count += 1;
					return;
				}
			}
		}
	}
	return;
}
void isLeft(int j, long x, long y)
{
	if (x == 0 || y == 0)
		return;
	for (int i = 0; i < n; i++)
	{
		if (i != j)
		{
			if (trash[i].y == y)
			{
				if (trash[i].x == x - 1)
				{
					trash[j].count += 1;
					return;
				}
			}
		}
	}
	return;
}
void isRight(int j, long x, long y)
{
	if (x == 0 || y == 0)
		return;
	for (int i = 0; i < n; i++)
	{
		if (i != j)
		{
			if (trash[i].y == y)
			{
				if (trash[i].x == x + 1)
				{
					trash[j].count += 1;
					return;
				}
			}
		}
	}
	return;
}

void is1(int j, long x, long y)
{
	if (x == 0 || y == 0)
		return;
	for (int i = 0; i < n; i++)
	{
		if (i != j)
		{
			if (trash[i].y == y + 1)
			{
				if (trash[i].x == x + 1)
				{
					trash[j].score += 1;
					return;
				}
			}
		}
	}
	return;
}
void is2(int j, long x, long y)
{
	if (x == 0 || y == 0)
		return;
	for (int i = 0; i < n; i++)
	{
		if (i != j)
		{
			if (trash[i].y == y + 1)
			{
				if (trash[i].x == x - 1)
				{
					trash[j].score += 1;
					return;
				}
			}
		}
	}
	return;
}
void is3(int j, long x, long y)
{
	if (x == 0 || y == 0)
		return;
	for (int i = 0; i < n; i++)
	{
		if (i != j)
		{
			if (trash[i].y == y - 1)
			{
				if (trash[i].x == x - 1)
				{
					trash[j].score += 1;
					return;
				}
			}
		}
	}
	return;
}
void is4(int j, long x, long y)
{
	if (x == 0 || y == 0)
		return;
	for (int i = 0; i < n; i++)
	{
		if (i != j)
		{
			if (trash[i].y == y - 1)
			{
				if (trash[i].x == x + 1)
				{
					trash[j].score += 1;
					return;
				}
			}
		}
	}
	return;
}

int main()
{
	int score[5];
	for (int i = 0; i < 5; i++)
	{
		score[i] = 0;
	}
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		long x, y;
		scanf("%ld %ld", &x, &y);
		trash[i].x = x;
		trash[i].y = y;
		trash[i].count = 0;
		trash[i].score = 0;
	}
	for (int i = 0; i < n; i++)
	{
		long x = trash[i].x;
		long y = trash[i].y;
		isUp(i, x, y);
		isDown(i, x, y);
		isLeft(i, x, y);
		isRight(i, x, y);
		if (trash[i].count == 4)
		{
			is1(i, x, y);
			is2(i, x, y);
			is3(i, x, y);
			is4(i, x, y);
			score[trash[i].score]++;
		}
	}
	for (int i = 0; i < 5; i++)
	{
		printf("%d\n", score[i]);
	}
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值