티스토리 뷰

알고리즘

- 완전탐색 문제.

- White와 Blue와 Red 전부 다 해보는 수밖에 없음.

- 3중 for문으로 count값만 계산하여 최소값 저장.

 

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

public class 러시아국기같은깃발 {
	static int T, N, M, cnt = Integer.MAX_VALUE, wcount,rcount,bcount;
	static char map[][];

	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		T = Integer.parseInt(br.readLine());

		for (int tc = 1; tc <= T; tc++) {
			StringTokenizer st = new StringTokenizer(br.readLine());
			N = Integer.parseInt(st.nextToken());
			M = Integer.parseInt(st.nextToken());
			map = new char[N][];
			for (int i = 0; i < N; i++) {
				map[i] = br.readLine().toCharArray();
			}

			wcount = 0;
			for (int w = 0; w < map.length - 2; w++) {
				for (int i = 0; i < M; i++) {
					if (map[w][i] != 'W') {
						++wcount;
					}
				}
				bcount = 0;
				for (int b = w + 1; b < map.length - 1; b++) {
					for (int i = 0; i < M; i++) {
						if (map[b][i] != 'B') {
							++bcount;
						}
					}
					rcount = 0;
					for (int r = b + 1; r < map.length; r++) {
						for (int i = 0; i < M; i++) {
							if (map[r][i] != 'R') {
								++rcount;
							}
						}
					}
					cnt = Math.min(cnt, wcount + bcount + rcount);
				}
			}
			System.out.printf("#%d %d\n",tc,cnt);
			cnt = Integer.MAX_VALUE;
//			print(map);
		}

	}

	private static void print(char[][] map) {
		for (int i = 0; i < map.length; i++) {
			for (int j = 0; j < map[i].length; j++) {
				System.out.print(map[i][j] + " ");
			}
			System.out.println();

		}
	}

}

'Algorithm' 카테고리의 다른 글

SW Expert Academy_상호의 배틀필드  (0) 2020.09.22
SW Expert Academy_햄버거 다이어트  (0) 2020.09.21
백준_플로이드_11404  (0) 2020.09.18
백준_경쟁적 전염_18405  (0) 2020.09.17
백준_다리만들기_2146  (0) 2020.09.16
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG more
«   2025/02   »
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
글 보관함