728x90
๋ฐ์ํ
https://softeer.ai/practice/info.do?idx=1&eid=580
Softeer
์ฐ์ต๋ฌธ์ ๋ฅผ ๋ด์ Set์ ์ ํํด์ฃผ์ธ์. ์ทจ์ ํ์ธ
softeer.ai
๋ฌธ์
ํ์ด
BFS๋ฌธ์ .
์ฌ์ ์ ์ ์๋ ๊ต์ฐจ๋ก ์ ๋ณด ์ ์ฅํ๊ณ ํ๋์ฉ ๋ฐฉ๋ฌธํด์ ๊ฐฏ์ count
์ฒ์์ 20์ ์ด ๋์ค๋๋ฐ ํ๋ฆฐ ๋ถ๋ถ์ ์ฐพ๋๋ผ ์ค๋ ๊ฑธ๋ฆผ. ๋ฐฉ๋ฌธํ๋ ๊ณณ์ด์ด๋ ๋ค์ ๋ฐฉ๋ฌธํด๋ ๊ด์ฐฎ์์
import sys
from collections import deque
from pprint import pprint
N, T = map(int, input().split())
intersection = [[0] + [] for i in range(N + 1)]
for i in range(1, N + 1) :
for j in range(1, N + 1) :
intersection[i].append(list(map(int, input().split())))
visited = [[0] * (N + 1) for _ in range(N + 1)]
go = [[0, 1], [0, -1], [1, 0], [-1, 0]] # ๋,์,๋จ,๋ถ
heading = { # ๊ฐ ๊ต์ฐจ๋ก์์ ์ด์ ์งํ ๋ฐฉํฅ
1:go[0], 2:go[3], 3:go[1], 4:go[2],
5:go[0], 6:go[3], 7:go[1], 8:go[2],
9:go[0], 10:go[3], 11:go[1], 12:go[2]
}
signals = { # ์ ํธ ์ ๋ณด
1:[3,0,2], 2:[1,3,0], 3:[3,1,2], 4:[1,2,0],
5:[3,0], 6:[1,3], 7:[1,2], 8:[2,0],
9:[0,2], 10:[3,0], 11:[3,1], 12:[1,2]
}
def BFS(start) :
queue = deque()
queue.append([start, go[3], 0]) # [ํ์ฌ ๊ต์ฐจ๋ก, ์ด์ ์ heading๋ฐฉํฅ, ์๊ฐ]
while queue :
now, prev, t = queue.popleft()
visited[now[0]][now[1]] = 1
signal = intersection[now[0]][now[1]][t % 4]
if prev != heading[signal] : # ์ง์
๋ฐฉํฅ๊ณผ ๊ต์ฐจ๋ก ์งํ ๋ฐฉํฅ์ด ๋ค๋ฅธ ๊ฒฝ์ฐ
continue
move = signals[signal] # ์
๋ ฅ๋ฐ์ ๊ต์ฐจ๋ก์ ์ ํธ
for m in move : # ๊ฐ ์ ํธ์ ๊ฒฝ์ฐ
nx = now[0] + go[m][0]
ny = now[1] + go[m][1]
if nx < 1 or nx > N or ny < 1 or ny > N : # ๋ฒ์๋ฅผ ๋ฒ์ด๋๋ ๊ฒฝ์ฐ
continue
if not visited[nx][ny] and t+1 <= T : # ๋ฐฉ๋ฌธํ์ง ์์๊ฑฐ๋ ์ด๋ ๊ฒฝ๋ก ์๋ณด๋ค ์์ ๊ฒฝ์ฐ
queue.append([[nx,ny], go[m], t + 1])
tmp = 0
for i in range(len(visited)) :
tmp += visited[i].count(1)
return tmp
print(BFS([1,1]))
์ฝ๋
import sys
from collections import deque
from pprint import pprint
N, T = map(int, input().split())
intersection = [[0] + [] for i in range(N + 1)]
for i in range(1, N + 1) :
for j in range(1, N + 1) :
intersection[i].append(list(map(int, input().split())))
visited = [[0] * (N + 1) for _ in range(N + 1)]
go = [[0, 1], [0, -1], [1, 0], [-1, 0]] # ๋,์,๋จ,๋ถ
heading = { # ๊ฐ ๊ต์ฐจ๋ก์์ ์ด์ ์งํ ๋ฐฉํฅ
1:go[0], 2:go[3], 3:go[1], 4:go[2],
5:go[0], 6:go[3], 7:go[1], 8:go[2],
9:go[0], 10:go[3], 11:go[1], 12:go[2]
}
signals = { # ์ ํธ ์ ๋ณด
1:[3,0,2], 2:[1,3,0], 3:[3,1,2], 4:[1,2,0],
5:[3,0], 6:[1,3], 7:[1,2], 8:[2,0],
9:[0,2], 10:[3,0], 11:[3,1], 12:[1,2]
}
def BFS(start) :
queue = deque()
queue.append([start, go[3], 0]) # [ํ์ฌ ๊ต์ฐจ๋ก, ์ด์ ์ heading๋ฐฉํฅ, ์๊ฐ]
while queue :
now, prev, t = queue.popleft()
visited[now[0]][now[1]] = 1
signal = intersection[now[0]][now[1]][t % 4]
if prev != heading[signal] : # ์ง์
๋ฐฉํฅ๊ณผ ๊ต์ฐจ๋ก ์งํ ๋ฐฉํฅ์ด ๋ค๋ฅธ ๊ฒฝ์ฐ
continue
move = signals[signal] # ์
๋ ฅ๋ฐ์ ๊ต์ฐจ๋ก์ ์ ํธ
for m in move : # ๊ฐ ์ ํธ์ ๊ฒฝ์ฐ
nx = now[0] + go[m][0]
ny = now[1] + go[m][1]
if nx < 1 or nx > N or ny < 1 or ny > N : # ๋ฒ์๋ฅผ ๋ฒ์ด๋๋ ๊ฒฝ์ฐ
continue
if t < T : # ์ด๋ ๊ฒฝ๋ก ์๋ณด๋ค ์์ ๊ฒฝ์ฐ
queue.append([[nx,ny], go[m], t + 1])
tmp = 0
for i in range(len(visited)) :
tmp += visited[i].count(1)
return tmp
print(BFS([1,1]))
728x90
๋ฐ์ํ
'Programming > Softeer' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Softeer][level3] ์์๋๋ก ๋ฐฉ๋ฌธํ๊ธฐ (python) (0) | 2023.10.02 |
---|---|
[Softeer][level3] ๋ก๋ด์ด ์ง๋๊ฐ ๊ฒฝ๋ก (python) (0) | 2023.09.29 |
[Softeer][level4] ์ง์ฐ๋ ์์๋ฅผ ์ข์ํด (python) (0) | 2023.09.27 |
[Softeer][level3] ๊ฐ์์ค ๋ฐฐ์ (python) (0) | 2023.09.26 |
[Softeer][level3] GINI์ผ ๋์์ค (python) (0) | 2023.09.26 |