Skip to content

Commit 0030527

Browse files
authored
승연 책 숙제(6/15/화) (#89)
* 책 숙제 * 16234 fix * delete test.py
1 parent f332164 commit 0030527

File tree

4 files changed

+284
-0
lines changed

4 files changed

+284
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#CH13 BFSDFS기출
2+
#예제 13-19
3+
#연산자 끼워 넣기
4+
#백준 14888
5+
#실버 1
6+
7+
#덧셈, 뺼셈, 곱셈, 나눗셈
8+
9+
#아이디어 1
10+
#1. 연산자 경우의 수를 모두 구함 -> bfs
11+
#2. 연산자 리스트가 완성되면 max, min 값에 값을 계산하면서 답을 구함.
12+
13+
#아이디어 2
14+
#1. 계산 결과와 남은 연산자의 갯수를 저장-> bfs
15+
#2. 남은 연산자의 갯수가 없으면 계산 결과로 min, max return
16+
17+
from collections import deque
18+
19+
def cal(a,b,cmd):
20+
if cmd == 0:
21+
return a+b
22+
elif cmd == 1:
23+
return a-b
24+
elif cmd == 2:
25+
return a*b
26+
elif cmd == 3:
27+
if a<0 or b<0:
28+
return -(abs(a)//abs(b))
29+
return a//b
30+
31+
N = int(input())
32+
33+
nums = list(map(int,input().split()))
34+
35+
cmdCnts = list(map(int,input().split()))
36+
37+
minValue = int(1e9)
38+
maxValue = -int(1e9)
39+
40+
def bfs():
41+
global minValue, maxValue
42+
a,b,c,d = cmdCnts
43+
q = deque([(nums[0],1,a,b,c,d)])
44+
while q:
45+
rst, idx, a, b, c, d = q.popleft()
46+
#완성된 연산자 리스트 계산 후 max,min 값 비교
47+
if a+b+c+d == 0:
48+
minValue = min(rst,minValue)
49+
maxValue = max(rst,maxValue)
50+
#연산자 경우의 수 모두 구하기
51+
if a!=0:
52+
q.append((cal(rst,nums[idx],0),idx+1,a-1,b,c,d))
53+
if b!=0:
54+
q.append((cal(rst,nums[idx],1),idx+1,a,b-1,c,d))
55+
if c!=0:
56+
q.append((cal(rst,nums[idx],2),idx+1,a,b,c-1,d))
57+
if d!=0:
58+
q.append((cal(rst,nums[idx],3),idx+1,a,b,c,d-1))
59+
60+
61+
bfs()
62+
print(maxValue)
63+
print(minValue)
64+
65+
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#CH13 BFSDFS기출
2+
#예제 13-19
3+
#연산자 끼워 넣기
4+
#백준 14888
5+
#실버 1
6+
7+
#덧셈, 뺼셈, 곱셈, 나눗셈
8+
9+
#아이디어 1
10+
#1. 연산자 경우의 수를 모두 구함 -> bfs
11+
#2. 연산자 리스트가 완성되면 max, min 값에 값을 계산하면서 답을 구함.
12+
13+
#아이디어 2
14+
#1. 계산 결과와 남은 연산자의 갯수를 저장-> bfs
15+
#2. 남은 연산자의 갯수가 없으면 계산 결과로 min, max return
16+
17+
from collections import deque
18+
19+
def cal(a,b,cmd):
20+
if cmd == 0:
21+
return a+b
22+
elif cmd == 1:
23+
return a-b
24+
elif cmd == 2:
25+
return a*b
26+
elif cmd == 3:
27+
if a<0 or b<0:
28+
return -(abs(a)//abs(b))
29+
return a//b
30+
31+
N = int(input())
32+
33+
nums = deque(list(map(int,input().split())))
34+
35+
cmdCnts = list(map(int,input().split()))
36+
37+
def bfs():
38+
minValue = int(1e9)
39+
maxValue = -int(1e9)
40+
a,b,c,d = cmdCnts
41+
q = deque([([],a,b,c,d)])
42+
while q:
43+
cmdList, a, b, c, d = q.popleft()
44+
#완성된 연산자 리스트 계산 후 max,min 값 비교
45+
if a+b+c+d == 0:
46+
rst = nums[0]
47+
idx = 1
48+
for cmd in cmdList:
49+
rst = cal(rst,nums[idx],cmd)
50+
idx+=1
51+
minValue = min(rst,minValue)
52+
maxValue = max(rst,maxValue)
53+
#연산자 경우의 수 모두 구하기
54+
if a!=0:
55+
q.append((cmdList+[0],a-1,b,c,d))
56+
if b!=0:
57+
q.append((cmdList+[1],a,b-1,c,d))
58+
if c!=0:
59+
q.append((cmdList+[2],a,b,c-1,d))
60+
if d!=0:
61+
q.append((cmdList+[3],a,b,c,d-1))
62+
63+
return (maxValue,minValue)
64+
65+
a,b = bfs()
66+
print(a)
67+
print(b)
68+
69+
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#CH13 BFSDFS기출
2+
#예제 13-20
3+
#감시 피하기
4+
#백준 18428
5+
#실버 1
6+
7+
from collections import deque
8+
9+
N = int(input())
10+
11+
maps = []
12+
students = []
13+
teachers = []
14+
obstructs = []
15+
for i in range(N):
16+
tmp = list(map(str,input().split()))
17+
for j in range(N):
18+
if tmp[j]=='S':
19+
students.append((i,j))
20+
if tmp[j]=='T':
21+
teachers.append((i,j))
22+
if tmp[j]=='X':
23+
obstructs.append((i,j))
24+
maps.append(tmp)
25+
26+
def isAvoid(a,b,d):
27+
for student in students:
28+
row = student[0]
29+
col = student[1]
30+
r = row
31+
c = col
32+
n = 1
33+
while r-n>=0:
34+
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):
35+
break
36+
if maps[r-n][col]=='T':
37+
return False
38+
n+=1
39+
n = 1
40+
while r+n<N:
41+
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):
42+
break
43+
if maps[r+n][col]=='T':
44+
return False
45+
n+=1
46+
n = 1
47+
while c-n>=0:
48+
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):
49+
break
50+
if maps[row][c-n]=='T':
51+
return False
52+
n+=1
53+
n = 1
54+
while c+n<N:
55+
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):
56+
break
57+
if maps[row][c+n]=='T':
58+
return False
59+
n+=1
60+
61+
62+
return True
63+
64+
def bfs():
65+
#visited = [[False]*N for _ in range(N)]
66+
q = deque([])
67+
#실수가 있었음 -> range 안에 len(obstructs)-2를 넣어야 하는데 N을 넣음 ;;
68+
for i in range(len(obstructs)-2):
69+
q.append(([i],1)) #방해물 위치 인덱스, 카운트
70+
while q:
71+
locs , cnt = q.popleft()
72+
if cnt == 3:
73+
if isAvoid(obstructs[locs[0]],obstructs[locs[1]],obstructs[locs[2]]):
74+
return True
75+
else:
76+
for i in range(locs[-1]+1,len(obstructs)):
77+
q.append((locs+[i],cnt+1))
78+
79+
return False
80+
81+
if bfs():
82+
print("YES")
83+
else:
84+
print("NO")
85+
86+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#CH13 BFSDFS기출
2+
#예제 13-21
3+
#인구 이동
4+
#백준 16234
5+
#골드 5
6+
7+
#Pypy3로 통과
8+
9+
#아이디어 : dfs를 통해 한 칸 기준 연합할 수 있는 묶음을 구함.
10+
11+
import sys
12+
sys.setrecursionlimit(10**9)
13+
14+
dx = [0,0,-1,1]
15+
dy = [1,-1,0,0]
16+
17+
#dfs
18+
def check(row,col,N,L,R):
19+
for i in range(4):
20+
r = row+dx[i]
21+
c = col+dy[i]
22+
#연합 가능 조건
23+
if 0<=r<N and 0<=c<N and not visited[r][c] and L<=abs(maps[r][c]-maps[row][col])<=R:
24+
visited[r][c]=True
25+
result.append((r,c))
26+
check(r,c,N,L,R)
27+
28+
29+
N,L,R = map(int,input().split())
30+
31+
maps = []
32+
33+
for i in range(N):
34+
maps.append(list(map(int,input().split())))
35+
36+
whole_cnt = 0
37+
38+
while True:
39+
locations = []
40+
visited = [[False]*N for _ in range(N)]
41+
#모든 칸에 대해 반복
42+
for i in range(N):
43+
for j in range(N):
44+
if not visited[i][j]:
45+
result = [(i,j)]
46+
visited[i][j]=True
47+
check(i,j,N,L,R)
48+
#연합된 경우가 있으면
49+
if len(result)>1:
50+
locations.append(result)
51+
#연합 그룹이 아예 없는 경우
52+
if not locations:
53+
break
54+
#인구 이동
55+
for location in locations:
56+
total = sum([maps[i][j] for i,j in location])
57+
people = total//len(location)
58+
for loc in location:
59+
maps[loc[0]][loc[1]]=people
60+
#카운트 증가
61+
whole_cnt+=1
62+
63+
print(whole_cnt)
64+

0 commit comments

Comments
 (0)