Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

HanJun.Dev

[백준 18428번] 감시 피하기 본문

알고리즘

[백준 18428번] 감시 피하기

hanjun.e 2022. 7. 18. 06:06

https://www.acmicpc.net/problem/18428

 

18428번: 감시 피하기

NxN 크기의 복도가 있다. 복도는 1x1 크기의 칸으로 나누어지며, 특정한 위치에는 선생님, 학생, 혹은 장애물이 위치할 수 있다. 현재 몇 명의 학생들은 수업시간에 몰래 복도로 빠져나왔는데, 복

www.acmicpc.net


 

문제 유형: 브루트포스

 

풀이 과정:

DFS를 이용하여 해결 했다.

장애물의 위치는 itertools 라이브러리를 사용하여 장애물의 위치를 바꾸면서 가능,불가능을 판정함.

DFS를 이용하는 문제 중 한방향으로만 계속 가는 문제이기 때문에 초반 DFS를 실행할때 dir를 지정해주고,

계속 같은 dir으로만 DFS를 실행하는 방식으로 문제를 풀었다.


#백준18428
from itertools import combinations
import copy
n = int(input())
graph = [input().split() for _ in range(n)]

list_x = []

for i in range(n):
    for j in range(n):
        if graph[i][j] == 'X':
            list_x.append((i,j))

comb = list(combinations(list_x,3))

dx = [-1,1,0,0]
dy = [0,0,-1,1]


def dfs(x,y,dir):
    nx = x+dx[dir]
    ny = y+dy[dir]
    if nx<0 or nx>=n or ny<0 or ny>=n:
        return
    if map[nx][ny] == 'O':
        return
    if map[nx][ny] == 'S':
        global poss
        poss = False
    dfs(nx,ny,dir)

for o in comb:
    poss = True
    map = copy.deepcopy(graph)
    
    o1,o2,o3 = o[0],o[1],o[2]
    map[o1[0]][o1[1]] = 'O'
    map[o2[0]][o2[1]] = 'O'
    map[o3[0]][o3[1]] = 'O'
    for i in range(n):
        for j in range(n):
            if map[i][j] == 'T':
                for dir in range(4):
                    dfs(i,j,dir)
    if poss == True:
        print("YES")
        break
if poss == False:
    print("NO")

*처음 제출하였을 때 틀렸는데 map의 index 범위를 넘어서는 코드 처리하는 부분에서 부등호 기호를 잘못썻다...

'알고리즘' 카테고리의 다른 글

[백준 2156번] 포도주 시식  (1) 2023.02.08
[백준 2110번] 공유기 설치  (0) 2022.08.05
[프로그래머스] 블록 이동하기  (0) 2022.07.26
[백준 16234번] 인구 이동  (0) 2022.07.19
[백준 14888번] 연산자 끼워넣기  (0) 2022.07.15