CCF 2019.12 認證 -- 回收站選址

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述


#include<iostream>
#include <algorithm>
using namespace std;
struct Point {
	long int x;
	long int y;
}point[1000];

int comp(const Point& s1, const Point& s2) {
	if (s1.x == s2.x) {
		return s1.y < s2.y;
	}
	else {
		return s1.x < s2.x;
	}
}

bool Is_Find(long int x,long int y, Point point[],const int & total_num,int &gra) {
	int cnt = 0,grade = 0;
	for (int i = 0; i < total_num; ++i) {
		if (point[i].x == x && point[i].y == y + 1)//right
			++cnt;
		if (point[i].x == x && point[i].y == y - 1)//left
			++cnt;
		if (point[i].x == x + 1 && point[i].y == y)//down
			++cnt;
		if (point[i].x == x - 1 && point[i].y == y)//up
			++cnt;

		//求分數
		if (point[i].x == x + 1 && point[i].y == y + 1)//right_down
			++grade;
		if (point[i].x == x - 1 && point[i].y == y - 1)//left_up
			++grade;
		if (point[i].x == x + 1 && point[i].y == y - 1)//left_down
			++grade;
		if (point[i].x == x - 1 && point[i].y == y + 1)//right_up
			++grade;

		if (point[i].x >= x + 2)//後續不存在相鄰座標點
			break;
	}
	if (cnt == 4) {
		gra = grade;
		return true;
	}
	else
		return 0;
}

int main()
{
	int total_num = 0, grade[5] = { 0 };
	cin >> total_num;
	for(int i = 0;i < total_num;++i)
		cin >> point[i].x >> point[i].y;
	sort(point, point + total_num, comp);//從小達到排序
	//for (int i = 0; i < total_num; ++i)
	//	cout << point[i].x << " " << point[i].y << endl;
	for (int i = 0; i < total_num; ++i) {
		int x = point[i].x, y = point[i].y,gra = 0;
		if (Is_Find(x, y, point, total_num,gra))
			++grade[gra];
	}
	for (int i = 0; i <= 4; ++i)
		cout << grade[i] << endl;
	return 0;
}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章