Skip to content

Commit 511fcd8

Browse files
authored
Merge pull request #88 from YoonYn9915/main
YoonYn9915 / 11월 3주차 / 2문제
2 parents 7f01545 + 98021c3 commit 511fcd8

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
def link(arr, dir, x, y):
2+
length = 0
3+
dx = [-1, 1, 0, 0]
4+
dy = [0, 0, -1, 1]
5+
while True:
6+
x += dx[dir]
7+
y += dy[dir]
8+
9+
# 전선이 전원까지 잘 연결되었으면 전선이 이어지는 부분을 표시하고 길이 반환
10+
if x < 0 or x >= N or y < 0 or y >= N:
11+
return length
12+
13+
# 전선이 중간에 다른 전선을 만났으면 이어지지 못하므로 길이 -1 반환
14+
if arr[x][y] != 0:
15+
return -1
16+
17+
arr[x][y] = 2
18+
length += 1
19+
20+
21+
def dfs(arr, coreNum, count, wire_length):
22+
global ans, max_cores
23+
24+
if coreNum == len(core):
25+
# 현재 연결된 코어 수가 더 많거나 코어 수가 같은데 전선 길이가 더 짧은 경우 답 갱신
26+
if count > max_cores or (count == max_cores and wire_length < ans):
27+
max_cores = count
28+
ans = wire_length
29+
return
30+
31+
x, y = core[coreNum]
32+
if x == 0 or x == N - 1 or y == 0 or y == N - 1:
33+
# 가장자리에 있는 코어는 전선이 필요 없으므로 바로 다음 코어 탐색
34+
dfs(arr, coreNum + 1, count + 1, wire_length)
35+
return
36+
37+
for dir in range(4):
38+
arr_copy = [row[:] for row in arr] # 백트래킹을 위해 복사
39+
length = link(arr_copy, dir, x, y)
40+
if length != -1:
41+
dfs(arr_copy, coreNum + 1, count + 1, wire_length + length)
42+
43+
# 현재 코어를 연결하지 않고 다음 코어 탐색
44+
dfs(arr, coreNum + 1, count, wire_length)
45+
46+
47+
testCaseNum = int(input())
48+
outputs = []
49+
50+
for test_case in range(1, testCaseNum + 1):
51+
N = int(input())
52+
arr = []
53+
core = []
54+
ans = float('inf')
55+
max_cores = 0
56+
57+
for i in range(N):
58+
arr.append(list(map(int, input().split())))
59+
for j in range(N):
60+
if arr[i][j] == 1:
61+
core.append((i, j))
62+
63+
dfs(arr, 0, 0, 0)
64+
outputs.append(f"#{test_case} {ans}")
65+
66+
for output in outputs:
67+
print(output)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from collections import deque
2+
3+
n = int(input())
4+
k = int(input())
5+
6+
graph = [[0] * n for _ in range(n)]
7+
dx = [0, 1, 0, -1]
8+
dy = [1, 0, -1, 0]
9+
10+
for i in range(k):
11+
a, b = map(int, input().split())
12+
graph[a - 1][b - 1] = 2
13+
14+
l = int(input())
15+
dirDict = dict()
16+
queue = deque()
17+
queue.append((0, 0))
18+
19+
for i in range(l):
20+
x, c = input().split()
21+
dirDict[int(x)] = c
22+
23+
x, y = 0, 0
24+
graph[x][y] = 1
25+
cnt = 0
26+
direction = 0
27+
28+
def turn(alpha):
29+
global direction
30+
if alpha == 'L':
31+
direction = (direction - 1) % 4
32+
else:
33+
direction = (direction + 1) % 4
34+
35+
36+
while True:
37+
cnt += 1
38+
x += dx[direction]
39+
y += dy[direction]
40+
41+
if x < 0 or x >= n or y < 0 or y >= n:
42+
break
43+
44+
if graph[x][y] == 2:
45+
graph[x][y] = 1
46+
queue.append((x, y))
47+
if cnt in dirDict:
48+
turn(dirDict[cnt])
49+
50+
elif graph[x][y] == 0:
51+
graph[x][y] = 1
52+
queue.append((x, y))
53+
tx, ty = queue.popleft()
54+
graph[tx][ty] = 0
55+
if cnt in dirDict:
56+
turn(dirDict[cnt])
57+
58+
else:
59+
break
60+
61+
print(cnt)

0 commit comments

Comments
 (0)