2 분 소요

Question

1018Q


Solution

  • 2차원 배열로 입력 받기
  • ‘W’와 ‘B’로 시작하는 2차원배열의 체스판 선언 및 초기화
  • 입력받은 체스판을 8x8 크기로 자를 수있는 모든 경우의 수로 ‘W’와 ‘B’ 각각 한번씩 최솟값 찾아 출력

Cord

#include <iostream>

using namespace std;

char chess[51][51];

char wChess[8][8] =
{
	{'W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'},
	{'B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'},
	{'W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'},
	{'B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'},
	{'W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'},
	{'B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'},
	{'W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'},
	{'B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'},
};

char bChess[8][8] =
{
	{'B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'},
	{'W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'},
	{'B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'},
	{'W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'},
	{'B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'},
	{'W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'},
	{'B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'},
	{'W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'}
};

int wChessResult(int _i, int _j)
{
	int result = 0;
	for (int i = 0; i < 8; i++)
	{
		for (int j = 0; j < 8; j++)
		{
			if (chess[_i + i][_j + j] != wChess[i][j])
			{
				result++;
			}
		}
	}

	return result;
}

int bChessResult(int _i, int _j)
{
	int result = 0;
	for (int i = 0; i < 8; i++)
	{
		for (int j = 0; j < 8; j++)
		{
			if (chess[_i + i][_j + j] != bChess[i][j])
			{
				result++;
			}
		}
	}

	return result;
}

int main()
{
	int count = 64;
	int wCount = 0, bCount = 0;

	// 가로세로 크기 입력
	int row, column;
	cin >> column >> row;

	for (int i = 0; i < column; i++)
	{
		for (int j = 0; j < row; j++)
		{
			cin >> chess[i][j];
		}
	}

	// 8x8 최솟값 탐색
	for (int i = 0; i <= column - 8; i++)
	{
		for (int j = 0; j <= row - 8; j++)
		{
			// 첫 시작이 'W'와 'B' 경우 둘다 탐색
			wCount = wChessResult(i, j);
			bCount = bChessResult(i, j);

			// 최솟값 갱신
			if (wCount < bCount) 
			{
				count = (wCount < count) ? wCount : count;
			}
			else
			{
				count = (bCount < count) ? bCount : count;
			}
		}
	}

	cout << count;

	return 0;
}

Result

1018