File tree Expand file tree Collapse file tree 2 files changed +141
-0
lines changed Expand file tree Collapse file tree 2 files changed +141
-0
lines changed Original file line number Diff line number Diff line change 1+ n = int (input ())
2+
3+ arr = []
4+ temp = [[0 ] * n for _ in range (n )]
5+ teacher = []
6+ find = 0
7+ for i in range (n ):
8+ arr .append (list (map (str , input ().split ())))
9+ for j in range (n ):
10+ if arr [i ][j ] == 'T' :
11+ teacher .append ((i , j ))
12+
13+
14+ def dfs (count ):
15+ global find
16+ if count == 3 : # 장애물 3개가 다 설치된 경우
17+ for i in range (n ): # 장애물 설치된 맵을 복사한 후에
18+ for j in range (n ):
19+ temp [i ][j ] = arr [i ][j ]
20+
21+ if not check (): # 감시를 피하면 YES 출력후 종료
22+ print ("YES" )
23+ quit ()
24+
25+ else : # 감시 못피하면 다른 경우도 검사해보기
26+ return
27+
28+ for i in range (n ): # 빈 공간에 대해서 장애물 3개까지 설치해보는 과정
29+ for j in range (n ):
30+ if arr [i ][j ] == 'X' :
31+ arr [i ][j ] = 'O' # 알파벳 O임
32+ count += 1
33+ dfs (count )
34+ count -= 1
35+ arr [i ][j ] = 'X'
36+
37+
38+ def check ():
39+ result = 0
40+ for x , y in teacher :
41+ for i in range (4 ): # 상하좌우 방향 검사
42+ if see (x , y , i ): # 학생의 위치를 파악한 경우
43+ return True
44+
45+ return False # 학생의 위치를 파악하지 못한 경우
46+
47+
48+ def see (x , y , dir ): # 상하좌우 순으로 검사
49+ if dir == 0 :
50+ while 0 <= x < n :
51+ if temp [x ][y ] == 'S' :
52+ return True
53+ if temp [x ][y ] == 'O' :
54+ return False
55+ else :
56+ x -= 1
57+ if dir == 1 :
58+ while 0 <= x < n :
59+ if temp [x ][y ] == 'S' :
60+ return True
61+ if temp [x ][y ] == 'O' :
62+ return False
63+ else :
64+ x += 1
65+ if dir == 2 :
66+ while 0 <= y < n :
67+ if temp [x ][y ] == 'S' :
68+ return True
69+ if temp [x ][y ] == 'O' :
70+ return False
71+ else :
72+ y -= 1
73+ if dir == 3 :
74+ while 0 <= y < n :
75+ if temp [x ][y ] == 'S' :
76+ return True
77+ if temp [x ][y ] == 'O' :
78+ return False
79+ else :
80+ y += 1
81+
82+ return False
83+
84+
85+ dfs (0 )#dfs 함수안에서 한번만이라도 감시를 피했으면 YES 출력후 종료함
86+ print ("NO" )
Original file line number Diff line number Diff line change 1+ from collections import deque
2+
3+ n , l , r = map (int , input ().split ())
4+
5+ arr = []
6+ for _ in range (n ):
7+ arr .append (list (map (int , input ().split ())))
8+
9+
10+ #bfs, visited 사용해서 연합의 위치를 큐에 넣어주기
11+ #1. bfs로 일단 먼저 연합끼리 큐에 넣고 (이때 큐에 넣을수 있으면 True, 못 넣으면 False)
12+ #2. 큐에 넣은 연합애들끼리 통일시킨다
13+ #3. 그 다음 또 bfs를 돌릴수 있는지 없는지 판단하기, bfs를 몇번 돌릴 수 있냐를 물어보는 문제
14+
15+ dx = [- 1 , 1 , 0 , 0 ]
16+ dy = [0 , 0 , - 1 , 1 ]
17+
18+ visited = [[0 ] * n for _ in range (n )]
19+ union = [[- 1 ] * n for _ in range (n )]
20+ count = 0
21+
22+ def bfs (x , y , index ): #x, y로 주어진 나라 위치 기준으로 연합국을 찾아 나가는 과정
23+ for i in range (4 ):
24+ nx = x + dx [i ]
25+ ny = y + dy [i ]
26+ if 0 <= nx < n and 0 <= ny < n and not visited [nx ][ny ]: #bfs 조건
27+ if l <= abs (arr [x ][y ]- arr [nx ][ny ]) <= r : #두 나라 사이의 인구수차이가 조건을 만족할때
28+ union [nx ][ny ] = index
29+ visited [nx ][ny ] = 1
30+ bfs (nx , ny , index )
31+ return
32+
33+
34+ for i in range (n ): #모든 나라에 대해서 연합 index를 union배열에 초기화 시켜주기
35+ for j in range (n ):
36+ if not visited [i ][j ]:
37+ #q = deque((i, j, count)) #bfs 돌릴 초기값 설정
38+ visited [i ][j ] = 1
39+ union [i ][j ] = count #bfs 시작위치의 연합 index 설정
40+ bfs (i , j , count )
41+
42+ count += 1
43+
44+ for i in range (count ):
45+
46+
47+
48+
49+
50+
51+
52+
53+
54+
55+
You can’t perform that action at this time.
0 commit comments