diff --git "a/syheo/codingTest1/13-19-\354\227\260\354\202\260\354\236\220 \353\201\274\354\233\214 \353\204\243\352\270\260-2.py" "b/syheo/codingTest1/13-19-\354\227\260\354\202\260\354\236\220 \353\201\274\354\233\214 \353\204\243\352\270\260-2.py" new file mode 100644 index 0000000..de825d9 --- /dev/null +++ "b/syheo/codingTest1/13-19-\354\227\260\354\202\260\354\236\220 \353\201\274\354\233\214 \353\204\243\352\270\260-2.py" @@ -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) + + diff --git "a/syheo/codingTest1/13-19-\354\227\260\354\202\260\354\236\220 \353\201\274\354\233\214 \353\204\243\352\270\260.py" "b/syheo/codingTest1/13-19-\354\227\260\354\202\260\354\236\220 \353\201\274\354\233\214 \353\204\243\352\270\260.py" new file mode 100644 index 0000000..f14ca40 --- /dev/null +++ "b/syheo/codingTest1/13-19-\354\227\260\354\202\260\354\236\220 \353\201\274\354\233\214 \353\204\243\352\270\260.py" @@ -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) + + diff --git "a/syheo/codingTest1/13-20-\352\260\220\354\213\234 \355\224\274\355\225\230\352\270\260.py" "b/syheo/codingTest1/13-20-\352\260\220\354\213\234 \355\224\274\355\225\230\352\270\260.py" new file mode 100644 index 0000000..d6a111a --- /dev/null +++ "b/syheo/codingTest1/13-20-\352\260\220\354\213\234 \355\224\274\355\225\230\352\270\260.py" @@ -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=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 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") + + diff --git "a/syheo/codingTest1/13-21-\354\235\270\352\265\254\354\235\264\353\217\231.py" "b/syheo/codingTest1/13-21-\354\235\270\352\265\254\354\235\264\353\217\231.py" new file mode 100644 index 0000000..cd66b77 --- /dev/null +++ "b/syheo/codingTest1/13-21-\354\235\270\352\265\254\354\235\264\353\217\231.py" @@ -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<=r1: + 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) +