하다보니

1941번-소문난 칠공주 본문

알고리즘 풀이/백준

1941번-소문난 칠공주

claire 2022. 3. 11. 21:48
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<tuple>
using namespace std;

bool mask[25];
string board[5];
int ans;
int dx[4] = { 1,-1,0,0 };
int dy[4] = { 0,0,1,-1 };

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	for (int i = 0; i < 5; i++)
		cin >> board[i];

	fill(mask + 7, mask + 25, true);

	do {
		queue<pair<int, int>> q;
		int dasom = 0;
		int adj = 0;
		bool isp7[5][5] = {};
		bool vis[5][5] = {};
		for (int i = 0; i < 25; i++) {
			if (!mask[i]) {
				int x = i / 5;
				int y = i % 5;
				isp7[x][y] = true;
				if (q.empty()) {
					q.push({ x,y });
					vis[x][y] = true;
				}
			}
		}
		while (!q.empty()) {
			int x, y;
			tie(x, y) = q.front();
			q.pop();
			adj++;
			if (board[x][y] == 'S')dasom += 1;
			for (int i = 0; i < 4; i++) {
				int nx = x + dx[i];
				int ny = y + dy[i];
				if (nx < 0 || nx >= 5 || ny < 0 || ny >= 5 || !isp7[nx][ny] || vis[nx][ny])continue;
				q.push({ nx,ny });
				vis[nx][ny] = true;
			}
		}
		if (adj >= 7 && dasom >= 4) {
			ans++;
		}

	} while (next_permutation(mask, mask + 25));
	cout << ans;

}

'알고리즘 풀이 > 백준' 카테고리의 다른 글

9095번-1,2,3 더하기  (0) 2022.03.12
1463번 - 1로 만들기  (0) 2022.03.12
15656번-n과 m(7)  (0) 2022.03.09
15655번-n과 m(6)  (0) 2022.03.09
15654번-n과 m(5)  (0) 2022.03.08