๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ์žฅ์›์ต ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ
๐Ÿ’ป Computer Science/- Data Structure, Algorithm

[์•Œ๊ณ ๋ฆฌ์ฆ˜ PS] ๋ฐฑ์ค€ 1018๋ฒˆ ์ฒด์ŠคํŒ ๋‹ค์‹œ ์น ํ•˜๊ธฐ ์ž๋ฐ” ๋ฌธ์ œํ’€์ด

by Wonit 2021. 2. 17.

๋ฌธ์ œ

ํ•ด๋‹น ํฌ์ŠคํŒ…์€ ๋ฐฑ์ค€์˜ ์ฒด์ŠคํŒ ๋‹ค์‹œ ์น ํ•˜๊ธฐ ์˜ ์ ‘๊ทผ๊ณผ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•œ ๊ธ€ ์ž…๋‹ˆ๋‹ค.
์ •๋‹ต ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•˜์‹œ๋ ค๋ฉด solve url ์—์„œ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผํ•ด์•ผ ํ•˜๋Š”์ง€๋ฅผ ๋จผ์ € ์ƒ๊ฐํ•ด๋ณด์ž.

๋ฌธ์ œ ์ ‘๊ทผ

์ด๋ฒˆ ๋ฌธ์ œ๋Š” Brute Force ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•ด์„œ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ด๋‹ค.

 

์ฒด์ŠคํŒ์€ 2๊ฐ€์ง€์˜ ์ข…๋ฅ˜๊ฐ€ ์กด์žฌํ•œ๋‹ค.

  1. B๊ฐ€ ๋จผ์ € ์˜ค๋Š” ๊ฒฝ์šฐ
  2. W๊ฐ€ ๋จผ์ € ์˜ค๋Š” ๊ฒฝ์šฐ

์ด ๋‘ ๊ฒฝ์šฐ์—์„œ ์ƒˆ๋กญ๊ฒŒ ์น ํ•ด์•ผ ํ•˜๋Š” ํŒ์˜ ์ตœ์†Œ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋ฉด ์ •๋‹ต์ด ๋œ๋‹ค.

ํ•ด๊ฒฐ๋ฒ•

๋‚˜๋Š” ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ W๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฒฝ์šฐ์™€ B๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฒฝ์šฐ์˜ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์—ˆ๋‹ค.


์ฒด์ŠคํŒ ์ „์ฒด๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๊ณ  ํ•˜๋‚˜์”ฉ ๋น„๊ตํ•ด๊ฐ€๋ฉฐ ๋‹ค๋ฅธ ๊ฒฝ์šฐ count ๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๋ฉด ๋œ๋‹ค.

String[][] bw = {
  { "BWBWBWBW" },
  { "WBWBWBWB" },
  { "BWBWBWBW" },
  { "WBWBWBWB" },
  { "BWBWBWBW" },
  { "WBWBWBWB" },
  { "BWBWBWBW" },
  { "WBWBWBWB" }
};

String[][] wb = {
  { "WBWBWBWB" },
  { "BWBWBWBW" },
  { "WBWBWBWB" },
  { "BWBWBWBW" },
  { "WBWBWBWB" },
  { "BWBWBWBW" },
  { "WBWBWBWB" },
  { "BWBWBWBW" }
};

 

ํ•˜์ง€๋งŒ ์–ด์ฐจํ”ผ WB๊ฐ€ ๋ฐ˜๋ณต๋  ๊ฒƒ์„ ์•Œ๊ธฐ ๋•Œ๋ฌธ์— 1์ฐจ์› ๋ฐฐ์—ด๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

 

char[] bw = {'B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'};
char[] wb = {'W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'};

์ •๋‹ต ์ฝ”๋“œ

public class Main {

    static int n;
    static int m;

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

        String[] nm = br.readLine().split(" ");

        n = Integer.parseInt(nm[0]);
        m = Integer.parseInt(nm[1]);

        char[][] arr =  new char[n][m];

        for (int i = 0; i < n; i++) {
            char[] line = br.readLine().toCharArray();
            for (int j = 0; j < m; j++) {
                arr[i][j] = line[j];
            }
        }
        int answer = 32;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                answer = Math.min(answer, countRepaintNum(arr, j, i));
            }
        }
        bw.write(String.valueOf(answer));
        bw.flush();
        bw.close();
    }

    private static int countRepaintNum(char[][] arr, int x, int y) {
        int count = 0;
        int xx = x + 7;
        int yy = y + 7;

        char[] bw = {'B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'};
        char[] wb = {'W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'};
        int jIndex;
        if(isValidPoint(xx, yy)) {
            for (int i = y; i <= yy; i++) {
                jIndex = 0;
                for (int j = x; j <= xx; j++) {
                    if(i % 2 != 0) {
                        if(arr[i][j] != bw[jIndex++]) count++;
                    }else {
                        if(arr[i][j] != wb[jIndex++]) count++;
                    }
                }
            }

            int temp = count;
            count = 0;
            for (int i = y; i <= yy; i++) {
                jIndex = 0;
                for (int j = x; j <= xx; j++) {
                    if(i % 2 != 0) {
                        if(arr[i][j] != wb[jIndex++]) count++;
                    }else {
                        if(arr[i][j] != bw[jIndex++]) count++;
                    }
                }
            }
            return Math.min(count, temp);
        }else return 100;
    }

    private static boolean isValidPoint(int x, int y) {
        return (0 <= x && 0 <= y && x < m && y < n);
    }
}

๋ฌธ์ œ ํšŒ๊ณ 

ํšŒ๊ณ 

์ •๋‹ต ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•˜์‹œ๋ ค๋ฉด solve url ์—์„œ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

๋Œ“๊ธ€