# 백준 : 체스판 다시 칠하기(1018)

문제

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

소스

(Github: https://github.com/wonjnlee/wallydev/blob/master/bj_1018_recolorchessmap)

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cstring>
#include <list>
#include <cmath>
using namespace std;

int whitecase[8][8];
int blackcase[8][8];
int chessmap[50][50];
int M, N;

int makeresult(int x, int y) {
    int whiteresult = 0;
    int blackresult = 0;
    for(int i=x; i<x+8; i++) {
        for(int j=y; j<y+8; j++) {
            if(whitecase[i-x][j-y] != chessmap[i][j]) whiteresult++;
            if(blackcase[i-x][j-y] != chessmap[i][j]) blackresult++;
        }
    }
    return min(whiteresult, blackresult);
}

int main(void)
{
    string word;
    int result = 64;
    for(int i=0; i<8; i++) {
        for(int j=0; j<8; j++) {
            if((i + j)%2) {
                whitecase[i][j] = 1;
                blackcase[i][j] = 0;
            }
            else {
                whitecase[i][j] = 0;
                blackcase[i][j] = 1;
            }
        }
    }
    
    cin>>M>>N;
    for(int i=0; i<M; i++) {
        cin>>word;
        for(int j=0; j<N; j++) {
            if(word[j] == 'W')  chessmap[i][j] = 0;
            else                chessmap[i][j] = 1;
        }
    }
    
    for(int i=0; i<=(M-8); i++) {
        for(int j=0; j<=(N-8); j++) {
            result = min(result, makeresult(i, j));
        }
    }
    cout<<result<<endl;  
    return 0;
}

풀이

문제에 대한 이해는 일찌감치 했는데.
알고리즘 연습을 하면서 안좋은 습관이 생겼는데, 함수를 사용하지 않는다는 것이었다.
함수 없이 제한된 구역을 돌아가면서 값을 계산하려니 굉장히 "뻘짓"을 많이 하게 되었다.
그래서 정말 쉽게 함수로 구현할 수 있는 부분을 못구현하다보니 오류가 계속 났던거같은데..
우선 White와 Black을 각각 0, 1로 숫자처리하였다.
문자 비교보다는 정수 비교가 빠르다나뭐라나.. 무튼 그런식으로 바꿔주었고.
8 x 8 모양대로 돌아가면서 비교하여 가장 작은 수를 출력해주었다.
최대는 모두가 다른 64이기 때문에, 초기 최소값은 64로 지정하였다.

댓글