-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
27-SeongHoonC #201
27-SeongHoonC #201
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
λ¬Έμ 보μλ§μ μ κ°μ μ¬μ΄ν΄μ λλ μ λ€μ μΉ΄μ΄ν νλ©΄ λκ² κ΅¬λ!
ν΄μ BFSλ₯Ό λ°λ‘ λ μ¬λ Έμλλ€.
κ·Όλ° μ΄μ
3 4 DLLL DRLL RRUL
κ°μ κ²½μ°λ μ΄λλ₯Ό λμ΄λ (1,1)
μ§μ μΌλ‘ μλ ΄νκΈ° λλ¬Έμ 1μ΄ λμμΌνλλ°,
(1,3)
, (2,3)
μ§μ μμ BFSλ₯Ό λ리면 λ μλ‘μ΄ μ¬μ΄ν΄λ‘ μΈμνκΈ° λλ¬Έμ visited
λ₯Ό μ΄μ©ν΄μ μ‘°κΈ λ μ²λ¦¬ν΄μ£Όμ΄μ νμμλλ€!
μ¬λ―Έμ§λ€ ν€ν€ν€νΌ
import sys
from collections import deque
N, M = map(int, input().split())
board = [list(input()) for _ in range(N)]
visited = set()
answer = 0
for row in range(N):
for col in range(M):
if (row, col) in visited: continue
answer += 1
deq = deque()
visited.add((row, col))
inner_visited = {(row, col),}
deq.append((row, col))
while deq:
now_row, now_col = deq.popleft()
new_row, new_col = now_row, now_col
if board[now_row][now_col] == "D":
new_row += 1
elif board[now_row][now_col] == "U":
new_row -= 1
elif board[now_row][now_col] == "L":
new_col -= 1
elif board[now_row][now_col] == "R":
new_col += 1
if (new_row, new_col) in inner_visited: break
if (new_row, new_col) in visited:
answer -= 1
break
visited.add((new_row, new_col))
inner_visited.add((new_row, new_col))
deq.append((new_row, new_col))
print(answer)
for (i in 0..3) { | ||
val nextX = now.first + dx[i] | ||
val nextY = now.second + dy[i] | ||
// λ²μ λμ΄κ° | ||
if (nextX >= n || nextY >= m || nextX < 0 || nextY < 0) continue | ||
if (visited[nextX][nextY]) continue | ||
// μ΄λ λ°©ν₯κ³Ό κ°μ λ or νμ¬ κ΅¬μμΌλ‘ μ΄λν κ³³ | ||
if (dDInverse[i] == graph[nextX][nextY] || i == nowDirection) { | ||
visited[nextX][nextY] = true | ||
q.add(nextX to nextY) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ€νΈλΌ μ£Όλ³μ νμν΄μ κ°μ μ¬μ΄ν΄μ λλ©΄ 체ν¬ν΄μ£Όλ μμΌλ‘ νμκΎΌμ... μ κΈ°λ°©κΈ°λ€
μ²μμλ μλμ κ°μ΄ BFSλ‘ νλλ° κ³μ νλ €μ μΌλ¨ μ μ λκ³ νλ¦° λΆλΆfrom collections import deque
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
graph = [list(input()) for _ in range(n)]
visited = [[0] * m for _ in range(n)]
result = 0
for y in range(n):
for x in range(m):
if(visited[y][x] == 1):
continue
queue = deque([(x,y)])
visited[y][x] = 1
while queue:
x, y = queue.popleft()
nx, ny = x, y
if graph[y][x] == "U":
ny -= 1
elif graph[y][x] == "D":
ny += 1
elif graph[y][x] == "L":
nx -= 1
elif graph[y][x] == "R":
nx += 1
if ny >= n or ny < 0 or nx >= m or nx < 0 :
continue
if visited[ny][nx] == 1:
result += 1
break
visited[ny][nx] = 1
queue.append((nx,ny))
print(result) μΌλ¨ DFSλ‘ λ°κΏμ μ¬μ΄ν΄ μΉ΄μ΄νΈ νλ λ°©μμΌλ‘ λ°κΏ¨μ΅λλ€! κΈ°λ³Έ λ‘μ§μ κ°μ§λ§ μ¬μ΄ν΄μ΄ μκΈ°λ©΄ result += 1μ νκ² νκ±°λ μ¬μ΄ν΄μ΄ μλλ©΄ λ€μ μμΉλ‘ DFSκ° λλλ‘ μ§ννμ΅λλ€. from collections import deque
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
graph = [list(input()) for _ in range(n)]
visited = [[0] * m for _ in range(n)]
result = 0
def dfs(x,y):
global result
visited[y][x] = 1
cycle.append([x, y])
if graph[y][x] == "U":
y -= 1
elif graph[y][x] == "D":
y += 1
elif graph[y][x] == "L":
x -= 1
elif graph[y][x] == "R":
x += 1
if visited[y][x] == 1:
if [x, y] in cycle:
result += 1
else :
dfs(x,y)
for y in range(n):
for x in range(m):
if(visited[y][x] == 0):
cycle = []
dfs(x, y)
print(result) |
val dx = listOf(1, 0, 0, -1) | ||
val dy = listOf(0, 1, -1, 0) | ||
val dD = listOf("D", "R", "L", "U") | ||
val dDInverse = listOf("U", "L", "R", "D") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ€,, λ°λλ°©ν₯λ μ΄μ©νλ λ°©λ²μ΄ μκ΅°μ...
μ²μμλ DRLUμ dx,dyμ μ°κ΄μ΄ μλμ€ μκ³ νκ°λ Έλ€μ
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Safe Zone μ΄μΌ μ°Ύμ§ νκ³ κ·Έλ¦Ό 그리λ€λ³΄λκΉ,,
κ²°κ΅ Safe Zoneμ΄ μλ―Ένλκ² μ£Όμ΄μ§ gridμμ μ‘΄μ¬νλ μν΄ μλ₯Ό μλ―ΈνλλΌκ³ μ!
κ·Έλμ μ λ gridμ μν΄ μλ₯Ό μ°Ύμ λ°ννλ μμΌλ‘ νμμ΅λλ€.
κ·Έλμ μ΄κ±΄ ν릴 μκ° μλ€νκ³ μ μΆνλλ° νλ Έμ΅λλ€κ° λ¨κΈΈλ
???????μμ§?????? νκ³ λ€λ₯Έ λΆλ€μ μ견μ ꡬνκ³ μ .. μ£Όμμ λ¬μλλ°
κ°μ μλͺ» μ
λ°μ΄νΈ μμΌμ€μ νλ Έλ€λκ±Έ μκ³ λ€μ κ³ μ³μ μ μΆνλλ
ν΅κ³Όνμ΅λλ€ γ
γ
보ν΅μ DFS, BFS λ¬Έμ μ λ€λ₯΄κ²
μ΄λ² λ¬Έμ λ
μ λ
Έλμ μ£Όλ³ μμΉ νμμΌλ‘λ Safe Zoneμ λͺ»μ°Ύκ² λλ°? λΌκ³ μκ°νλλ°
μ£Όλ³ μμΉ μ΄λνλ©΄μ νμ¬λ°©ν₯κ³Ό μΌμΉνλμ§ λ³΄λ©΄ λλ κ΅°μ ..?
μκ°μΉ λͺ»ν μ κ·Ό λ°©μμΈλ° μμκ°λλΉ
λ¬Έμ μ¬λ°λ€μ© μκ³ νμ ¨μ΅λλ€ ~~~~~
import sys
from collections import deque
def input() : return sys.stdin.readline().rstrip()
N, M = map(int, input().split())
grid = [[i for i in input()] for _ in range(N)]
visited = [[0 for _ in range(M)] for _ in range(N)]
q = deque()
circle_cnt = 0 # μ΅μ’
μ μΈ μν΄μ μ
step = circle_cnt + 1 # n λ²μ§Έ μν΄μ
for i in range(N):
for j in range(M):
# qμ μ무κ²λ μλ κ²½μ°
if visited[i][j] == 0:
q.append((i,j,grid[i][j]))
visited[i][j] = step
while q:
cx, cy, letter = q.popleft() # νμ¬ μμΉ
# λ€μ μμΉ
if letter == "R":
nx = cx
ny = cy + 1
elif letter == "L":
nx = cx
ny = cy - 1
elif letter == "U":
nx = cx - 1
ny = cy
else: # letter == "D"
nx = cx + 1
ny = cy
if 0<= nx < N and 0<= ny < M: # μ’νκ° λ²μ λ΄μ μμΌλ©΄
if visited[nx][ny] < step and visited[nx][ny] != 0:
# μ΄λ―Έ μ‘΄μ¬νλ μν΄μ μ’
μλλ κ²½μ° (μ΄λ―Έ μλ μν΄μ νμ νμ¬ stepλ³΄λ€ μκ³ 0μ΄ μλ)
visited[cx][cy] = visited[nx][ny] # μ’
μμν΄
step += 1 # λ€μ μ€ν
μ¦κ°
continue
if visited[nx][ny] == step: # μμ ν μν΄ νλ μ°Ύμ κ²½μ° -> λ€μ μν΄ μ°ΎκΈ° μν΄ step + 1
circle_cnt += 1
step += 1
continue
if visited[nx][ny] == 0: # μμ§ λ―Έλ°©λ¬Έ λ
ΈλμΈ κ²½μ°
visited[nx][ny] = step
q.append((nx, ny, grid[nx][ny]))
print(circle_cnt)
π λ¬Έμ λ§ν¬
νΌλ¦¬ λΆλ μ¬λμ΄
μ€λμ ν μ»·
νΌλ¦¬ λΆλ μ±μ°λ₯Ό μν΄ ν μ»·
βοΈ μμλ μκ°
40λΆ
β¨ μλ μ½λ
λ°©ν₯λΌλ¦¬ μ°κ²°λ κ°μλ₯Ό μ νλ©΄ λλ λ¬Έμ λΌκ³ μκ°νμ΅λλ€.
μ²μμ dfs λ‘ νλ €κ³ νμλλ°μ.
νμ¬ κ΅¬μμΌλ‘ μ€λ λ°©ν₯μ μ μ μμΌλ©΄ bfs λ‘ ν΄λ νλ¦¬μ§ μμκΉ? λΌλ μκ°μ νμ΅λλ€.
μλ₯Ό λ€μ΄ μ΄λ° λͺ¨μμ΄λΌλ©΄
νμμΉλ‘ κ°λ¦¬ν€λ ꡬμκ³Ό λ€μ ꡬμμ νμ μΆκ°νλ μμ λλ€.
κ·Έλμ λ°©ν₯κ³Ό ν΄λΉ λ°©ν₯, κ·Έλ¦¬κ³ λ°λ λ°©ν₯μ μ μ₯ν΄λκ³
쑰건문μΌλ‘ ν΄λΉλλ ꡬμμ νμ μΆκ°νλλ‘ νμ΅λλ€.
π μλ‘κ² μκ²λ λ΄μ©