Skip to content

승연 책 숙제(6/15/화) #89

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 3 commits into from
Jun 24, 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
65 changes: 65 additions & 0 deletions syheo/codingTest1/13-19-연산자 끼워 넣기-2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#CH13 BFSDFS기출
#예제 13-19
#연산자 끼워 넣기
#백준 14888
#실버 1

#덧셈, 뺼셈, 곱셈, 나눗셈

#아이디어 1
#1. 연산자 경우의 수를 모두 구함 -> bfs
#2. 연산자 리스트가 완성되면 max, min 값에 값을 계산하면서 답을 구함.

#아이디어 2
#1. 계산 결과와 남은 연산자의 갯수를 저장-> bfs
#2. 남은 연산자의 갯수가 없으면 계산 결과로 min, max return

from collections import deque

def cal(a,b,cmd):
if cmd == 0:
return a+b
elif cmd == 1:
return a-b
elif cmd == 2:
return a*b
elif cmd == 3:
if a<0 or b<0:
return -(abs(a)//abs(b))
return a//b

N = int(input())

nums = list(map(int,input().split()))

cmdCnts = list(map(int,input().split()))

minValue = int(1e9)
maxValue = -int(1e9)

def bfs():
global minValue, maxValue
a,b,c,d = cmdCnts
q = deque([(nums[0],1,a,b,c,d)])
while q:
rst, idx, a, b, c, d = q.popleft()
#완성된 연산자 리스트 계산 후 max,min 값 비교
if a+b+c+d == 0:
minValue = min(rst,minValue)
maxValue = max(rst,maxValue)
#연산자 경우의 수 모두 구하기
if a!=0:
q.append((cal(rst,nums[idx],0),idx+1,a-1,b,c,d))
if b!=0:
q.append((cal(rst,nums[idx],1),idx+1,a,b-1,c,d))
if c!=0:
q.append((cal(rst,nums[idx],2),idx+1,a,b,c-1,d))
if d!=0:
q.append((cal(rst,nums[idx],3),idx+1,a,b,c,d-1))


bfs()
print(maxValue)
print(minValue)


69 changes: 69 additions & 0 deletions syheo/codingTest1/13-19-연산자 끼워 넣기.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#CH13 BFSDFS기출
#예제 13-19
#연산자 끼워 넣기
#백준 14888
#실버 1

#덧셈, 뺼셈, 곱셈, 나눗셈

#아이디어 1
#1. 연산자 경우의 수를 모두 구함 -> bfs
#2. 연산자 리스트가 완성되면 max, min 값에 값을 계산하면서 답을 구함.

#아이디어 2
#1. 계산 결과와 남은 연산자의 갯수를 저장-> bfs
#2. 남은 연산자의 갯수가 없으면 계산 결과로 min, max return

from collections import deque

def cal(a,b,cmd):
if cmd == 0:
return a+b
elif cmd == 1:
return a-b
elif cmd == 2:
return a*b
elif cmd == 3:
if a<0 or b<0:
return -(abs(a)//abs(b))
return a//b

N = int(input())

nums = deque(list(map(int,input().split())))

cmdCnts = list(map(int,input().split()))

def bfs():
minValue = int(1e9)
maxValue = -int(1e9)
a,b,c,d = cmdCnts
q = deque([([],a,b,c,d)])
while q:
cmdList, a, b, c, d = q.popleft()
#완성된 연산자 리스트 계산 후 max,min 값 비교
if a+b+c+d == 0:
rst = nums[0]
idx = 1
for cmd in cmdList:
rst = cal(rst,nums[idx],cmd)
idx+=1
minValue = min(rst,minValue)
maxValue = max(rst,maxValue)
#연산자 경우의 수 모두 구하기
if a!=0:
q.append((cmdList+[0],a-1,b,c,d))
if b!=0:
q.append((cmdList+[1],a,b-1,c,d))
if c!=0:
q.append((cmdList+[2],a,b,c-1,d))
if d!=0:
q.append((cmdList+[3],a,b,c,d-1))

return (maxValue,minValue)

a,b = bfs()
print(a)
print(b)


86 changes: 86 additions & 0 deletions syheo/codingTest1/13-20-감시 피하기.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#CH13 BFSDFS기출
#예제 13-20
#감시 피하기
#백준 18428
#실버 1

from collections import deque

N = int(input())

maps = []
students = []
teachers = []
obstructs = []
for i in range(N):
tmp = list(map(str,input().split()))
for j in range(N):
if tmp[j]=='S':
students.append((i,j))
if tmp[j]=='T':
teachers.append((i,j))
if tmp[j]=='X':
obstructs.append((i,j))
maps.append(tmp)

def isAvoid(a,b,d):
for student in students:
row = student[0]
col = student[1]
r = row
c = col
n = 1
while r-n>=0:
if (a[0]==r-n and a[1]==col) or (b[0]==r-n and b[1]==col) or (d[0]==r-n and d[1]==col):
break
if maps[r-n][col]=='T':
return False
n+=1
n = 1
while r+n<N:
if (a[0]==r+n and a[1]==col) or (b[0]==r+n and b[1]==col) or (d[0]==r+n and d[1]==col):
break
if maps[r+n][col]=='T':
return False
n+=1
n = 1
while c-n>=0:
if (a[0]==row and a[1]==c-n) or (b[0]==row and b[1]==c-n) or (d[0]==row and d[1]==c-n):
break
if maps[row][c-n]=='T':
return False
n+=1
n = 1
while c+n<N:
if (a[0]==row and a[1]==c+n) or (b[0]==row and b[1]==c+n) or (d[0]==row and d[1]==c+n):
break
if maps[row][c+n]=='T':
return False
n+=1


return True

def bfs():
#visited = [[False]*N for _ in range(N)]
q = deque([])
#실수가 있었음 -> range 안에 len(obstructs)-2를 넣어야 하는데 N을 넣음 ;;
for i in range(len(obstructs)-2):
q.append(([i],1)) #방해물 위치 인덱스, 카운트
while q:
locs , cnt = q.popleft()
if cnt == 3:
if isAvoid(obstructs[locs[0]],obstructs[locs[1]],obstructs[locs[2]]):
return True
else:
for i in range(locs[-1]+1,len(obstructs)):
q.append((locs+[i],cnt+1))

return False

if bfs():
print("YES")
else:
print("NO")


64 changes: 64 additions & 0 deletions syheo/codingTest1/13-21-인구이동.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#CH13 BFSDFS기출
#예제 13-21
#인구 이동
#백준 16234
#골드 5

#Pypy3로 통과

#아이디어 : dfs를 통해 한 칸 기준 연합할 수 있는 묶음을 구함.

import sys
sys.setrecursionlimit(10**9)

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

#dfs
def check(row,col,N,L,R):
for i in range(4):
r = row+dx[i]
c = col+dy[i]
#연합 가능 조건
if 0<=r<N and 0<=c<N and not visited[r][c] and L<=abs(maps[r][c]-maps[row][col])<=R:
visited[r][c]=True
result.append((r,c))
check(r,c,N,L,R)


N,L,R = map(int,input().split())

maps = []

for i in range(N):
maps.append(list(map(int,input().split())))

whole_cnt = 0

while True:
locations = []
visited = [[False]*N for _ in range(N)]
#모든 칸에 대해 반복
for i in range(N):
for j in range(N):
if not visited[i][j]:
result = [(i,j)]
visited[i][j]=True
check(i,j,N,L,R)
#연합된 경우가 있으면
if len(result)>1:
locations.append(result)
#연합 그룹이 아예 없는 경우
if not locations:
break
#인구 이동
for location in locations:
total = sum([maps[i][j] for i,j in location])
people = total//len(location)
for loc in location:
maps[loc[0]][loc[1]]=people
#카운트 증가
whole_cnt+=1

print(whole_cnt)