본문 바로가기

코테문제

[백준] S2477

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

 

2477번: 참외밭

첫 번째 줄에 1m2의 넓이에 자라는 참외의 개수를 나타내는 양의 정수 K (1 ≤ K ≤ 20)가 주어진다. 참외밭을 나타내는 육각형의 임의의 한 꼭짓점에서 출발하여 반시계방향으로 둘레를 돌면서 지

www.acmicpc.net

 

# 아이디어

4종류의 모양으로 참외밭이 존재하므로

북:4, 서:2, 남:3, 동:1 일 때, 다음과 같은 순서로 4가지 모양이 존재함. 임의의 한 꼭짓점에서 반시계 방향으로 회전함.

4 -> 2 -> 3 -> 1 -> 3 -> 1 : ◲

4 -> 2 -> 3 -> 2 -> 3 -> 1 : ◱

3 -> 1 -> 4 -> 1 -> 4 -> 2 : ◳

3 -> 1 -> 4 -> 2 -> 4 -> 2 : ◰

 

큰 직사각형에서 작은 직사각형을 빼는 것으로 전체 넓이를 구하자.

6면체이기 때문에 동일한 방향이 존재할 때, 해당 방향의 값이 작은 직사각형의 넓이이다.

4와 3를 기준으로 1전후의 값이 큰 직사각형의 넓이이다.

 

=> 4가지 모양을 판별하고, 모양에 따라 len 배열을 정렬하여, 결과값 계산 후 결과 값 출력

 

 

모양 판별: cnt의 총합으로 구별한다.

		int cnt = 0;
		for (int i = 0; i < dir.length; i++) {
			if (dir[i] == 4) {
				cnt++;
			}
			if (dir[i] == 2) {
				cnt += 10;
			}
			if (dir[i] == 3) {
				cnt--;
			}
			if (dir[i] == 1) {
				cnt -= 10;
			}
		}

 

len 배열 정렬

		int[] newDir = new int[6];
		int[] newLen = new int[6];
		int[] temp1 = new int[6];
		int[] temp2 = new int[6];
		for (int i = 0; i < 6; i++) {
			if (cnt > 0) {
				if (cnt > 10) {
					if (dir[i] == 3) {
						temp1 = Arrays.copyOfRange(dir, 0, i);
						temp2 = Arrays.copyOfRange(dir, i, dir.length);
						newDir = IntStream.concat(Arrays.stream(temp2), Arrays.stream(temp1)).toArray();
						temp1 = Arrays.copyOfRange(len, 0, i);
						temp2 = Arrays.copyOfRange(len, i, len.length);
						newLen = IntStream.concat(Arrays.stream(temp2), Arrays.stream(temp1)).toArray();
					}
				} else {
					if (dir[i] == 4) {
						temp1 = Arrays.copyOfRange(dir, 0, i);
						temp2 = Arrays.copyOfRange(dir, i, dir.length);
						newDir = IntStream.concat(Arrays.stream(temp2), Arrays.stream(temp1)).toArray();
						temp1 = Arrays.copyOfRange(len, 0, i);
						temp2 = Arrays.copyOfRange(len, i, len.length);
						newLen = IntStream.concat(Arrays.stream(temp2), Arrays.stream(temp1)).toArray();
					}
				}
			} else if (cnt < 0) {
				if (cnt < -10) {
					if (dir[i] == 4) {
						temp1 = Arrays.copyOfRange(dir, 0, i);
						temp2 = Arrays.copyOfRange(dir, i, dir.length);
						newDir = IntStream.concat(Arrays.stream(temp2), Arrays.stream(temp1)).toArray();
						temp1 = Arrays.copyOfRange(len, 0, i);
						temp2 = Arrays.copyOfRange(len, i, len.length);
						newLen = IntStream.concat(Arrays.stream(temp2), Arrays.stream(temp1)).toArray();
					}
				} else {
					if (dir[i] == 3) {
						temp1 = Arrays.copyOfRange(dir, 0, i);
						temp2 = Arrays.copyOfRange(dir, i, dir.length);
						newDir = IntStream.concat(Arrays.stream(temp2), Arrays.stream(temp1)).toArray();
						temp1 = Arrays.copyOfRange(len, 0, i);
						temp2 = Arrays.copyOfRange(len, i, len.length);
						newLen = IntStream.concat(Arrays.stream(temp2), Arrays.stream(temp1)).toArray();
					}
				}
			}
		}

=> 이런 라이브러리말고 인덱스만을 활용하여 풀 수 있도록 노력하자 🤔

 

결과값 출력

		int result = 0;
		if (cnt > 0) {
			if (cnt > 10) {
				result = newLen[0] * newLen[1] - newLen[3] * newLen[4];
			} else {
				result = newLen[0] * newLen[5] - newLen[2] * newLen[3];
			}
		} else if (cnt < 0) {
			if (cnt < -10) {
				result = newLen[0] * newLen[1] - newLen[3] * newLen[4];
			} else {
				result = newLen[0] * newLen[5] - newLen[2] * newLen[3];
			}
		}

 

 

💯💯💯

처음에 방향과 길이를 배열에 저장하는데 이를 정렬해주는 과정에서 인덱스를 사용해야 하는데, 다른 방법에 매몰되어 규칙을 제대로 찾지 못했다. i = (i + ?) % arr.length; arr[i];

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

[백준] S25418  (0) 2024.01.25
[백준] S2477 다시 풀기  (0) 2024.01.05
[백준] S17086  (0) 2023.11.20
[백준] S2606  (1) 2023.11.12
[백준] S1260  (0) 2023.11.05