본문 바로가기

코테문제

[백준] S2477 다시 풀기

https://dekoms-coding.tistory.com/75

 

두 번째로 풀어보는 참외밭...!

 

package baekjoon;

import java.util.Scanner;

public class S2477_RE_TEST {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();

		int[] direction = new int[6];
		int[] length = new int[6];
		for (int i = 0; i < 6; i++) {
			direction[i] = sc.nextInt();
			length[i] = sc.nextInt();
		}

		int[] cnt = { 0, 0, 0, 0 };
		int startPoint = -1;
		for (int i = 0; i < direction.length; i++) {
			cnt[direction[i] - 1]++;
			if (direction[i] == 4 && direction[(i + 1) % 6] == 2 && direction[(i + 2) % 6] != 4) {
				startPoint = i;
			}
		}

		int bigSquare = 0, smallSqueare = 0;
		if (cnt[0] == 2) {
			if (cnt[2] == 2) {
				bigSquare = length[(startPoint) % 6] * length[(startPoint + 1) % 6];
				smallSqueare = length[(startPoint + 3) % 6] * length[(startPoint + 4) % 6];
			} else if (cnt[3] == 2) {
				bigSquare = length[(startPoint + 1) % 6] * length[(startPoint + 2) % 6];
				smallSqueare = length[(startPoint + 4) % 6] * length[(startPoint + 5) % 6];
			}
		} else if (cnt[1] == 2) {
			if (cnt[2] == 2) {
				bigSquare = length[(startPoint + 5) % 6] * length[(startPoint) % 6];
				smallSqueare = length[(startPoint + 2) % 6] * length[(startPoint + 3) % 6];
			} else if (cnt[3] == 2) {
				bigSquare = length[(startPoint + 2) % 6] * length[(startPoint + 3) % 6];
				smallSqueare = length[(startPoint + 5) % 6] * length[(startPoint) % 6];
			}
		}

		System.out.println(n * (bigSquare - smallSqueare));

		sc.close();

	}

}

처음에는 다음과 같이 풀었는데 if-else문이 너무 많이 반복되어서 중복을 제거하고자 함.

 

재서님이 코드 리뷰해주셨다!!

1. direction, length를 입력받을 때 maxHeight, maxWidth를 계산해두어서 불필요한 연산 없애기.

2. 꺾이는 부분 찾아서 minHeight, minWidth 계산. (lastDir, curDir, nextDir 느낌?)

 

훨씬 더 깔끔해진 코드~~~

코드 수정한 것

 

 

'코테문제' 카테고리의 다른 글

[백준] B1978  (0) 2024.01.25
[백준] S25418  (0) 2024.01.25
[백준] S2477  (0) 2023.12.02
[백준] S17086  (0) 2023.11.20
[백준] S2606  (1) 2023.11.12