๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 1018_์ฒด์ŠคํŒ ๋‹ค์‹œ ์น ํ•˜๊ธฐ

๋ฟŒ์•ผ._. 2024. 4. 19. 11:36

Silver IV

๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/1018)

< ์ฒด์ŠคํŒ ๋‹ค์‹œ ์น ํ•˜๊ธฐ >

 

๋ฌธ์ œ ํ’€์ด 

 

๋ณด๋“œ๋ฅผ 8x8 ํฌ๊ธฐ์˜ ์ฒด์ŠคํŒ์œผ๋กœ ์ž˜๋ผ 'B'๋กœ ์‹œ์ž‘ํ•  ๋•Œ์™€ 'W'๋กœ ์‹œ์ž‘ํ•  ๋•Œ๋ฅผ ๊ฐ๊ฐ ๊ณ„์‚ฐํ•˜์—ฌ ๋‹ค์‹œ ์น ํ•ด์•ผ ํ•˜๋Š” ์ •์‚ฌ๊ฐํ˜• ๊ฐœ์ˆ˜์˜ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•œ๋‹ค.

 

 my solution (Java)

import java.io.*;
import java.util.*;

public class _1018_ { // ์ฒด์ŠคํŒ ๋‹ค์‹œ ์น ํ•˜๊ธฐ

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(bf.readLine());

		int n = Integer.parseInt(st.nextToken());
		int m = Integer.parseInt(st.nextToken());

		char arr[][] = new char[n][m];
		for (int i = 0; i < n; i++) {
			String str = bf.readLine();
			for (int j = 0; j < m; j++) {
				arr[i][j] = str.charAt(j);
			}
		}

		char temp[][] = new char[8][8];
		int result = n * m;
		for (int i = 0; i <= n - 8; i++) {
			for (int j = 0; j <= m - 8; j++) {
				for (int x = i; x < i + 8; x++) {
					for (int y = j; y < j + 8; y++) {
						temp[x - i][y - j] = arr[x][y];
					}
				}
				int cnt = 0;
				char c[] = new char[] { 'B', 'W' };
				int idx = 0;

				for (int x = 0; x < 8; x++) {
					if (x % 2 == 0) {
						idx = 0;
					} else {
						idx = 1;
					}
					for (int y = 0; y < 8; y++) {
						if (x % 2 == 0) {
							if (temp[x][y] != c[idx++]) {
								cnt += 1;
							}
							if (idx == 2) {
								idx = 0;
							}
						} else {
							if (temp[x][y] != c[idx--]) {
								cnt += 1;
							}
							if (idx == -1) {
								idx = 1;
							}
						}
					}
				}
				result = Math.min(result, cnt);

				cnt = 0;
				for (int x = 0; x < 8; x++) {
					if (x % 2 == 0) {
						idx = 1;
					} else {
						idx = 0;
					}
					for (int y = 0; y < 8; y++) {
						if (x % 2 == 0) {
							if (temp[x][y] != c[idx--]) {
								cnt += 1;
							}
							if (idx == -1) {
								idx = 1;
							}
						} else {
							if (temp[x][y] != c[idx++]) {
								cnt += 1;
							}
							if (idx == 2) {
								idx = 0;
							}
						}
					}
				}
				result = Math.min(result, cnt);
			}
		}
		System.out.println(result);
	}
}
๋ณ€์ˆ˜)
n, m : ๋ณด๋“œ ํฌ๊ธฐ
arr : ๋ณด๋“œ ๊ฐ’
str : ๋ณด๋“œ ์ƒํƒœ (ํ–‰)
temp : 8x8 ์ฒด์ŠคํŒ
result : ๋‹ค์‹œ ์น ํ•ด์•ผ ํ•˜๋Š” ์ •์‚ฌ๊ฐํ˜• ๊ฐœ์ˆ˜ ์ตœ์†Ÿ๊ฐ’
cnt : ๋‹ค์‹œ ์น ํ•ด์•ผ ํ•˜๋Š” ์ •์‚ฌ๊ฐํ˜• ๊ฐœ์ˆ˜
c : ['B', 'W']
idx : index

 

