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

 

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

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

 

24767번: Beehives

Input will be provided on multiple lines. Each case will begin with a floating point number d (0 < d < 1000.0), the distance within which hives will fight. On the next line will be N (1 ≤ N ≤ 100), the number of hives in that case. The next N lines wil

www.acmicpc.net

각 테스트케이스를 구성하는 벌집의 수가 100개 이하임을 관찰하자. 따라서 두 벌집의 쌍의 개수는 4,950개로 충분히 적음을 알 수 있다.

 

위의 관찰을 이용하면, 모든 두 벌집의 쌍을 조사하며 그 두 벌집의 거리가 D보다 작은지를 이용해 각 벌집이 시큼한 꿀을 생산할지의 여부를 알아내는 것으로 문제를 해결할 수 있다. 어떠한 벌집과도 그 거리가 D보다 작지 않다면 해당 벌집은 달콤한 꿀을 생산할 것이다.

 

부동소수점 오차가 걱정된다면 decimal 자료형을 지원하는 언어를 이용하거나 직접 구현하는 개선안을 생각해볼 수 있다.

 

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

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
typedef long double ld;

string s1, s2;
ld D; int N;

ld X[100], Y[100];
int ans[100];

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

	cin >> s1 >> s2;
	while (!(s1 == "0.0" && s2 == "0")) {
		memset(ans, 0, sizeof(ans));
		D = stold(s1), N = stoi(s2);
		int cnt = 0;
		for (int i = 0; i < N; i++) {
			cin >> X[i] >> Y[i];
			for (int j = 0; j < i; j++) {
				ld dx = X[i] - X[j], dy = Y[i] - Y[j];
				if (dx * dx + dy * dy < D * D) {
					if (!ans[i]) ans[i] = 1, cnt++;
					if (!ans[j]) ans[j] = 1, cnt++;
				}
			}
		}

		cout << cnt << " sour, " << N - cnt << " sweet\n";
		cin >> s1 >> s2;
	}
}
728x90

+ Recent posts