Skip to content

Commit fe25e18

Browse files
committed
Add: Brute Force boj 17471
1 parent df919c2 commit fe25e18

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
| [백준](https://www.acmicpc.net/problem/2960) | Etc(Eratos) | [2960](boj/etc/2960.py) | 실버4 | 22.03.18 |
136136
| [백준](https://www.acmicpc.net/problem/16434) | Binary Search | [16434](boj/binary_search/16434.py) | 골드4 | 22.03.25 |
137137
| [백준](https://www.acmicpc.net/problem/14225) | Brute Force | [14225](boj/brute_force/14225.py) | 실버1 | 22.04.04 |
138+
| [백준](https://www.acmicpc.net/problem/17471) | Brute Force | [17471](boj/brute_force/17471.py) | 골드4 | 22.04.19 |
138139

139140
> \*표시한 것은 힌트를 얻거나 해설을 참고했으므로 다시 풀어봐야함
140141

boj/brute_force/17471.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
'''
2+
풀이
3+
코드가... 상당히 부끄럽다;;😅
4+
최대 구간 범위가 10 이하이므로 완전 탐색으로 모든 구간 조합을 구해도 가능하다.
5+
6+
1. A구간이 가능한 모든 조합을 구한다.(구간 수는 1 ~ n - 1, 각 구간은 1구역 이상 포함해야하므로)
7+
2. A구간의 각 경우에서 bfs를 이용하여 포함된 구역들이 연결되어있는지 확인한다.
8+
이때, 연결되어있으면 포함된 전체 인원 수를 반환하고 아닌 경우 0을 반환해서
9+
각 구간에 대한 정보를 딕셔너리에 저장해둔다.(B구간이 연결되어 있는지와 B구간 인원 확인을 위함)
10+
3. 2에서 구한 딕셔너리를 순회하며 각 경우에서 A구간 총 인원과 B구간 총 인원을 구한다.
11+
두 구간 모두 총 인원이 0이 아닌 경우 둘의 차를 구하고 현재 값과 비교하여 최소값을 구한다.
12+
'''
13+
14+
import sys
15+
from collections import deque
16+
input = lambda: sys.stdin.readline().strip()
17+
18+
def solution():
19+
answer = 1000
20+
n = int(input())
21+
people = [0] + list(map(int, input().split()))
22+
23+
area = [[] for _ in range(n + 1)]
24+
for i in range(1, n + 1):
25+
cities = list(map(int, input().split()))
26+
if cities[0] != 0:
27+
area[i] = cities[1:]
28+
for j in cities[1:]:
29+
area[j].append(i)
30+
31+
areaA = []
32+
33+
def combinations(arr, start, l):
34+
if len(arr) == l:
35+
areaA.append(arr)
36+
37+
for i in range(start, n + 1):
38+
combinations(arr + [i], i + 1, l)
39+
40+
# A 구간 조합을 모두 구한다.
41+
for i in range(1, n):
42+
combinations([], 1, i)
43+
44+
def bfs(start, cities):
45+
q = deque([start])
46+
visited = [False] * (n + 1)
47+
visited[start] = True
48+
cnt, total = 0, 0
49+
50+
while q:
51+
cur = q.popleft()
52+
total += people[cur]
53+
cnt += 1
54+
55+
for a in area[cur]:
56+
if not visited[a] and a in set(cities):
57+
q.append(a)
58+
visited[a] = True
59+
60+
return 0 if cnt != len(cities) else total
61+
62+
# 각 A구간이 서로 연결됬는지 확인 후 연결된 경우 총 인원 수를, 아닌 경우 0을 저장한다.
63+
connected = {}
64+
for a in areaA:
65+
connected[tuple(a)] = bfs(a[0], a)
66+
67+
# A구간 총 인원이 0이 아닌 경우 B구간을 구해 연결됬는지 확인 후 차를 구한다.
68+
for k, v in connected.items():
69+
if v != 0:
70+
A, a_sum = set(k), v
71+
areaB = tuple([i for i in range(1, n + 1) if i not in A])
72+
b_sum = connected[areaB]
73+
if b_sum != 0:
74+
answer = min(answer, abs(a_sum - b_sum))
75+
76+
return answer if answer != 1000 else -1
77+
78+
if __name__ == '__main__':
79+
print(solution())

0 commit comments

Comments
 (0)