자기개발👨‍💻/코딩 알고리즘

[C++] 백준 1018 체스판 다시 칠하기

천숭이 2022. 3. 23. 14:48

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

 

1018번: 체스판 다시 칠하기

첫째 줄에 N과 M이 주어진다. N과 M은 8보다 크거나 같고, 50보다 작거나 같은 자연수이다. 둘째 줄부터 N개의 줄에는 보드의 각 행의 상태가 주어진다. B는 검은색이며, W는 흰색이다.

www.acmicpc.net

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

//               검은색 먼저   흰색 먼저 
int arr[55][55], board1[8][8], board2[8][8];

int main(){
	int n,m, result = 64;
	int chk1=0, chk2=0;
	cin >> n>> m;
	for(int i=0;i<n;i++){
		string s;
		cin>>s;
		for(int j=0;j<m;j++){
			if (s[j] == 'B') arr[i][j] = 1;
			else arr[i][j] = 0;
		}
	}
	
	for(int i=0;i<8;i++) for(int j=0;j<8;j++){
		board1[i][j] = (i+j+1)%2;
		board2[i][j] = (i+j)%2; 
	}
	
	// 확 인 
//	for(int i=0;i<n;i++){
//		for(int j=0;j<m;j++){
//			cout<<arr[i][j]<<" ";
//		}
//		cout<<endl;
//	}

//	for(int i=0;i<8;i++){
//		for(int j=0;j<8;j++){
//			cout<<board1[i][j]<<" ";
//		}
//		cout<<endl;
//	} 
	
	for(int i=0;i+8<=n;i++){
		for(int j=0;j+8<=m;j++){
			chk1 = 0; chk2 = 0;
			// board1,2 비교
			for(int k=0;k<8;k++){
				for(int l=0;l<8;l++){
					if(arr[i+k][j+l] != board1[k][l]) chk1++;
					if(arr[i+k][j+l] != board2[k][l]) chk2++;
				}
			}
			result = min(min(chk1, chk2), result);
		}
	}
	
	cout<<result;
	
}