Post

[swea-D3] 1220. [S/W 문제해결 기본] 5일차 - Magnetic

# [swea-D3] 1220. [S/W 문제해결 기본] 5일차 - Magnetic

🙋‍♂️ 들어가며

이 문제는 소거 가능상태와 교착상태를 잘 이해해야한다.

이를 위해 위에서 아래로 검사하는 start_idx, 밑에서 위로 검사하는 end_idx를 사용하자

Desktop View

열을 고정해놓고 행만 움직이면 되겠다

1
2
start_idx = 0;
end_idx = leng-1;



다음은 배치 가능한 경우들이다

1
2
3
leng = 5;
start_idx = 0;
end_idx = leng-1; 

case-1

Desktop View

이 경우

1
2
start_idx = 0;
end_idx = -1;

case-2

Desktop View

이 경우

1
2
start_idx = 5;
end_idx = leng-1;

case-3

Desktop View

이 경우

1
2
start_idx = 3;
end_idx = 2;

소거가 가능하다.

case-4

Desktop View

이 경우

1
2
start_idx = 1;
end_idx = 4;

case-5

Desktop View

이 경우

1
2
start_idx = 0;
end_idx = 3




pair 구하기

Desktop View


범위는

1
start_idx ~ end_idx

그래서 첫값과 첫색깔을 고정한다음 2로 나누어 몫을 구하면 되겠다

1
2
3
pair_cnt = 0;
temp_color = arr[start_idx][c];
pair_cnt++;

그다음 구할 범위는

start_idx + 1 ~ end_idx

이 반복문에서 0이 아닌 상태에서 숫자가 바뀔때마다 pair_cnt++ 를 하면 되겠다

반복문이 종료된 이후 총횟수에 가산해주자

총 횟수 += pair_cnt / 2;


✅ 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package swea;


import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;

import java.util.Arrays;

public class Solution {	
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int T = 10;
		for (int tc = 1; tc < T+1; tc++) {
			int leng = Integer.parseInt(br.readLine());
			
			// 1. 배열 생성
			int[][] arr = new int[leng][leng];
			for (int r = 0; r < leng; r++) {
				String[] cols = br.readLine().split(" ");
				for (int c = 0; c < leng; c++) {
					arr[r][c] = Integer.parseInt(cols[c]);
				}
			}
			
			int total_cnt = 0;
			// 2. 탐색 시작
			for (int c = 0; c < leng; c++) {
				
				// 2-1. 맨 위에서부터 아래로 검사
				int start_idx = 0;
				for (int r = 0; r < leng; r++) {
					// 2-1-a. N극이 나올떄까지 검사
					if (arr[r][c] == 1) break;
					
					// 2-1-b. S극(파란색 2) 이나 빈칸(0)이면 계속 전진
					start_idx++;
				}
				
				// 2-2. 맨 아래에서부터 위로 검사
				int end_idx = leng-1;
				for (int r = leng-1; r >= 0; r--) {
					// 2-2-a. S극이 나올떄까지 검사
					if (arr[r][c] == 2) break;
					
					// 2-2-b. N극(빨간색 1) 이나 빈칸(0)이면 계속 전진
					end_idx--;
				}
				
				// 2-3. 만약 자성체들을 전부 소각할 수 있다면?
				// 붉은 자성체만 있거나
				// 푸른 자성체만 있거나
				// 둘다 있긴한데 소각이 가능하다면
				if (start_idx == leng || end_idx == -1 || start_idx > end_idx) {
					continue;
				}
				
				// 2-4. 교착상태라면?
				int pair_cnt = 0;
				int temp_color = arr[start_idx][c];
				pair_cnt++;
				for (int r = start_idx+1; r < leng; r++) {
					// 2-4-a. 만약 다음 숫자가 다르면, 빈칸은 불가능
					if (temp_color != arr[r][c] && arr[r][c] != 0) {
						pair_cnt++;
						temp_color = arr[r][c];
					}
				}
				
				// 2-5. 교착상태 검사 종료후 총 횟수 가산
				int temp_cnt = pair_cnt / 2;
				total_cnt += temp_cnt;
			}
			
			
			// 3. 출력
			System.out.println("#" + tc + " " + total_cnt);
			
		}
		
		
	}
}
This post is licensed under CC BY 4.0 by the author.

Trending Tags

도움이 필요하면
나를 눌러 구리🐻
챗봇
너굴
bot
어서와 구리 ʕ ·ᴥ·ʔ
무엇이 필요하셔 구리?

Trending Tags

도움이 필요하면
나를 눌러 구리🐻
챗봇
너굴
bot
어서와 구리 ʕ ·ᴥ·ʔ
무엇이 필요하셔 구리?