※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 5959번 문제인 Crop Circles이다.
문제는 아래 링크를 확인하자.

https://www.acmicpc.net/problem/5959 

 

5959번: Crop Circles

Bessie and her fellow herd-mates have become extremely territorial. The N (1 <= N <= 400) cows conveniently numbered 1..N have all staked out a grazing spot in the pasture. Each cow i has a spot on an integer grid (0 <= X_i <= 10,000; 0 <= Y_i <= 10,000) a

www.acmicpc.net

각 원에 대하여 다른 모든 원들과 한번씩 비교해 만나는지를 계산해 문제를 해결하자.

 

두 원의 반지름의 합과 두 원의 중점 사이의 거리를 비교하면 두 원이 서로 만나는지를 판별할 수 있다.

 

아래는 제출한 소스코드이다.

#include <iostream>
using namespace std;

struct cowfield {
	int x, y, r;
};

cowfield field[401];
int ans[401];

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	int N; cin >> N;
	for (int i = 0; i < N; i++) {
		int x, y, r; cin >> x >> y >> r;
		field[i].x = x, field[i].y = y, field[i].r = r;
	}

	for (int i = 0; i < N; i++) {
		for (int j = i + 1; j < N; j++) {
			int dx = field[j].x - field[i].x, dy = field[j].y - field[i].y;
			int rr = field[j].r + field[i].r;
			if (dx * dx + dy * dy < rr * rr) ans[i]++, ans[j]++;
		}
	}

	for (int i = 0; i < N; i++) cout << ans[i] << '\n';
}
728x90

+ Recent posts