문제풀이

백준 1343 폴리오미노 C++

명도환 2022. 7. 11. 23:19

문제

민식이는 다음과 같은 폴리오미노 2개를 무한개만큼 가지고 있다. AAAA와 BB

이제 '.'와 'X'로 이루어진 보드판이 주어졌을 때, 민식이는 겹침없이 'X'를 모두 폴리오미노로 덮으려고 한다. 이때, '.'는 폴리오미노로 덮으면 안 된다.

폴리오미노로 모두 덮은 보드판을 출력하는 프로그램을 작성하시오.

입력

첫째 줄에 보드판이 주어진다. 보드판의 크기는 최대 50이다.

출력

첫째 줄에 사전순으로 가장 앞서는 답을 출력한다. 만약 덮을 수 없으면 -1을 출력한다.

풀이

'.'이 나올때까지의 사이즈를 통해 폴리오미노를 출력해 주었다. 가지고있는 폴리노미오로 출력이 불가능 하다면 -1을 출력해 주었다.

코드

#include <iostream>

using namespace std;

int main(void) {

	string str;
	bool check = false;
	int count = 0;
	int temp = 0;
	cin >> str;
	string A = "AAAA";
	string B = "BB";
	string sol = "";
	for (int i = 0; i < str.size(); i++) {
		temp++;
		if (str[i] == '.') {
			temp -=1;

			
			if (temp % 4 == 0 || temp % 2 == 0) {

				for (int j = 0; j < temp / 4; j++) {
					//cout << A;
					sol += A;
				}
				temp = temp % 4;


				for (int j = 0; j < temp / 2; j++) {
					//cout << B;
					sol += B;
				}
				temp = temp % 2;
				
			}

			if (temp == 0) {
				//cout << str[i];
				sol += str[i];
			}
			else {
				cout << -1;
				check = true;
				break;
			}
			temp = 0;
			
		}
		else if (i == str.size() - 1) {
			
			if (temp % 4 == 0 || temp % 2 == 0) {

				for (int j = 0; j < temp / 4; j++) {
					//cout << A;
					sol += A;
				}
				temp = temp % 4;


				for (int j = 0; j < temp / 2; j++) {
					//cout << B;
					sol += B;
				}
				temp = temp % 2;

			}
			else {
				cout << -1;
				check = true;
				break;
			}
			
		}
		
		
	}
	if(!check)cout << sol << endl;
	return 0;
}