Algorithm, Data structure/Solved Algorithmic Problem

BAEKJOON 1018 - 체스판 다시 칠하기

JaykayChoi 2016. 8. 2. 00:00

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


더 좋은 풀이법이 있을 것 같긴하지만 완전 탐색으로도 충분히 시간내에 풀 수 있어 완전탐색으로 풀었습니다.



my solving

c++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <fstream>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
 
using namespace std;
 
const int MAX_N = 50;
 
int height, width;
//0 is black, 1 is white. means boards[y][x]
bool board[MAX_N][MAX_N];
 
int repaint(int startY, int startX)
{
    int ret = INT_MAX;
    for (int i = 0; i < 2; i++)
    {
        int count = 0;
        bool answer = i;
        for (int y = startY; y < startY + 8; y++)
        {
            if (y % != startY % 2)
                answer = !i;
            else
                answer = i;
            for (int x = startX; x < startX + 8; x++)
            {
                answer = !answer;
                if (board[y][x] != answer)
                    count++;
            }
        }
        ret = min(ret, count);
    }
    return ret;
}
 
int main()
{
    cin >> height >> width;
 
    for (int i = 0; i < height; i++)
    {
        string str;
        cin >> str;
        for (int j = 0; j < str.size(); j++)
            board[i][j] = str[j] == 'B' ? true : false;
    }
 
    int ret = INT_MAX;
 
    for (int y = 0; y <= height - 8; y++)
    {
        for (int x = 0; x <= width - 8; x++)
        {
            ret = min(ret, repaint(y, x));
        }
    }
 
    cout << ret << endl;
 
    return 0;
}
 
cs