Skip to content

감시피하기 o, 인구이동 x #88

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

Merged
merged 1 commit into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
n = int(input())

arr = []
temp = [[0] * n for _ in range(n)]
teacher = []
find = 0
for i in range(n):
arr.append(list(map(str, input().split())))
for j in range(n):
if arr[i][j] == 'T':
teacher.append((i, j))


def dfs(count):
global find
if count == 3: # 장애물 3개가 다 설치된 경우
for i in range(n): # 장애물 설치된 맵을 복사한 후에
for j in range(n):
temp[i][j] = arr[i][j]

if not check(): # 감시를 피하면 YES 출력후 종료
print("YES")
quit()

else: # 감시 못피하면 다른 경우도 검사해보기
return

for i in range(n): # 빈 공간에 대해서 장애물 3개까지 설치해보��� 과정
for j in range(n):
if arr[i][j] == 'X':
arr[i][j] = 'O' # 알파벳 O임
count += 1
dfs(count)
count -= 1
arr[i][j] = 'X'


def check():
result = 0
for x, y in teacher:
for i in range(4): # 상하좌우 방향 검사
if see(x, y, i): # 학생의 위치를 파악한 경우
return True

return False # 학생의 위치를 파악하지 못한 경우


def see(x, y, dir): # 상하좌우 순으로 검사
if dir == 0:
while 0 <= x < n:
if temp[x][y] == 'S':
return True
if temp[x][y] == 'O':
return False
else:
x -= 1
if dir == 1:
while 0 <= x < n:
if temp[x][y] == 'S':
return True
if temp[x][y] == 'O':
return False
else:
x += 1
if dir == 2:
while 0 <= y < n:
if temp[x][y] == 'S':
return True
if temp[x][y] == 'O':
return False
else:
y -= 1
if dir == 3:
while 0 <= y < n:
if temp[x][y] == 'S':
return True
if temp[x][y] == 'O':
return False
else:
y += 1

return False


dfs(0)#dfs 함수안에서 한번만이라도 감시를 피했으면 YES 출력후 종료함
print("NO")
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from collections import deque

n, l, r = map(int, input().split())

arr = []
for _ in range(n):
arr.append(list(map(int, input().split())))


#bfs, visited 사용해서 연합의 위치를 큐에 넣어주기
#1. bfs로 일단 먼저 연합끼리 큐에 넣고 (이때 큐에 넣을수 있으면 True, 못 넣으면 False)
#2. 큐에 넣은 연합애들끼리 통일시킨다
#3. 그 다음 또 bfs를 돌릴수 있는지 없는지 판단하기, bfs를 몇번 돌릴 수 있냐를 물어보는 문제

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

visited = [[0] * n for _ in range(n)]
union = [[-1] * n for _ in range(n)]
count = 0

def bfs(x, y, index): #x, y로 주어진 나라 위치 기준으로 연합국을 찾아 나가는 과정
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < n and 0 <= ny < n and not visited[nx][ny]: #bfs 조건
if l <= abs(arr[x][y]-arr[nx][ny]) <= r: #두 나라 사이의 인구수차이가 조건을 만족할때
union[nx][ny] = index
visited[nx][ny] = 1
bfs(nx, ny, index)
return


for i in range(n): #모든 나라에 대해서 연합 index를 union배열에 초기화 시켜주기
for j in range(n):
if not visited[i][j]:
#q = deque((i, j, count)) #bfs 돌릴 초기값 설정
visited[i][j] = 1
union[i][j] = count #bfs 시작위치의 연합 index 설정
bfs(i, j, count)

count += 1

for i in range(count):