알고리즘 풀이

1018번 체스판 다시 칠하기

남생이야 2023. 12. 31. 16:43

 

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

 

1018번: 체스판 다시 칠하기

첫째 줄에 N과 M이 주어진다. N과 M은 8보다 크거나 같고, 50보다 작거나 같은 자연수이다. 둘째 줄부터 N개의 줄에는 보드의 각 행의 상태가 주어진다. B는 검은색이며, W는 흰색이다.

www.acmicpc.net

 

브루트포스를 이용해서 풀이해야했다. 구현을 어떻게 할까 고민을 하는 상태에선 고민이 많이 되었는데.. w스타트 b 스타트를 고려해야해서 둘다 비교하는 것을 구현하는 것으로 하였다. 

 

 

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <cmath>
using namespace std;

#define MAX 51

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

string chessMap[MAX];

int WstartCheck(int startI, int startJ)
{
    int count = 0; 
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 8; j++)
        {
            if (WB[i][j] != chessMap[startI + i][startJ + j])
                count++; 
        }
    }

    return count;
}
 
int BstartCheck(int startI, int startJ)
{
    int count = 0;
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 8; j++)
        {
            if (BW[i][j] != chessMap[startI + i][startJ + j])
                count++;
        }
    }

    return count;
}



int main() {

	int n, m;
	cin >> n >>  m; 
    cin.ignore();

    for (int i = 0; i < n; i++)
    {
        cin >> chessMap[i];
    }
    
    int minValue = 1000;
    for (int i = 0; i + 8 <= n; i++)
    {
        for (int j = 0; j + 8 <= m; j++)
        {
            minValue = min(minValue, min(WstartCheck(i,j) ,BstartCheck(i, j)));
        }
    }

    cout << minValue;

    return 0;
}