๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 20529_๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์„ธ ์‚ฌ๋žŒ์˜ ์‹ฌ๋ฆฌ์  ๊ฑฐ๋ฆฌ

๋ฟŒ์•ผ._. 2023. 12. 13. 22:21

Silver I

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

< ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์„ธ ์‚ฌ๋žŒ์˜ ์‹ฌ๋ฆฌ์  ๊ฑฐ๋ฆฌ >

 

๋ฌธ์ œ ํ’€์ด 

 

์กฐํ•ฉ์„ ์‚ฌ์šฉํ•˜์—ฌ 3๋ช…์˜ ํ•™์ƒ์˜ MBTI ์„ฑ๊ฒฉ ์œ ํ˜•์„ ๋ฝ‘์•„ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์„ธ ์‚ฌ๋žŒ์˜ ์‹ฌ๋ฆฌ์  ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•œ๋‹ค.

 

์กฐํ•ฉ์œผ๋กœ๋งŒ ๋ฌธ์ œ๋ฅผ ๊ตฌํ˜„ํ•  ๊ฒฝ์šฐ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ์‹œ๊ฐ„์„ ์ค„์ผ ๋ฐฉ๋ฒ•์„ ๋ชฐ๋ผ ์ฐพ์•„๋ณด๋‹ˆ ๋น„๋‘˜๊ธฐ์ง‘ ์›๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด์—ˆ๋‹ค. ๋น„๋‘˜๊ธฐ์ง‘ ์›๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด MBTI๊ฐ€ ์ด 16๊ฐœ ์ด๋ฏ€๋กœ ๋งŒ์•ฝ ์‚ฌ๋žŒ ์ˆ˜๊ฐ€ 32๋ช…์ด๋ผ๋ฉด ๋˜‘๊ฐ™์€ MBTI๊ฐ€ 2๋ช…์”ฉ ์žˆ์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ ์‚ฌ๋žŒ ์ˆ˜๊ฐ€ 32๋ช…๋ณด๋‹ค ๋งŽ๋‹ค๋ฉด ๋˜‘๊ฐ™์€ MBTI๊ฐ€ ๋ฌด์กฐ๊ฑด 3๋ช…์ด ์žˆ๊ฒŒ ๋œ๋‹ค. ๊ทธ๋Ÿผ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์„ธ ์‚ฌ๋žŒ์˜ ์‹ฌ๋ฆฌ์  ๊ฑฐ๋ฆฌ๊ฐ€ 0์ด ๋˜๋ฏ€๋กœ ์‚ฌ๋žŒ ์ˆ˜๊ฐ€ 32๋ณด๋‹ค ํฌ๋‹ค๋ฉด ์กฐํ•ฉ์„ ๊ตฌํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

 

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;

public class _20529_ { // ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์„ธ ์‚ฌ๋žŒ์˜ ์‹ฌ๋ฆฌ์  ๊ฑฐ๋ฆฌ
	static String arr[], result[];
	static int answer;

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

		int t = Integer.parseInt(bf.readLine());

		for (int i = 0; i < t; i++) {
			int n = Integer.parseInt(bf.readLine());

			arr = new String[n];
			result = new String[n];
			answer = Integer.MAX_VALUE;

			st = new StringTokenizer(bf.readLine());
			for (int j = 0; j < n; j++) {
				arr[j] = st.nextToken();
			}

			if (n > 32) {
				answer = 0;
			} else {
				search(0, 0);
			}

			bw.write(answer + "\n");
		}
		bw.flush();
	}

	private static void search(int idx, int start) {
		if (idx == 3) {
			int temp = 0;
			for (int i = 0; i < 4; i++) {
				if (result[0].charAt(i) != result[1].charAt(i)) {
					temp += 1;
				}
				if (result[1].charAt(i) != result[2].charAt(i)) {
					temp += 1;
				}
				if (result[0].charAt(i) != result[2].charAt(i)) {
					temp += 1;
				}
			}
			answer = Math.min(temp, answer);
			return;
		}
		for (int i = start; i < arr.length; i++) {
			result[idx] = arr[i];
			search(idx + 1, i + 1);
		}
	}
}
๋ณ€์ˆ˜)
t : ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ˆ˜
n : ํ•™์ƒ์˜ ์ˆ˜
arr : ํ•™์ƒ์˜ MBTI
result : ์กฐํ•ฉ์œผ๋กœ ๊ตฌํ•œ 3๋ช…์˜ MBTI
answer : ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์„ธ ์‚ฌ๋žŒ์˜ ์‹ฌ๋ฆฌ์  ๊ฑฐ๋ฆฌ

 

ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ˆ˜, ํ•™์ƒ์˜ ์ˆ˜, ํ•™์ƒ์˜ MBTI๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ์ €์žฅํ•œ๋‹ค. ํ•™์ƒ์˜ ์ˆ˜๊ฐ€ 32๋ณด๋‹ค ํฌ๋‹ค๋ฉด ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์„ธ ์‚ฌ๋žŒ์˜ ์‹ฌ๋ฆฌ์  ๊ฑฐ๋ฆฌ๊ฐ€ 0์ด๋‹ค. 32๋ณด๋‹ค ํฌ์ง€ ์•Š๋‹ค๋ฉด ์กฐํ•ฉ์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด search ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

 

search ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์กฐํ•ฉ์œผ๋กœ 3๋ช…์„ ๋ฝ‘๋Š”๋‹ค. ๋งŒ์•ฝ ์กฐํ•ฉ์œผ๋กœ 3๋ช…์„ ๋‹ค ๊ตฌํ–ˆ๋‹ค๋ฉด A์™€ B ์‚ฌ์ด์˜ ์‹ฌ๋ฆฌ์ ์ธ ๊ฑฐ๋ฆฌ, B์™€ C ์‚ฌ์ด์˜ ์‹ฌ๋ฆฌ์ ์ธ ๊ฑฐ๋ฆฌ, A์™€ C ์‚ฌ์ด์˜ ์‹ฌ๋ฆฌ์ ์ธ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•œ ํ›„ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์„ธ ์‚ฌ๋žŒ์˜ ์‹ฌ๋ฆฌ์  ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•œ๋‹ค.