๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm๐Ÿฐ/๋ฐฑ์ค€

[๋ฐฑ์ค€] 1018 ์ฒด์ŠคํŒ ๋‹ค์‹œ ์น ํ•˜๊ธฐ brute force

by Jouureee 2022. 2. 17.

๋ฌธ์ œ :

https://www.acmicpc.net/problem/1018

 

1018๋ฒˆ: ์ฒด์ŠคํŒ ๋‹ค์‹œ ์น ํ•˜๊ธฐ

์ฒซ์งธ ์ค„์— N๊ณผ M์ด ์ฃผ์–ด์ง„๋‹ค. N๊ณผ M์€ 8๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 50๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ๋ณด๋“œ์˜ ๊ฐ ํ–‰์˜ ์ƒํƒœ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. B๋Š” ๊ฒ€์€์ƒ‰์ด๋ฉฐ, W๋Š” ํฐ์ƒ‰์ด๋‹ค.

www.acmicpc.net

 

ํ’€์ด :

์ธํ’‹์œผ๋กœ ์ฃผ์–ด์ง„ ๋ฐฐ์—ด๊ณผ ์ฃผ์–ด์ง„ 8x8 ๋ฐฐ์—ด๊ณผ cnn ๋ชจ๋ธ์ฒ˜๋Ÿผ ํ•„ํ„ฐ๋งํ•˜๋“ฏ์ด ๋น„๊ตํ•˜๋Š” ๋ฌธ์ œ์˜€๋‹ค. ๋”ฐ๋ผ์„œ ๋ธŒ๋ฃจํŠธํฌ์Šค๋กœ ๋น„๊ตํ•  ์ฒด์ŠคํŒ์˜ ๋ฒ”์œ„๋ฅผ ๋จผ์ € ์ •ํ•˜๊ณ , ๋ธ”๋ž™์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ฒด์ŠคํŒ or ํฐ์ƒ‰์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ฒด์ŠคํŒ ๋‘๊ฐ€์ง€ ๊ฒฝ์šฐ์˜ ์ˆ˜์™€ ๋Œ€์กฐํ•˜์—ฌ min ๊ฐ’์„ ์ฐพ์•„๋‚ธ๋‹ค.

 

์šฐ์„  for๋ฌธ์ด ๋งŽ๋‹ค๋Š” ์ ์— ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚ ๊นŒ ๊ฑฑ์ •ํ–ˆ์ง€๋งŒ 

์ฒด์ŠคํŒ์˜ ํฌ๊ธฐ M x N ์™€ (50 x 50)๊ณผ ๋งŒ๋“  ์ฒด์ŠคํŒ์˜ ํฌ๊ธฐ 8 x 8์„ ๋‘๋ฒˆ ๋น„๊ตํ•˜๋ฏ€๋กœ ์ด

50 x 50 x 8 x 8 x 2 < 10^8 ์ด๋ฏ€๋กœ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚˜์ง€๋Š” ์•Š์•˜๋‹ค.

 

c++ ์ฝ”๋“œ : 

//
//  1018_reDrawChess.cpp
//  SOMA๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป
//
//  Created by JoSoJeong on 2021/09/10.
//

#include <stdio.h>
#include <iostream>

using namespace std;
int col, row;
string arr[51];

int min_val = 1000000;
string WB[8] = {
        "WBWBWBWB",
        "BWBWBWBW",
        "WBWBWBWB",
        "BWBWBWBW",
        "WBWBWBWB",
        "BWBWBWBW",
        "WBWBWBWB",
        "BWBWBWBW"
};
string BW[8] = {
        "BWBWBWBW",
        "WBWBWBWB",
        "BWBWBWBW",
        "WBWBWBWB",
        "BWBWBWBW",
        "WBWBWBWB",
        "BWBWBWBW",
        "WBWBWBWB"
};

int WB_cnt(int x, int y)
{
    int cnt = 0;
    for(int i = 0; i < 8; i++)
    {
        for(int j = 0; j < 8; j++)
        {
            if(arr[x+i][y+j] != WB[i][j])
                cnt++;
        }

    }
    return cnt;
}

int BW_cnt(int x, int y)
{
    int cnt = 0;
    for(int i = 0; i < 8; i++)
    {
        for(int j = 0; j < 8; j++)
        {
            if(arr[x+i][y+j] != BW[i][j])
                cnt++;
        }

    }
    return cnt;
}
int main() {
    cin >> col >> row;
    for(int i = 0; i < col; i++)
        cin >> arr[i];
    
    for(int i = 0; i + 8 <= col; i++)
    {
        for(int j = 0; j + 8 <= row; j++)
        {
            int tmp;
            tmp = min(WB_cnt(i,j),BW_cnt(i,j));
            if(tmp < min_val) {
                min_val = tmp;
            }
        }
    }
    cout << min_val << '\n';
    return 0;
}

๋Œ“๊ธ€