๋ณด๋“œ์˜ ํฌ๊ธฐ๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ๋ณด๋“œ์˜ ์ •๋ณด๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ arr์— ์ €์žฅํ•œ๋‹ค. arr๋ฅผ 8x8 ํฌ๊ธฐ๋งŒํผ ์ž˜๋ผ temp ๋ฐฐ์—ด์— ์ €์žฅํ•œ๋‹ค. ๋‹ค์‹œ ์น ํ•ด์•ผ ํ•˜๋Š” ์ •์‚ฌ๊ฐํ˜• ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ ๊ณผ์ •์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

 

'B'๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฒฝ์šฐ

1) ํ–‰%2๊ฐ€ 0์ผ ๊ฒฝ์šฐ - idx๋ฅผ 0์œผ๋กœ ์ €์žฅํ•œ๋‹ค.

1-1) c์˜ idx๊ฐ’๊ณผ ์ผ์น˜ํ•˜์ง€ ์•Š์œผ๋ฉด cnt ๊ฐ’์„ 1 ์ฆ๊ฐ€ํ•œ๋‹ค.

1-2) idx ๊ฐ’์ด ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋Š” ๊ฒฝ์šฐ idx๋ฅผ 0์œผ๋กœ ์ €์žฅํ•œ๋‹ค.

2) ํ–‰%2๊ฐ€ 1์ผ ๊ฒฝ์šฐ - idx๋ฅผ 1๋กœ ์ €์žฅํ•œ๋‹ค.

2-1) c์˜ idx ๊ฐ’๊ณผ ์ผ์น˜ํ•˜์ง€ ์•Š์œผ๋ฉด cnt ๊ฐ’์„ 1 ์ฆ๊ฐ€ํ•œ๋‹ค.

2-2) idx ๊ฐ’์ด ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋Š” ๊ฒฝ์šฐ idx๋ฅผ 1๋กœ ์ €์žฅํ•œ๋‹ค.

result ๊ฐ’์„ ํ˜„์žฌ ๊ฐ’๊ณผ cnt ๊ฐ’ ์ค‘์—์„œ ์ตœ์†Ÿ๊ฐ’์œผ๋กœ ์ €์žฅํ•œ๋‹ค.

 

'W'๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฒฝ์šฐ 

1) ํ–‰%2๊ฐ€ 0์ผ ๊ฒฝ์šฐ - idx๋ฅผ 1๋กœ ์ €์žฅํ•œ๋‹ค.

1-1) c์˜ idx๊ฐ’๊ณผ ์ผ์น˜ํ•˜์ง€ ์•Š์œผ๋ฉด cnt ๊ฐ’์„ 1 ์ฆ๊ฐ€ํ•œ๋‹ค.

1-2) idx ๊ฐ’์ด ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋Š” ๊ฒฝ์šฐ idx๋ฅผ 1๋กœ ์ €์žฅํ•œ๋‹ค.

2) ํ–‰%2๊ฐ€ 1์ผ ๊ฒฝ์šฐ - idx๋ฅผ 0์œผ๋กœ ์ €์žฅํ•œ๋‹ค.

2-1) c์˜ idx ๊ฐ’๊ณผ ์ผ์น˜ํ•˜์ง€ ์•Š์œผ๋ฉด cnt ๊ฐ’์„ 1 ์ฆ๊ฐ€ํ•œ๋‹ค.

2-2) idx ๊ฐ’์ด ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋Š” ๊ฒฝ์šฐ idx๋ฅผ 0์œผ๋กœ ์ €์žฅํ•œ๋‹ค.

result ๊ฐ’์„ ํ˜„์žฌ ๊ฐ’๊ณผ cnt ๊ฐ’ ์ค‘์—์„œ ์ตœ์†Ÿ๊ฐ’์œผ๋กœ ์ €์žฅํ•œ๋‹ค.

 

์ตœ์ข… result ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.


๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•œ ํ›„์— ํ’€์ด ๋ฐฉ๋ฒ•์„ ์ฐพ์•„๋ณด๋‹ˆ 'B'๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฒฝ์šฐ์™€ 'W'๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฒฝ์šฐ ๋‘˜ ์ค‘์— ํ•˜๋‚˜๋งŒ ๊ตฌํ•œ ํ›„ 64์—์„œ ๊ทธ ๊ฐ’์„ ๋นผ๋ฉด ๋‹ค๋ฅธ ํ•˜๋‚˜์˜ ๊ฒฝ์šฐ ๊ฐ’์ด ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์—ˆ๋‹ค.