728x90
반응형

문제 요약
- 알고리즘 분류: 기하학, 수학, 구현
- 문제난이도: Silver 3
- 문제 내용
- 1m × 1m 면적당 참외 K개 있음
- 동서남북 6번 이동한다
- 각 입력마다 동서남북 이동방향과 길이를 준다.
- 참외밭의 참외 개수를 구한다.
- 사이트 주소: https://www.acmicpc.net/problem/2477
2477번: 참외밭
첫 번째 줄에 1m2의 넓이에 자라는 참외의 개수를 나타내는 양의 정수 K (1 ≤ K ≤ 20)가 주어진다. 참외밭을 나타내는 육각형의 임의의 한 꼭짓점에서 출발하여 반시계방향으로 둘레를 돌면서 지
www.acmicpc.net
문제 풀이
- 각 가장진 가로, 세로 길이, 인덱스 저장할 변수를 선언한다.
- 가장긴 가로,세로 인덱스와 길이를 구한다.
- 가장긴 가로, 세로의 양쪽 인덱스 구한다
- why? 전체 면적 - 비어 있는 면적에서 비어 있는 가로, 세로는 가장 긴 가로 ,세로에서 그 다음 또는 이전에 이동한 곳은 없다.
- 가장 긴 가로, 가세에서 그 다음 또는 이전 이동한 곳을 빼면 비어있는 가로, 세로위치가 나온다.
- .전체 넓이에서 제외된 부분 넓이를 빼서 1m 면적당 참외 개수를 구한다.
Python
K = int(input())
max_width, max_height = (0, 0), (0, 0)
total = []
for i in range(6):
d, length = map(int, input().split())
if(d == 1 or d == 2):
if length > max_height[1]:
max_height = (i, length)
if(d == 3 or d == 4):
if length > max_width[1]:
max_width = (i, length)
total.append(length)
check = [(max_height[0] + 1) % 6, (max_height[0] - 1) % 6, (max_width[0] + 1) % 6, (max_width[0] - 1) % 6]
S = 1
for i in range(6):
if i not in check:
S *= total[i]
print(((max_width[1] * max_height[1]) - S) * K)
JAVA
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int[] total = new int[6];
int[] max_width = {0, 0};
int[] max_height = {0, 0};
int K = Integer.parseInt(br.readLine());
for(int i = 0; i < 6; i++) {
st = new StringTokenizer(br.readLine());
int dir = Integer.parseInt(st.nextToken());
int length = Integer.parseInt(st.nextToken());
if(dir == 1 || dir == 2) {
if(length > max_width[1]) {
max_width[0] = i;
max_width[1] = length;
}
}else if(dir == 3 || dir == 4) {
if(length > max_height[1]) {
max_height[0] = i;
max_height[1] = length;
}
}
total[i] = length;
}
Set<Integer> check = new HashSet<Integer>();
check.add((max_width[0] + 1) % 6);
check.add((max_width[0] + 5) % 6);
check.add((max_height[0] + 1) % 6);
check.add((max_height[0] + 5) % 6);
int S = 1;
for(int i = 0; i < 6; i++) {
if(!check.contains(i)) {
S *= total[i];
}
}
System.out.println(((max_width[1] * max_height[1]) - S) * K);
}
}
728x90
반응형
'알고리즘 > 백준' 카테고리의 다른 글
[BAEJOON] 5058 배수와 약수 (0) | 2022.09.06 |
---|---|
[BAEJOON] 1358 하키 (0) | 2022.08.26 |
[BAEJOON] 1004 어린 왕자 (0) | 2022.08.23 |
[BAEKJOON] 2738 행렬 덧셈 (0) | 2022.08.22 |
[BAEKJOON] 11478 서로 다른 부분 문자열의 개수 (0) | 2022.08.16 |