백준 1018 c++

magicdrill·2024년 2월 22일
0

백준 문제풀이

목록 보기
11/654

백준 1018 c++

#include <iostream>

using namespace std;

int input(int lower, int upper);
void input_chess_board(char** board, int col, int row);
int find_minimum_square(char** board, int col, int row);
int count_W_chess_board(char** board, int col, int row);
int count_B_chess_board(char** board, int col, int row);

int main(void)
{
	int N, M;
	int i;
	char** chess_board;

	N = input(8, 50);
	M = input(8, 50);
	chess_board = new char*[N];
	for (i = 0; i < N; i++)
	{
		chess_board[i] = new char[M];
	}

	input_chess_board(chess_board, N, M);
	cout << find_minimum_square(chess_board, N, M);


	for (i = 0; i < N; i++)
	{
		delete[] chess_board[i];
	}
	delete[] chess_board;

	return 0;
}

int input(int lower, int upper)
{
	int A;

	while (1)
	{
		cin >> A;
		if (A >= lower && A <= upper)
		{
			break;
		}
		else
		{
			;
		}
	}

	return A;
}

void input_chess_board(char **board, int col, int row)
{
	int i, j;
	char temp;

	for (i = 0; i < col; i++)
	{
		for (j = 0; j < row; j++)
		{
			while (1)
			{
				cin >> temp;
				if (temp == 'W' || temp == 'B')
				{
					break;
				}
				else
				{
					;
				}
			}
			board[i][j] = temp;
		}
	}

	return;
}

int find_minimum_square(char** board, int col, int row)
{
	int i, j;
	int count_W, count_B;
	int min = 64;

	for (i = 0; i <= col - 8; i++)
	{
		for (j = 0; j <= row - 8; j++)
		{
			count_W = count_W_chess_board(board, i, j);
			if (count_W < min)
			{
				min = count_W;
			}
			else 
			{
				;
			}
			count_B = count_B_chess_board(board, i, j);
			if (count_B < min)
			{
				min = count_B;
			}
			else
			{
				;
			}
		}
	}

	return min;
}

int count_W_chess_board(char** board, int col, int row)
{
	int count = 0, i, j;
	char W_chess[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'
	};

	for (i = col; i < col + 8; i++)
	{
		for (j = row; j < row + 8; j++)
		{
			if (board[i][j] == W_chess[i - col][j - row])
			{
				;
			}
			else
			{
				count++;
			}
		}
	}

	return count;
}

int count_B_chess_board(char** board, int col, int row)
{
	int count = 0, i, j;
	char B_chess[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'
	};

	for (i = col; i < col + 8; i++)
	{
		for (j = row; j < row + 8; j++)
		{
			if (board[i][j] == B_chess[i - col][j - row])
			{
				;
			}
			else
			{
				count++;
			}
		}
	}

	return count;
}

0개의 댓글