그럴듯한 개발 블로그
반응형

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;
}
반응형
profile

그럴듯한 개발 블로그

@donghyk2

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!