1
- // Authored by : BaaaaaaaaaaarkingDog
2
- // Co-authored by : -
3
- // http://boj.kr/****************
1
+ // Authored by : heheHwang
2
+ // Co-authored by : BaaaaaaaaaaarkingDog
3
+ // http://boj.kr/b318d03bfd694a57be91322ff1dcd4ef
4
4
#include < bits/stdc++.h>
5
5
using namespace std ;
6
6
7
- int main (void ){
7
+ bool mask[25 ];
8
+ string board[5 ];
9
+ int ans;
10
+ int dx[4 ] = {1 , 0 , -1 , 0 };
11
+ int dy[4 ] = {0 , 1 , 0 , -1 };
12
+ int main (void ) {
8
13
ios::sync_with_stdio (0 );
9
14
cin.tie (0 );
10
-
11
- }
15
+
16
+ for (int i = 0 ; i < 5 ; i++)
17
+ cin >> board[i];
18
+
19
+ // 25명중 칠공주가 될 사람의 후보 조합을 뽑습니다.
20
+ fill (mask + 7 , mask+25 , true );
21
+ do {
22
+ queue<pair<int , int >> q;
23
+ // 구성원 중 이다솜파의 수, 가로세로로 인접한 사람의 수
24
+ int dasom = 0 , adj = 0 ;
25
+ bool isp7[5 ][5 ] = {}, vis[5 ][5 ] = {};
26
+ for (int i = 0 ; i < 25 ; i++)
27
+ if (!mask[i]) {
28
+ int x = i / 5 , y = i % 5 ;
29
+ isp7[x][y] = true ;
30
+ if (q.empty ()) {
31
+ q.push ({x, y});
32
+ vis[x][y] = true ;
33
+ }
34
+ }
35
+ while (!q.empty ()) {
36
+ int x, y;
37
+ tie (x, y) = q.front ();
38
+ q.pop ();
39
+ adj++;
40
+ dasom += board[x][y] == ' S' ;
41
+ for (int k = 0 ; k < 4 ; k++) {
42
+ int nx = x + dx[k], ny = y + dy[k];
43
+ if (nx < 0 || nx >= 5 || ny < 0 || ny >= 5 || vis[nx][ny] || !isp7[nx][ny])
44
+ continue ;
45
+ q.push ({nx, ny});
46
+ vis[nx][ny] = true ;
47
+ }
48
+ }
49
+ ans += (adj >= 7 && dasom >= 4 );
50
+
51
+ } while (next_permutation (mask, mask + 25 ));
52
+ cout << ans;
53
+ }
54
+ /*
55
+ 25명 중 칠공주가 배치될 수 있는 모든 조합을 시도합니다.
56
+ 경우의 수가 많아보이지만, 25C7 = 480700이므로
57
+ 충분히 2초안에 수행될 수 있습니다.
58
+ 서로 가로세로로 인접해야 한다는 2번 조건은 여러가지 방법으로
59
+ 확인할 수 있으나, 본 풀이에서는 BFS를 이용하였습니다.
60
+ */
0 commit comments