Baekjoon/Silver

Baekjoon 1018. 체스판 다시 칠하기 c++ [Silver IV]

Hun-bot 2024. 11. 6. 20:21
728x90
반응형

문제에서 "따라서 이 정의를 따르면 체스판을 색칠하는 경우는 두 가지뿐이다. 하나는 맨 왼쪽 위 칸이 흰색인 경우, 하나는 검은색인 경우이다" 이 부분에 대해서 생각해야 했고, 8x8고정 크기이기에 더 큰 체스판도 고정 크기로 움직이며 잘라내 계산해야 했다. 해당 코드가 main 부분에 있는 N-8과 M-8부분이다(N,M이 8이면 8x8크기가 고정되었기에 첫 번째 루프만 수행한다. 만약 N:10,M:12 -> 2랑 4만큼만 잘라내면서 비교하면 된다.) 

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int white_repainting(vector<string> CB,int x, int y){
    int repaints=0;
    for (int i = 0; i < 8; i++){
        for (int j = 0; j < 8; j++){
            char expected=((i+j)%2==0) ? 'W' : 'B';
            if(CB[x+i][y+j]!=expected) repaints++;
        }
    }
    return repaints;
}

int black_repainting(vector<string> CB,int x, int y){
    int repaints=0;
    for (int i = 0; i < 8; i++){
        for (int j = 0; j < 8; j++){
            char expected=((i+j)%2==0) ? 'B' : 'W';
            if(CB[x+i][y+j]!=expected) repaints++;
        }
    }
    return repaints;
}
int main() {
    int N,M;
    cin >> N >> M;
    vector<string> chess_board(N);
    for(int i=0;i<N;i++){
        cin >> chess_board[i];
    }
    
    int min_repaint=64;
    // check board
    for (int i = 0; i <= N-8; i++){
        for (int j = 0; j <= M-8; j++){
            int white_start=white_repainting(chess_board,i,j);
            int black_start=black_repainting(chess_board,i,j);

            min_repaint = min(min_repaint, min(white_start, black_start));
        }
    }
    cout << min_repaint << endl;
    return 0;
}

 

728x90
반응형