Skip to content
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

Merged
merged 2 commits into from
Jun 3, 2024
Merged

27-SeongHoonC #201

merged 2 commits into from
Jun 3, 2024

Conversation

SeongHoonC
Copy link
Collaborator

@SeongHoonC SeongHoonC commented May 30, 2024

πŸ”— 문제 링크

피리 λΆ€λŠ” μ‚¬λ‚˜μ΄

였늘의 ν•œ μ»·

피리 λΆ€λŠ” μ„±μš°λ₯Ό μœ„ν•΄ ν•œ μ»·
κ΅°λŒ€μ΄λ―Έμ§€

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

40λΆ„

✨ μˆ˜λ„ μ½”λ“œ

λ°©ν–₯끼리 μ—°κ²°λœ 개수λ₯Ό μ •ν•˜λ©΄ λ˜λŠ” 문제라고 μƒκ°ν–ˆμŠ΅λ‹ˆλ‹€.

μ²˜μŒμ—” dfs 둜 ν’€λ €κ³  ν–ˆμ—ˆλŠ”λ°μš”.

ν˜„μž¬ κ΅¬μ—­μœΌλ‘œ μ˜€λŠ” λ°©ν–₯을 μ•Œ 수 있으면 bfs 둜 해도 풀리지 μ•Šμ„κΉŒ? λΌλŠ” 생각을 ν–ˆμŠ΅λ‹ˆλ‹€.

예λ₯Ό λ“€μ–΄ 이런 λͺ¨μ–‘이라면
image

ν˜„μœ„μΉ˜λ‘œ κ°€λ¦¬ν‚€λŠ” ꡬ역과 λ‹€μŒ ꡬ역을 큐에 μΆ”κ°€ν•˜λŠ” μ‹μž…λ‹ˆλ‹€.
image

κ·Έλž˜μ„œ λ°©ν–₯κ³Ό ν•΄λ‹Ή λ°©ν–₯, 그리고 λ°˜λŒ€ λ°©ν–₯을 μ €μž₯해놓고

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")

쑰건문으둜 ν•΄λ‹Ήλ˜λŠ” ꡬ역을 큐에 μΆ”κ°€ν•˜λ„λ‘ ν–ˆμŠ΅λ‹ˆλ‹€.

// bfs 둜 같은 사이클에 μ‘΄μž¬ν•˜λŠ” μ• λ“€ λ‹€ μ°ΎκΈ°
private fun findSafeZone(x: Int, y: Int, n: Int, m: Int): Boolean {
    if (visited[x][y]) return false
    val q = ArrayDeque<Pair<Int, Int>>()
    q.add(x to y)
    while (q.isNotEmpty()) {
        val now = q.removeFirst()
        val nowDirection = dD.indexOf(graph[now.first][now.second])
        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)
            }
        }
    }
    return true
}

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

Copy link
Member

@tgyuuAn tgyuuAn left a 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)

Comment on lines +42 to +52
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)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

였호라 주변을 νƒμƒ‰ν•΄μ„œ 같은 사이클을 돌면 μ²΄ν¬ν•΄μ£ΌλŠ” μ‹μœΌλ‘œ ν’€μ—ˆκΎΌμš”... μ‹ κΈ°λ°©κΈ°λ‹€

@alstjr7437
Copy link
Member

μ²˜μŒμ—λŠ” μ•„λž˜μ™€ 같이 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)

Comment on lines +8 to +11
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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

였,, λ°˜λŒ€λ°©ν–₯도 μ΄μš©ν•˜λŠ” 방법이 μžˆκ΅°μš”...
μ²˜μŒμ—λŠ” DRLU와 dx,dy와 연관이 μžˆλŠ”μ€„ μ•Œκ³  ν–‡κ°ˆλ Έλ„€μš”

Copy link
Collaborator

@H0ngJu H0ngJu left a 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)

@SeongHoonC SeongHoonC merged commit b3421a3 into main Jun 3, 2024
5 checks passed
@SeongHoonC SeongHoonC deleted the 27-SeongHoonC branch June 3, 2024 03:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants