![[백준 / BOJ] 1358번 하키 (C++, Python)](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FbvndBr%2FbtrJry6k73z%2F7t5aPhaETlzsFLaY48TiB1%2Fimg.png)
[백준 / BOJ] 1358번 하키 (C++, Python)◎ 자료구조와 알고리즘/백준(BOJ) 문제풀이2022. 6. 5. 12:00
반응형
링크 : https://www.acmicpc.net/problem/1358
1358번: 하키
첫째 줄에 수 W H X Y P가 주어진다. P는 선수의 수이다. W와 H는 100보다 작거나 같은 자연수이고, H는 짝수이다. X와 Y는 절댓값이 100보다 작거나 같은 정수이다. P는 최대 50인 자연수이다. 둘째 줄부
www.acmicpc.net
문제

문제 풀이
뭔가 복잡해보이지만, 조건을 나눠서 풀면 쉽게 풀리는 문제다.
- 사각형에 있을 경우
- 왼쪽 반원에 있을 경우
- 오른쪽 반원에 있을 경우
위 조건들로 만들어주었다.
C++ 상세 풀이
C++ 풀이 펼쳐보기
int calc_dist(int x, int y, int a, int b) {
return ((x - a) * (x - a)) + ((b - y) * (b - y));
}
for (int t = 0; t < P; ++t) {
int x, y, r = (H / 2) * (H / 2);
std::cin >> x >> y;
// 사각형 안에 있는지 판별
if (X <= x && x <= X + W && Y <= y && y <= Y + H)
cnt++;
// 왼쪽 반원에 있는지 판별
else if (calc_dist(X, Y + (H / 2), x, y) <= r)
cnt++;
// 오른쪽 반원에 있는지 판별
else if (calc_dist(X + W, Y + (H / 2), x, y) <= r)
cnt++;
}
핵심 코드는 위와 같다.
Python 상세 풀이
Python 풀이 펼쳐보기
def calc_dist(x, y, a, b):
return ((x - a) * (x - a)) + ((b - y) * (b - y))
# 사각형 안에 있는지 판별
if (X <= x and x <= X + W and Y <= y and y <= Y + H):
cnt += 1
# 왼쪽 반원에 있는지 판별
elif (calc_dist(X, Y + (H / 2), x, y) <= r):
cnt += 1
# 오른쪽 반원에 있는지 판별
elif (calc_dist(X + W, Y + (H / 2), x, y) <= r):
cnt += 1
핵심 코드는 위와 같다.
코드 전문
C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <algorithm> | |
#include <iostream> | |
#include <vector> | |
int calc_dist(int x, int y, int a, int b) { | |
return ((x - a) * (x - a)) + ((b - y) * (b - y)); | |
} | |
void init() { | |
std::ios_base::sync_with_stdio(false); | |
std::cin.tie(nullptr); | |
std::cout.tie(nullptr); | |
} | |
int main() { | |
init(); | |
int W, H, X, Y, P, cnt = 0; // 너비, 높이, x, y, 선수의 수 | |
std::cin >> W >> H >> X >> Y >> P; | |
for (int t = 0; t < P; ++t) { | |
int x, y, r = (H / 2) * (H / 2); | |
std::cin >> x >> y; | |
// 사각형 안에 있는지 판별 | |
if (X <= x && x <= X + W && Y <= y && y <= Y + H) | |
cnt++; | |
// 왼쪽 반원에 있는지 판별 | |
else if (calc_dist(X, Y + (H / 2), x, y) <= r) | |
cnt++; | |
// 오른쪽 반원에 있는지 판별 | |
else if (calc_dist(X + W, Y + (H / 2), x, y) <= r) | |
cnt++; | |
} | |
std::cout << cnt << "\n"; | |
return 0; | |
} |
Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
input = sys.stdin.readline | |
def calc_dist(x, y, a, b): | |
return ((x - a) * (x - a)) + ((b - y) * (b - y)) | |
cnt = 0 | |
W, H, X, Y, P = map(int, input().split()) | |
for t in range(P): | |
x, y = map(int, input().split()) | |
r = (H / 2) ** 2 | |
# 사각형 안에 있는지 판별 | |
if (X <= x and x <= X + W and Y <= y and y <= Y + H): | |
cnt += 1 | |
# 왼쪽 반원에 있는지 판별 | |
elif (calc_dist(X, Y + (H / 2), x, y) <= r): | |
cnt += 1 | |
# 오른쪽 반원에 있는지 판별 | |
elif (calc_dist(X + W, Y + (H / 2), x, y) <= r): | |
cnt += 1 | |
print(cnt) |
소감
반응형
'◎ 자료구조와 알고리즘 > 백준(BOJ) 문제풀이' 카테고리의 다른 글
[백준 / BOJ] 1037번 약수 (C++, Python) (0) | 2022.06.06 |
---|---|
[백준 / BOJ] 5086번 배수와 약수 (C++, Python) (0) | 2022.06.06 |
[백준 / BOJ] 1004번 어린 왕자 (C++, Python) (0) | 2022.06.05 |
[백준 / BOJ] 1002번 터렛 (C++, Python) (0) | 2022.06.04 |
[백준 / BOJ] 2477번 참외밭 (C++, Python) (0) | 2022.06.03 |
@Reo :: 코드 아카이브
자기계발 블로그