반응형
https://www.acmicpc.net/problem/1018
1018번: 체스판 다시 칠하기
첫째 줄에 N과 M이 주어진다. N과 M은 8보다 크거나 같고, 50보다 작거나 같은 자연수이다. 둘째 줄부터 N개의 줄에는 보드의 각 행의 상태가 주어진다. B는 검은색이며, W는 흰색이다.
www.acmicpc.net
옛날옛적 풀다가 어려워서 넘겼던 문제다. 이젠 190000
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<string> input;
int n, m;
int getDiffWhiteBoard(int startX, int startY) {
int res = 0;
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
int curX = x + startX;
int curY = y + startY;
if ((curX % 2 == 0 && curY % 2 == 0) || (curX % 2 == 1 && curY % 2 == 1)){
if (input[curX][curY] == 'B') // wbwbwb 일때 arr[x][y] 짝수 짝수 홀수 홀수
res++;
}
else {
if (input[curX][curY] == 'W')
res++;
}
}
}
return res;
}
int getDiffBlackBoard(int startX, int startY) {
int res = 0;
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
int curX = x + startX;
int curY = y + startY;
if ((curX % 2 == 0 && curY % 2 == 0) || (curX % 2 == 1 && curY % 2 == 1)){
if (input[curX][curY] == 'W') // wbwbwb 일때 arr[x][y] 짝수 짝수 홀수 홀수
res++;
}
else {
if (input[curX][curY] == 'B')
res++;
}
}
}
return res;
}
int main()
{
ios::sync_with_stdio(0),cin.tie(0);
cin >> n >> m;
int res = 2147483647;
for (int i = 0; i < n; i++) {
string cur;
cin >> cur;
input.push_back(cur);
}
for (int x = 0; x + 8 <= n; x++)
for (int y = 0; y + 8 <= m; y++)
res = min(res, min(getDiffBlackBoard(x, y), getDiffWhiteBoard(x, y)));
cout << res;
}
반응형
'<algorithm> > 백준' 카테고리의 다른 글
백준 9935 문자열 폭발 c++ (0) | 2023.09.12 |
---|---|
백준 11866 요세푸스 문제 0 c++ (0) | 2023.09.11 |
백준 1920 수 찾기 c++ (0) | 2023.09.11 |
백준 10816 숫자 카드 2 c++ (0) | 2023.09.11 |
백준 1620 (c++) 나는야 포켓몬 마스터 이다솜 (0) | 2023.04.06 |