├── README.md ├── 보물상자의_비밀번호.py ├── 입국심사.py ├── 배열회전.py ├── 격자판의_숫자_이어붙이기.py ├── 러시아_국기_같은_깃발.py ├── 특이한_자석.py ├── 색종이2.py ├── 탈주범검거.py ├── 원자소멸시뮬레이션.py ├── 색종이.py ├── 재미있는_오슬로.py ├── 활주로_건설.py ├── 보호필름.py ├── 연구소.py ├── 윷놀이.py ├── 등산로조정.py ├── 낚시왕.py ├── 파핑파핑_지뢰찾기.py ├── 디저트카페.py ├── 홈방범서비스.py ├── 벽돌깨기.py ├── 원판돌리기.py ├── 점심식사시간.py ├── 나무_재테크.py ├── 줄기세포_배양.py ├── 미생물격리.py ├── 무선충전.py ├── 핀볼게임.py └── 게리멘더링2.py /README.md: -------------------------------------------------------------------------------- 1 | # samsung_algorithm 2 | 삼성 기출 알고리즘 파이썬 풀이 모음 3 | 4 | 5 | 2020 삼성 SW 역량테스트 대비 알고리즘 풀이 6 | -------------------------------------------------------------------------------- /보물상자의_비밀번호.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.stdin = open("input.txt","r") 3 | 4 | from collections import deque 5 | testCase = int(input()) 6 | 7 | for tc in range(1,testCase+1): 8 | n,k = map(int,input().split()) 9 | a = list(input().rstrip()) 10 | 11 | x = n//4 12 | d = {} 13 | for _ in range(x): 14 | for i in range(0,n,x): 15 | string = ''.join(a[i:i+x]) 16 | d[string] = int(string,16) 17 | a = deque(a) 18 | a.append(a.popleft()) 19 | a=list(a) 20 | anslist = sorted(d.items(),key=lambda x:x[1],reverse=True) 21 | print("#{} {}".format(tc,anslist[k-1][1])) -------------------------------------------------------------------------------- /입국심사.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.stdin= open("input.txt","r") 3 | 4 | testCase = int(input()) 5 | 6 | for tc in range(1,testCase+1): 7 | n,m=map(int,input().split()) 8 | a = [] 9 | low,high=1,0 10 | for _ in range(n): 11 | tmp = int(input()) 12 | if tmp > high: 13 | high = tmp 14 | a.append(tmp) 15 | high = high * m 16 | ans = high 17 | while low <= high: 18 | mid = (low + high) // 2 19 | sum_p=0 20 | for k in a: 21 | sum_p += mid//k 22 | 23 | 24 | if sum_p >= m: 25 | high = mid -1 26 | ans = mid 27 | else: 28 | low = mid + 1 29 | 30 | print('#{} {}'.format(tc,ans)) 31 | 32 | -------------------------------------------------------------------------------- /배열회전.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.stdin = open("input.txt","r") 3 | 4 | testCase = int(input()) 5 | 6 | def rotate(a): 7 | n = len(a) 8 | tmp = [[0]*n for _ in range(n)] 9 | for i in range(n): 10 | for j in range(n): 11 | tmp[i][j]= a[n-j-1][i] 12 | return tmp 13 | 14 | for tc in range(1,testCase+1): 15 | n = int(input()) 16 | a = [list(map(int,input().split())) for _ in range(n)] 17 | ans= [ "" for _ in range(n)] 18 | for _ in range(3): 19 | a = rotate(a) 20 | for i in range(n): 21 | for num in a[i]: 22 | ans[i] +=str(num) 23 | if _ < 2: 24 | ans[i]+=' ' 25 | print("#{}".format(tc)) 26 | for an in ans: 27 | print(an) 28 | -------------------------------------------------------------------------------- /격자판의_숫자_이어붙이기.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.stdin = open("input.txt","r") 3 | 4 | from collections import defaultdict 5 | 6 | num_dict = defaultdict(int) 7 | testCase = int(input()) 8 | 9 | dx = [0,0,-1,1] 10 | dy = [1,-1,0,0] 11 | 12 | def dfs(idx,x,y,numlist,a): 13 | if idx==7: 14 | string = '' 15 | for num in numlist: 16 | string += str(num) 17 | num_dict[string]+=1 18 | 19 | else: 20 | for i in range(4): 21 | nx,ny = x + dx[i],y+dy[i] 22 | if 0<=nx<4 and 0<=ny<4: 23 | numlist.append(a[nx][ny]) 24 | dfs(idx+1,nx,ny,numlist,a) 25 | numlist.pop() 26 | 27 | 28 | for tc in range(1,testCase+1): 29 | a = [list(map(int,input().split())) for _ in range(4)] 30 | num_dict=defaultdict(int) 31 | for i in range(4): 32 | for j in range(4): 33 | dfs(0,i,j,[],a) 34 | print('#{} {}'.format(tc,len(num_dict))) 35 | -------------------------------------------------------------------------------- /러시아_국기_같은_깃발.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.stdin = open("input.txt","r") 3 | 4 | 5 | testCase = int(input()) 6 | 7 | 8 | def making(n): 9 | res = [] 10 | for i in range(1,n+1): 11 | for j in range(1,n+1): 12 | for k in range(1,n+1): 13 | if i+j+k==n: 14 | res.append([i,j,k]) 15 | return res 16 | 17 | def ret_cnt(rge,a,f): 18 | cnt = 0 19 | for i in range(rge[0],rge[1]): 20 | for j in range(m): 21 | if a[i][j]!=f: 22 | cnt += 1 23 | return cnt 24 | 25 | def count_tile(row,a): 26 | m = len(a[0]) 27 | w_rge = [0,row[0]] 28 | b_rge = [row[0],row[0]+row[1]] 29 | r_rge = [row[0]+row[1],row[0]+row[1]+row[2]] 30 | cnt = 0 31 | cnt += ret_cnt(w_rge,a,'W') 32 | cnt += ret_cnt(b_rge,a,'B') 33 | cnt += ret_cnt(r_rge,a,'R') 34 | 35 | return cnt 36 | 37 | for tc in range(1,testCase+1): 38 | n,m = map(int,input().split()) 39 | a = [list(map(str,input().rstrip())) for _ in range(n)] 40 | ans = 987654321 41 | for candi in making(n): 42 | c = count_tile(candi,a) 43 | if c < ans: 44 | ans = c 45 | 46 | print('#{} {}'.format(tc,ans)) 47 | -------------------------------------------------------------------------------- /특이한_자석.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.stdin = open("input.txt","r") 3 | 4 | from collections import deque 5 | 6 | testCase = int(input()) 7 | 8 | #1 9 | def Rotate(arr): 10 | tmp = deque([x for x in arr]) 11 | tmp.appendleft(tmp.pop()) 12 | return tmp 13 | 14 | #-1 15 | def RevRotate(arr): 16 | tmp = deque([x for x in arr]) 17 | tmp.append(tmp.popleft()) 18 | return tmp 19 | 20 | def Do(num,dir,a,v): 21 | if num<0 or num>3 or v[num]==1: 22 | return 23 | 24 | v[num]=1 25 | #left check 26 | if num-1>=0 and a[num][6]+a[num-1][2]==1 and v[num-1]==0: 27 | Do(num-1,dir*(-1),a,v) 28 | 29 | #right check 30 | if num+1<=3 and a[num][2]+a[num+1][6]==1 and v[num+1]==0: 31 | Do(num+1,dir*(-1),a,v) 32 | 33 | if dir<0: 34 | a[num] = RevRotate(a[num]) 35 | else: 36 | a[num] = Rotate(a[num]) 37 | 38 | 39 | for tc in range(1,testCase+1): 40 | k=int(input()) 41 | a = [deque(list(map(int,input().split()))) for _ in range(4)] 42 | 43 | for _ in range(k): 44 | num,dir=map(int,input().split()) 45 | Do(num-1,dir,a,[0,0,0,0]) 46 | 47 | ans = sum([1*a[0][0],2*a[1][0],4*a[2][0],8*a[3][0]]) 48 | 49 | print("#{} {}".format(tc,ans)) 50 | -------------------------------------------------------------------------------- /색종이2.py: -------------------------------------------------------------------------------- 1 | import sys 2 | input = sys.stdin.readline 3 | 4 | board = [list(map(int,input().split())) for _ in range(10)] 5 | paper=[0,0,0,0,0,0] 6 | ans = 100 7 | 8 | def check(): 9 | for i in range(10): 10 | for j in range(10): 11 | if board[i][j]==1: 12 | return False 13 | return True 14 | 15 | def is_cover(x,y,pad): 16 | if paper[pad]==5: 17 | return False 18 | for i in range(x,x+pad): 19 | for j in range(y,y+pad): 20 | if not (0<=i<10 and 0<=j<10 and board[i][j]==1): 21 | return False 22 | return True 23 | 24 | def cover(x,y,pad,t): 25 | for i in range(x,x+pad): 26 | for j in range(y,y+pad): 27 | board[i][j]=t 28 | 29 | def dfs(idx): 30 | global ans 31 | if idx==100: 32 | if check(): 33 | ans = min(ans,sum(paper)) 34 | return 35 | if ans < sum(paper): 36 | return 37 | r,c = idx//10,idx%10 38 | if board[r][c]==1: 39 | for pad in range(5,0,-1): 40 | if is_cover(r,c,pad): 41 | paper[pad]+=1 42 | cover(r,c,pad,0) 43 | dfs(idx+1) 44 | cover(r,c,pad,1) 45 | paper[pad]-=1 46 | else: 47 | dfs(idx+1) 48 | 49 | dfs(0) 50 | print(ans if ans!=100 else -1) 51 | -------------------------------------------------------------------------------- /탈주범검거.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.stdin = open("input.txt","r") 3 | 4 | 5 | from collections import deque 6 | testcase = int(input()) 7 | 8 | dx = [-1,0,1,0] 9 | dy = [0,1,0,-1] 10 | 11 | num2dir={ 12 | 1:[[-1,0],[0,1],[1,0],[0,-1]], 13 | 2:[[-1,0],[1,0]], 14 | 3:[[0,-1],[0,1]], 15 | 4:[[-1,0],[0,1]], 16 | 5:[[1,0],[0,1]], 17 | 6:[[1,0],[0,-1]], 18 | 7:[[-1,0],[0,-1]] 19 | } 20 | 21 | def bfs(tunnel,r,c,l): 22 | n,m = len(tunnel),len(tunnel[0]) 23 | ch = [[0 for _ in range(m)] for _ in range(n)] 24 | ch[r][c]=1 25 | q = deque([]) 26 | q.append([r,c,1]) 27 | 28 | while q: 29 | x,y,ti = q.popleft() 30 | 31 | psible_dir=num2dir[tunnel[x][y]] 32 | 33 | for p in psible_dir: 34 | nx,ny = x + p[0],y + p[1] 35 | cond_dir= [p[0]*-1,p[1]*-1] 36 | if 0<=nx0 \ 37 | and ti+1<=l and (cond_dir in num2dir[tunnel[nx][ny]]): 38 | ch[nx][ny]=1 39 | q.append([nx,ny,ti+1]) 40 | 41 | cnt = 0 42 | for c in ch: 43 | cnt += sum(c) 44 | return cnt 45 | 46 | for tc in range(1,testcase+1): 47 | n,m,r,c,l = map(int,input().split()) 48 | tunnel = [list(map(int,input().split())) for _ in range(n)] 49 | print('#{} {}'.format(tc,bfs(tunnel,r,c,l))) -------------------------------------------------------------------------------- /원자소멸시뮬레이션.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.stdin = open("input.txt","r") 3 | 4 | dx = [0,0,-1,1] 5 | dy = [1,-1,0,0] 6 | testCase = int(input()) 7 | 8 | def move(atom,ans): 9 | d = {} 10 | delList=[] 11 | for i in range(len(atom)): 12 | tx = atom[i][0] + dx[atom[i][2]] 13 | ty = atom[i][1] + dy[atom[i][2]] 14 | if abs(tx)>2000 or abs(ty)>2000: 15 | delList.append(i) 16 | else: 17 | string = '{} {}'.format(tx,ty) 18 | if string in d: 19 | d[string].append(i) 20 | else: 21 | d[string]=[] 22 | d[string].append(i) 23 | atom[i][0]=tx 24 | atom[i][1]=ty 25 | 26 | for idx,val in d.items(): 27 | if len(val) > 1: 28 | for v in val: 29 | ans += atom[v][3] 30 | delList.append(v) 31 | 32 | tmp=[] 33 | for i in range(len(atom)): 34 | if not( i in delList): 35 | tmp.append(atom[i]) 36 | 37 | return tmp,ans 38 | 39 | for tc in range(1,testCase+1): 40 | n = int(input()) 41 | atom=[] 42 | ans =0 43 | for _ in range(n): 44 | a,b,dir,energy = map(int,input().split()) 45 | atom.append([2*a,2*b,dir,energy]) 46 | 47 | while len(atom)>0: 48 | atom,ans = move(atom,ans) 49 | print("#{} {}".format(tc,ans)) 50 | 51 | -------------------------------------------------------------------------------- /색종이.py: -------------------------------------------------------------------------------- 1 | import sys 2 | input = sys.stdin.readline 3 | 4 | def is_cover(x,y,pad): 5 | if paper[pad]==5: 6 | return False 7 | for i in range(x,x+pad): 8 | for j in range(y,y+pad): 9 | if not (0<=i<10 and 0<=j<10 and board[i][j]==1): 10 | return False 11 | return True 12 | 13 | def cover(x,y,pad,t): 14 | for i in range(x,x+pad): 15 | for j in range(y,y+pad): 16 | board[i][j]=t 17 | 18 | def check(): 19 | for i in range(10): 20 | for j in range(10): 21 | if board[i][j]==1: 22 | return False 23 | return True 24 | 25 | def dfs(idx): 26 | global ans 27 | if idx==100: 28 | if check(): 29 | ans = min(ans,sum(paper[1:])) 30 | return 31 | 32 | if ans < sum(paper[1:]): 33 | return 34 | else: 35 | r = idx// 10 36 | c = idx % 10 37 | if board[r][c]==1: 38 | for p in range(5,0,-1): 39 | if is_cover(r,c,p): 40 | cover(r,c,p,0) 41 | paper[p]+=1 42 | dfs(idx+1) 43 | paper[p]-=1 44 | cover(r,c,p,1) 45 | else: 46 | dfs(idx+1) 47 | 48 | 49 | ans = 26 50 | board = [list(map(int,input().split())) for _ in range(10)] 51 | paper = [-1,0,0,0,0,0] 52 | dfs(0) 53 | print(ans if ans!=26 else -1) -------------------------------------------------------------------------------- /재미있는_오슬로.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.stdin = open("input.txt","r") 3 | 4 | dx = [0,0,1,-1,1,1,-1,-1] 5 | dy = [-1,1,0,0,1,-1,-1,1] 6 | 7 | testCase = int(input()) 8 | 9 | for tc in range(1,testCase+1): 10 | n,m=map(int,input().split()) 11 | board=[[0 for _ in range(n)] for _ in range(n)] 12 | mid=(n+1)//3 13 | 14 | board[mid][mid],board[mid+1][mid+1]=2,2 15 | board[mid+1][mid],board[mid][mid+1]=1,1 16 | 17 | 18 | for _ in range(m): 19 | x,y,k = map(int,input().split()) 20 | x,y=y-1,x-1 21 | board[x][y]=k 22 | for i in range(8): 23 | tx,ty=x,y 24 | flag=False 25 | while True: 26 | tx+=dx[i] 27 | ty+=dy[i] 28 | if 0>tx or n<=tx or 0>ty or n<=ty or board[tx][ty]==0: 29 | break 30 | if board[tx][ty]==k: 31 | flag=True 32 | break 33 | 34 | if flag: 35 | tmpx,tmpy=x,y 36 | while tmpx!=tx or tmpy!=ty: 37 | board[tmpx][tmpy] = k 38 | tmpx+=dx[i] 39 | tmpy+=dy[i] 40 | 41 | black,white=0,0 42 | for i in range(n): 43 | for j in range(n): 44 | if board[i][j]==1: 45 | black+=1 46 | elif board[i][j]==2: 47 | white+=1 48 | print("#{} {} {}".format(tc,black,white)) -------------------------------------------------------------------------------- /활주로_건설.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.stdin = open("input.txt","r") 3 | 4 | testCase = int(input()) 5 | 6 | 7 | def check(arr,x): 8 | tmp,cnt = arr[0],1 9 | cntlist=[] 10 | for i in range(1,len(arr)): 11 | if arr[i]!=tmp: 12 | if abs(arr[i]-tmp)>1: 13 | return False 14 | cntlist.append(cnt) 15 | cnt=1 16 | if arr[i]-tmp < 0: 17 | cntlist.append('D') 18 | else: 19 | cntlist.append('U') 20 | else: 21 | cnt+=1 22 | tmp = arr[i] 23 | else: 24 | cntlist.append(cnt) 25 | for i in range(len(cntlist)): 26 | if cntlist[i]=='D': 27 | if cntlist[i+1] < x: 28 | return False 29 | cntlist[i+1]-=x 30 | elif cntlist[i]=='U': 31 | if cntlist[i-1] < x: 32 | return False 33 | cntlist[i-1]-=x 34 | 35 | return True 36 | 37 | 38 | 39 | for tc in range(1,testCase+1): 40 | n,x = map(int,input().split()) 41 | a = [list(map(int,input().split())) for _ in range(n)] 42 | 43 | ans=0 44 | 45 | for i in range(n): 46 | row,col=[],[] 47 | for j in range(n): 48 | row.append(a[i][j]) 49 | col.append(a[j][i]) 50 | 51 | if check(row,x): 52 | ans+=1 53 | if check(col,x): 54 | ans+=1 55 | 56 | 57 | print("#{} {}".format(tc,ans)) -------------------------------------------------------------------------------- /보호필름.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.stdin = open("input.txt","r") 3 | 4 | import copy 5 | testCase = int(input()) 6 | 7 | ans = 987654321 8 | 9 | def check(a,k): 10 | d,w = len(a),len(a[0]) 11 | for i in range(w): 12 | tmp = [a[0][i],1] 13 | for j in range(1,d): 14 | if tmp[0] == a[j][i]: 15 | tmp[1]+=1 16 | if tmp[1]>=k: 17 | break 18 | else: 19 | tmp[0],tmp[1] = a[j][i],1 20 | else: 21 | return False 22 | return True 23 | 24 | def dfs(idx,k,a,cnt): 25 | global ans 26 | if idx == len(a): 27 | if check(a,k): 28 | if ans > cnt: 29 | ans = cnt 30 | else: 31 | dfs(idx+1,k,a,cnt) 32 | if cnt+1 < k: 33 | tmp = [ x for x in a[idx]] 34 | 35 | for i in range(len(a[idx])): 36 | a[idx][i] = 0 37 | dfs(idx+1,k,a,cnt+1) 38 | 39 | for i in range(len(a[idx])): 40 | a[idx][i] = 1 41 | dfs(idx+1,k,a,cnt+1) 42 | 43 | for i in range(len(a[idx])): 44 | a[idx][i] = tmp[i] 45 | 46 | 47 | 48 | 49 | for tc in range(1,testCase+1): 50 | d,w,k = map(int,input().split()) 51 | a = [ list(map(int,input().split())) for _ in range(d)] 52 | ans = k 53 | dfs(0,k,a,0) 54 | if k==1: 55 | print('#{} {}'.format(tc,0)) 56 | else: 57 | print("#{} {}".format(tc,ans)) 58 | 59 | -------------------------------------------------------------------------------- /연구소.py: -------------------------------------------------------------------------------- 1 | import sys 2 | input = sys.stdin.readline 3 | 4 | from itertools import combinations as comb 5 | from collections import deque 6 | 7 | n,m = map(int,input().split()) 8 | a = [list(map(int,input().split())) for _ in range(n)] 9 | #바이러스 위치 10 | v_idx=[] 11 | 12 | dx = [0,0,-1,1] 13 | dy = [1,-1,0,0] 14 | 15 | # v_idx 중에서 *가 아닌 것들을 m개를 bfs 돌리자 16 | def bfs_check(): 17 | q = deque([]) 18 | ch = [[0 for _ in range(n)] for _ in range(n)] 19 | for v in v_idx: 20 | if a[v[0]][v[1]]==2: 21 | q.append([v[0],v[1],0]) 22 | tmp_ans = 0 23 | while q: 24 | x,y,cnt = q.popleft() 25 | 26 | for i in range(4): 27 | nx,ny = x+dx[i],y+dy[i] 28 | if 0<=nxans: 14 | ans = len(track) 15 | for i in range(4): 16 | nx,ny = x+dx[i],y+dy[i] 17 | if 0<=nx0: 20 | track.append([nx,ny]) 21 | search(a,[nx,ny],track,k,isCut) 22 | track.pop() 23 | elif -1*k= st_point: 48 | st_point = a[i][j] 49 | 50 | for i in range(n): 51 | for j in range(n): 52 | if a[i][j]==st_point: 53 | st.append([i,j]) 54 | 55 | 56 | for s in st: 57 | search(a,s,[[s[0],s[1]]],k,False) 58 | 59 | print("#{} {}".format(tc,ans)) 60 | -------------------------------------------------------------------------------- /낚시왕.py: -------------------------------------------------------------------------------- 1 | import sys 2 | input = sys.stdin.readline 3 | 4 | # 상 하 우 좌 5 | dx = [0,-1,1,0,0] 6 | dy = [0,0,0,1,-1] 7 | 8 | di = {} 9 | ans = 0 10 | def move_shark(di,r,c): 11 | tmp = {} 12 | for k,v in di.items(): 13 | # 위치 14 | x,y = map(int,k.split()) 15 | 16 | # 속력 이동방향 크기 17 | s,d,z = v 18 | 19 | # s 줄이기 20 | base_len = (r-1)*2 if d<3 else (c-1)*2 21 | s = (s % base_len) 22 | 23 | # move 24 | nx,ny = x,y 25 | for _ in range(s): 26 | tmp_nx, tmp_ny = nx + dx[d], ny + dy[d] 27 | if tmp_nx<1 or tmp_nx> r or tmp_ny<1 or tmp_ny> c: 28 | if d%2==0: 29 | d -=1 30 | else: 31 | d +=1 32 | nx,ny = nx+dx[d],ny+dy[d] 33 | 34 | # check 35 | string = "{} {}".format(nx,ny) 36 | if string in tmp: 37 | if tmp[string][2] > z: 38 | continue 39 | tmp[string] = [s,d,z] 40 | 41 | return tmp 42 | 43 | def catch_shark(idx,r,di): 44 | global ans 45 | for i in range(1,r+1): 46 | string = "{} {}".format(i,idx) 47 | if string in di: 48 | ans += di[string][2] 49 | del di[string] 50 | break 51 | 52 | r,c,m = map(int,input().split()) 53 | for _ in range(m): 54 | x,y,s,d,z = map(int,input().split()) 55 | string = "{} {}".format(x,y) 56 | di[string]=[s,d,z] 57 | 58 | for i in range(1,c+1): 59 | catch_shark(i,r,di) 60 | di = move_shark(di,r,c) 61 | 62 | print(ans) -------------------------------------------------------------------------------- /파핑파핑_지뢰찾기.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.stdin = open("input.txt","r") 3 | 4 | from collections import deque 5 | testCase = int(input()) 6 | 7 | dx=[0,0,-1,1,-1,-1,1,1] 8 | dy=[1,-1,0,0,1,-1,1,-1] 9 | 10 | def ret_num(x,y): 11 | res = 0 12 | for i in range(8): 13 | nx,ny = x + dx[i],y+dy[i] 14 | if 0<=nx0 and ch[i][j]==0: 48 | cnt += 1 49 | 50 | return cnt 51 | 52 | for tc in range(1,testCase+1): 53 | n = int(input()) 54 | a = [list(map(str,input().rstrip())) for _ in range(n)] 55 | 56 | ans = 0 57 | for i in range(n): 58 | for j in range(n): 59 | if a[i][j]=='.': 60 | a[i][j] = ret_num(i,j) 61 | 62 | ans = count_a() 63 | print('#{} {}'.format(tc,ans)) 64 | 65 | 66 | -------------------------------------------------------------------------------- /디저트카페.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.stdin = open("input.txt","r") 3 | 4 | testcase = int(input()) 5 | ans = 0 6 | # 0:오른아래 1:왼아래 2:왼쪽위 3:오른위 7 | dx = [1,1,-1,-1] 8 | dy = [1,-1,-1,1] 9 | 10 | def possible_start_point(i,j,a): 11 | n = len(a) 12 | if 0<=i+dx[0] ans : 23 | ans = candi 24 | else: 25 | present_dir = dir_list[-1] 26 | nx,ny = i + dx[present_dir],j+dy[present_dir] 27 | if 0<=nx 0 and ans < houseCnt: 58 | flag = True 59 | ans = houseCnt 60 | if flag: 61 | break 62 | 63 | print(ans) 64 | -------------------------------------------------------------------------------- /벽돌깨기.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from collections import deque 3 | from copy import deepcopy 4 | sys.stdin = open("input.txt","r") 5 | 6 | testCase = int(input()) 7 | INF = 987654321 8 | ans = INF 9 | 10 | dx = [0,0,1,-1] 11 | dy = [1,-1,0,0] 12 | 13 | def Shoot(arr,line): 14 | # 0<= line < w 15 | w,h = len(arr[0]),len(arr) 16 | st=[-1,line] 17 | arr = deepcopy(arr) 18 | for i in range(h): 19 | if arr[i][line] != 0: 20 | st[0]=i 21 | break 22 | 23 | if st[0]<0: 24 | return arr 25 | ch = [[0 for _ in range(w)] for _ in range(h)] 26 | q = deque([st]) 27 | ch[st[0]][st[1]]=1 28 | 29 | # bfs check 30 | while q: 31 | x,y = q.popleft() 32 | 33 | for i in range(4): 34 | tx,ty = x,y 35 | for _ in range(arr[x][y]-1): 36 | tx +=dx[i] 37 | ty +=dy[i] 38 | if 0<=tx0: 65 | cnt+=1 66 | if cnt < ans: 67 | ans = cnt 68 | else: 69 | for i in range(w): 70 | FindMinBlock(idx+1,n,w,h,Shoot(arr,i)) 71 | 72 | 73 | 74 | for tc in range(1,testCase+1): 75 | n,w,h=map(int,input().split()) 76 | arr = [list(map(int,input().split())) for _ in range(h)] 77 | 78 | FindMinBlock(0,n,w,h,arr) 79 | 80 | print("#{} {}".format(tc,ans)) 81 | ans = INF 82 | -------------------------------------------------------------------------------- /원판돌리기.py: -------------------------------------------------------------------------------- 1 | import sys 2 | input = sys.stdin.readline 3 | from collections import deque 4 | 5 | dx = [-1,1,0,0] 6 | dy = [0,0,-1,1] 7 | 8 | 9 | def make_average(): 10 | sum_board=[] 11 | for i in range(1,n+1): 12 | for j in range(m): 13 | if board[i][j]!='X': 14 | sum_board.append(board[i][j]) 15 | if len(sum_board)==0: 16 | avg_board = 0 17 | else: 18 | avg_board = sum(sum_board)/len(sum_board) 19 | for i in range(1,n+1): 20 | for j in range(m): 21 | if board[i][j]!='X'and board[i][j]>avg_board: 22 | board[i][j]-=1 23 | elif board[i][j]!='X' and board[i][j]< avg_board: 24 | board[i][j]+=1 25 | 26 | 27 | # 인접수 지워버리기 28 | def check(): 29 | flag = False 30 | for i in range(1,n+1): 31 | # 각 판마다 인접 확인 32 | for j in range(m): 33 | # 현재 검사하려는 숫자 34 | cur = board[i][j] 35 | if cur != 'X': 36 | q = deque([[i,j]]) 37 | while q: 38 | x,y = q.popleft() 39 | for _ in range(4): 40 | ni,nj = x + dx[_],(y+dy[_])%(m) 41 | if 1<=ni<=n and board[ni][nj]!='X' and board[ni][nj]==cur: 42 | flag = True 43 | board[ni][nj]='X' 44 | board[i][j]='X' 45 | q.append([ni,nj]) 46 | 47 | return flag 48 | 49 | # xi 의 배수를 di 방향으로 ki 칸 회전하라 50 | # di:0 시계 di:1 반시계 51 | 52 | def rotate(xi,di,ki): 53 | for i in range(xi,n+1,xi): 54 | #반시계 55 | if di: 56 | for _ in range(ki): 57 | board[i].append(board[i].popleft()) 58 | #시계 59 | else: 60 | for _ in range(ki): 61 | board[i].appendleft(board[i].pop()) 62 | 63 | if not check(): 64 | make_average() 65 | 66 | n,m,t = map(int,input().split()) 67 | board = [deque(list(map(int,input().split()))) for _ in range(n)] 68 | board.insert(0,[-1]) 69 | 70 | for _ in range(t): 71 | xi,di,ki = map(int,input().split()) 72 | rotate(xi,di,ki) 73 | 74 | ans = 0 75 | for i in range(1,n+1): 76 | for j in range(m): 77 | if board[i][j]!='X': 78 | ans += board[i][j] 79 | print(ans) -------------------------------------------------------------------------------- /점심식사시간.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.stdin = open("input.txt","r") 3 | 4 | from copy import deepcopy 5 | testCase = int(input()) 6 | 7 | pCase=[] 8 | def MakepCase(idx,tmp,people,lenp,stairdist): 9 | if idx==lenp: 10 | pCase.append(deepcopy(tmp)) 11 | else: 12 | for i in range(2): 13 | tmp.append([0,people[idx][i]+stairdist[i],i]) 14 | MakepCase(idx+1,tmp,people,lenp,stairdist) 15 | tmp.pop() 16 | def check(case): 17 | for c in case: 18 | if c[0]<=c[1]: 19 | return True 20 | return False 21 | 22 | for tc in range(1,testCase+1): 23 | n=int(input()) 24 | a= [list(map(int,input().split())) for _ in range(n)] 25 | 26 | stairIndex,stairdist=[],[] 27 | for i in range(n): 28 | for j in range(n): 29 | if a[i][j]>1: 30 | stairIndex.append([i,j]) 31 | stairdist.append(a[i][j]) 32 | 33 | people=[] 34 | for i in range(n): 35 | for j in range(n): 36 | if a[i][j]==1: 37 | people.append([abs(i-stairIndex[0][0])+abs(j-stairIndex[0][1]), abs(i-stairIndex[1][0])+abs(j-stairIndex[1][1])]) 38 | pCase=[] 39 | MakepCase(0,[],people,len(people),stairdist) 40 | ans = 987654321 41 | 42 | for case in pCase: 43 | miniute=0 44 | st=[0,0] 45 | while check(case): 46 | #1분 씩 증가 47 | waiter = [] 48 | for i in range(len(case)): 49 | if case[i][0] < case[i][1]-stairdist[case[i][2]]: 50 | case[i][0]+=1 51 | elif case[i][0] == case[i][1]-stairdist[case[i][2]]: 52 | if st[case[i][2]] < 3: 53 | st[case[i][2]]+=1 54 | case[i][0]+=1 55 | elif st[case[i][2]]==3: 56 | waiter.append(i) 57 | elif case[i][1]-stairdist[case[i][2]] < case[i][0] <= case[i][1]: 58 | case[i][0]+=1 59 | if case[i][0]>case[i][1]: 60 | st[case[i][2]]-=1 61 | for w in waiter: 62 | if st[case[w][2]] < 3: 63 | st[case[w][2]]+=1 64 | case[w][0]+=1 65 | 66 | miniute+=1 67 | 68 | if ans > miniute: 69 | ans = miniute 70 | print("#{} {}".format(tc,ans)) -------------------------------------------------------------------------------- /나무_재테크.py: -------------------------------------------------------------------------------- 1 | import sys 2 | input = sys.stdin.readline 3 | 4 | # 한 칸에 여러개의 나무를 심을 수도 있다. 5 | # 가장 처음에 양분은 모든 칸에 5만큼 6 | # 모든 칸에 대해서 조사를 7 | 8 | # 봄에는 나무가 자신의 나이만큼 양분을 먹고, 나이가 1 증가한다 9 | # 하나의 칸에 여러 개의 나무가 있다면, 나이가 어린 나무부터 양분을 먹는다 10 | # 양분이 부족해 자신의 나이만큼 양분을 먹을 수 없는 나무는 양분을 먹지 못하고 즉시 죽는다. 11 | 12 | # 여름에는 각각의 죽은 나무마다 나이를 2로 나눈 값이 나무가 있던 칸에 양분으로 추가된다. 소수점 아래는 버린다. 13 | 14 | # 가을에는 나무가 번식한다. 번식하는 나무는 나이가 5의 배수이어야 하며, 인접한 8개의 칸에 나이가 1인 나무가 생긴다. 15 | 16 | dx = [0,0,-1,1,-1,-1,1,1] 17 | dy = [1,-1,0,0,1,-1,1,-1] 18 | 19 | def spring(): 20 | dead_tree=[] 21 | for i in range(1,n+1): 22 | for j in range(1,n+1): 23 | if len(age_board[i][j]) > 0: 24 | candi = [] 25 | for c in sorted(age_board[i][j]): 26 | if yang_bun[i][j] >= c: 27 | yang_bun[i][j]-=c 28 | candi.append(c+1) 29 | else: 30 | dead_tree.append([i,j,c//2]) 31 | age_board[i][j] = [x for x in candi] 32 | return dead_tree 33 | def summer(dead_tree): 34 | for d in dead_tree: 35 | yang_bun[d[0]][d[1]] += d[2] 36 | def fall(): 37 | for i in range(1,n+1): 38 | for j in range(1,n+1): 39 | if len(age_board)>0: 40 | for c in age_board[i][j]: 41 | if c%5==0: 42 | for _ in range(8): 43 | ni,nj = i+dx[_],j+dy[_] 44 | if 1<=ni<=n and 1<=nj<=n: 45 | age_board[ni][nj].append(1) 46 | def winter(): 47 | for i in range(1,n+1): 48 | for j in range(1,n+1): 49 | yang_bun[i][j]+=winter_y[i][j] 50 | 51 | def one_year_later(): 52 | summer(spring()) 53 | fall() 54 | winter() 55 | 56 | # nxn 들판 / m 개의 나무 심기 / k년후에 살아남은 나무의 수 57 | n,m,k = map(int,input().split()) 58 | winter_y=[[0]*(n+1)] 59 | for _ in range(n): 60 | tmp = list(map(int,input().split())) 61 | tmp.insert(0,0) 62 | winter_y.append(tmp) 63 | yang_bun = [[5]*(n+1) for _ in range(n+1)] 64 | age_board = [[[] for _ in range(n+1)] for _ in range(n+1)] 65 | for _ in range(m): 66 | i,j,age = map(int,input().split()) 67 | age_board[i][j].append(age) 68 | 69 | 70 | for _ in range(k): 71 | one_year_later() 72 | 73 | 74 | ans = 0 75 | for i in range(1,n+1): 76 | for j in range(1,n+1): 77 | ans += len(age_board[i][j]) 78 | print(ans) -------------------------------------------------------------------------------- /줄기세포_배양.py: -------------------------------------------------------------------------------- 1 | 2 | from collections import deque 3 | 4 | dx = [0,0,1,-1] 5 | dy = [1,-1,0,0] 6 | 7 | testCase = int(input()) 8 | def isExpandible(n,m,a): 9 | expandible = False 10 | for i in range(n): 11 | if a[i][0][1]!=0 and a[i][0][0]==a[i][0][1]: 12 | expandible = True 13 | break 14 | if a[i][m-1][1]!=0 and a[i][m-1][0]==a[i][m-1][1]: 15 | expandible = True 16 | break 17 | for j in range(m): 18 | if a[0][j][1]!=0 and a[0][j][0] == a[0][j][1]: 19 | expandible = True 20 | break 21 | if a[n-1][j][1]!=0 and a[n-1][j][0] == a[n-1][j][1]: 22 | expandible = True 23 | break 24 | return expandible 25 | 26 | def expandMap(n,m,a): 27 | row,col = len(a[0]),len(a) 28 | if isExpandible(n,m,a): 29 | tmp = [[[0,0] for _ in range(row*3)] for _ in range(col*3)] 30 | for i in range(col,col*2): 31 | for j in range(row,row*2): 32 | tmp[i][j] = [a[i-col][j-row][0],a[i-col][j-row][1]] 33 | return n*3,m*3,tmp 34 | else: 35 | return n,m,a 36 | 37 | def oneStep(n,m,a): 38 | n,m,a = expandMap(n,m,a) 39 | q = deque([]) 40 | for i in range(n): 41 | for j in range(m): 42 | r,s = a[i][j][0],a[i][j][1] 43 | if r>s: 44 | a[i][j] = [r-1,s] 45 | elif r>0 and r<=s: 46 | a[i][j] = [r-1,s] 47 | q.append([i,j]) 48 | while q: 49 | x,y = q.popleft() 50 | 51 | for i in range(4): 52 | tx = x + dx[i] 53 | ty = y + dy[i] 54 | if (a[tx][ty][0]!=0 and a[tx][ty][0]==a[tx][ty][1]*2 and a[tx][ty][1] < a[x][y][1]) or (a[tx][ty][1]==0): 55 | a[tx][ty]=[a[x][y][1]*2,a[x][y][1]] 56 | 57 | return n,m,a 58 | 59 | 60 | 61 | 62 | for tc in range(1,testCase+1): 63 | n,m,k=map(int,input().split()) 64 | a = [] 65 | for _ in range(n): 66 | tmp,line = [],list(map(int,input().split())) 67 | for l in line: 68 | tmp.append([l*2,l]) 69 | a.append(tmp) 70 | 71 | for _ in range(k): 72 | n,m,a = oneStep(n,m,a) 73 | cnt = 0 74 | for i in range(n): 75 | for j in range(m): 76 | if a[i][j][0]>0: 77 | cnt+=1 78 | print("#{} {}".format(tc,cnt)) 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /미생물격리.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.stdin = open("input.txt","r") 3 | 4 | 5 | from collections import deque 6 | testCase = int(input()) 7 | 8 | dx = [0,-1,1,0,0] 9 | dy = [0,0,0,-1,1] 10 | 11 | def move(a): 12 | n = len(a) 13 | q = deque([]) 14 | for i in range(n): 15 | for j in range(n): 16 | if len(a[i][j]) >0: 17 | q.append([i,j]) 18 | while q: 19 | x,y = q.popleft() 20 | 21 | a[x][y] = [ a[x][y][r] for r in range(len(a[x][y])-1,-1,-1)] 22 | num,dir = a[x][y].pop() 23 | 24 | tx = x + dx[dir] 25 | ty = y + dy[dir] 26 | a[tx][ty].append([num,dir]) 27 | 28 | 29 | def check(a): 30 | n=len(a) 31 | for i in range(n): 32 | if len(a[0][i]) >0: 33 | num,dir = a[0][i].pop() 34 | num = num // 2 35 | dir = dir-1 if dir%2==0 else dir+1 36 | a[0][i].append([num,dir]) 37 | if len(a[n-1][i]) >0: 38 | num,dir = a[n-1][i].pop() 39 | num = num // 2 40 | dir = dir-1 if dir%2==0 else dir+1 41 | a[n-1][i].append([num,dir]) 42 | for i in range(1,n-1): 43 | if len(a[i][0]) >0: 44 | num,dir = a[i][0].pop() 45 | num = num // 2 46 | dir = dir-1 if dir%2==0 else dir+1 47 | a[i][0].append([num,dir]) 48 | if len(a[i][n-1]) >0: 49 | num,dir = a[i][n-1].pop() 50 | num = num // 2 51 | dir = dir-1 if dir%2==0 else dir+1 52 | a[i][n-1].append([num,dir]) 53 | 54 | for i in range(1,n-1): 55 | for j in range(1,n-1): 56 | if len(a[i][j]) > 0: 57 | num,dir = a[i][j][0][0],a[i][j][0][1] 58 | max_num = num 59 | for r in range(1,len(a[i][j])): 60 | if a[i][j][r][0] > max_num: 61 | max_num = a[i][j][r][0] 62 | dir = a[i][j][r][1] 63 | num += a[i][j][r][0] 64 | a[i][j] = [[num,dir]] 65 | 66 | 67 | 68 | for tc in range(1,testCase+1): 69 | n,m,k = map(int,input().split()) 70 | a = [[[] for _ in range(n)] for _ in range(n)] 71 | ans = 0 72 | for _ in range(k): 73 | x,y,num,dir = map(int,input().split()) 74 | a[x][y].append([num,dir]) 75 | 76 | for _ in range(m): 77 | move(a) 78 | check(a) 79 | 80 | for i in range(n): 81 | for j in range(n): 82 | if len(a[i][j])==1: 83 | ans += a[i][j][0][0] 84 | 85 | 86 | print("#{} {}".format(tc,ans)) 87 | 88 | -------------------------------------------------------------------------------- /무선충전.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.stdin = open("input.txt","r") 3 | 4 | from collections import deque 5 | 6 | dx = [0,0,1,0,-1] 7 | dy = [0,-1,0,1,0] 8 | testCase = int(input()) 9 | 10 | def bfs(x,y,c,p,idx,board,idx2charge): 11 | ch = [[0 for _ in range(11)] for _ in range(11)] 12 | board[y][x].append(idx) 13 | idx2charge[idx] = p 14 | ch[y][x]=1 15 | base = [y,x] 16 | q = deque([[y,x]]) 17 | 18 | while q: 19 | y,x = q.popleft() 20 | 21 | for i in range(1,5): 22 | ty = y + dy[i] 23 | tx = x + dx[i] 24 | dist = abs(ty-base[0])+abs(tx-base[1]) 25 | if 1<=ty<=10 and 1<=tx<=10 and ch[ty][tx]==0 and dist <= c: 26 | ch[ty][tx]=1 27 | board[ty][tx].append(idx) 28 | q.append([ty,tx]) 29 | 30 | def charging(aloc,bloc,board,idx2charge,ans): 31 | ax,ay = aloc[0],aloc[1] 32 | bx,by = bloc[0],bloc[1] 33 | ascope = [ x for x in board[ay][ax]] 34 | bscope = [ x for x in board[by][bx]] 35 | if len(ascope) >0 and len(bscope) >0: 36 | max_tmp = -1 37 | for i in range(len(ascope)): 38 | for j in range(len(bscope)): 39 | if ascope[i]==bscope[j]: 40 | tmp = idx2charge[ascope[i]] 41 | else: 42 | tmp = idx2charge[ascope[i]] 43 | tmp += idx2charge[bscope[j]] 44 | if max_tmp < tmp: 45 | max_tmp = tmp 46 | ans += max_tmp 47 | else: 48 | max_tmp = -1 49 | for i in range(len(ascope)): 50 | if idx2charge[ascope[i]] > max_tmp: 51 | max_tmp = idx2charge[ascope[i]] 52 | if max_tmp!=-1: 53 | ans += max_tmp 54 | max_tmp=-1 55 | for i in range(len(bscope)): 56 | if idx2charge[bscope[i]] > max_tmp: 57 | max_tmp = idx2charge[bscope[i]] 58 | if max_tmp !=-1: 59 | ans+= max_tmp 60 | return ans 61 | for tc in range(1,testCase+1): 62 | m,a = map(int,input().split()) 63 | board = [ [[] for _ in range(11)] for _ in range(11)] 64 | atrk = list(map(int,input().split())) 65 | btrk = list(map(int,input().split())) 66 | atrk = [0] + atrk 67 | btrk = [0] + btrk 68 | idx2charge = {} 69 | for idx in range(1,a+1): 70 | x,y,c,p = map(int,input().split()) 71 | bfs(x,y,c,p,idx,board,idx2charge) 72 | 73 | aloc = [1,1] 74 | bloc = [10,10] 75 | ans = 0 76 | for i in range(len(atrk)): 77 | aloc[0] += dx[atrk[i]] 78 | aloc[1] += dy[atrk[i]] 79 | bloc[0] += dx[btrk[i]] 80 | bloc[1] += dy[btrk[i]] 81 | ans = charging(aloc,bloc,board,idx2charge,ans) 82 | print("#{} {}".format(tc,ans)) 83 | -------------------------------------------------------------------------------- /핀볼게임.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.stdin = open("input.txt", "r") 3 | 4 | def nextLoc(x,y,board,dir,wHole,cnt): 5 | reverseDir = {0:2,1:3,2:0,3:1} 6 | tx = x + dx[dir] 7 | ty = y + dy[dir] 8 | if board[tx][ty]==0: 9 | return tx,ty,dir,cnt 10 | elif board[tx][ty]==-1: 11 | return -1,-1,-1,cnt 12 | elif 1<=board[tx][ty]<=5: 13 | if (board[tx][ty]==1 and dir==3) or (board[tx][ty]==1 and dir==2): 14 | if dir==3: 15 | return tx,ty,0,cnt+1 16 | else: 17 | return tx,ty,1,cnt+1 18 | elif (board[tx][ty]==2 and dir==3) or (board[tx][ty]==2 and dir==0): 19 | if dir==3: 20 | return tx,ty,2,cnt+1 21 | else: 22 | return tx,ty,1,cnt+1 23 | elif (board[tx][ty]==3 and dir==1) or (board[tx][ty]==3 and dir==0): 24 | if dir==1: 25 | return tx,ty,2,cnt+1 26 | else: 27 | return tx,ty,3,cnt+1 28 | elif (board[tx][ty]==4 and dir==1) or (board[tx][ty]==4 and dir==2): 29 | if dir==1: 30 | return tx,ty,0,cnt+1 31 | else: 32 | return tx,ty,3,cnt+1 33 | else: 34 | return tx,ty,reverseDir[dir],cnt+1 35 | else: 36 | string ='{} {}'.format(tx,ty) 37 | wx,wy = map(int,wHole[string].split()) 38 | return wx,wy,dir,cnt 39 | 40 | 41 | def move(i,j,board,dir,wHole): 42 | st = [i,j] 43 | cnt = 0 44 | x,y=i,j 45 | while True: 46 | x,y,dir,cnt=nextLoc(x,y,board,dir,wHole,cnt) 47 | if x==st[0] and y==st[1]: 48 | break 49 | if x==-1: 50 | break 51 | return cnt 52 | 53 | # 상 우 하 좌 54 | dx = [-1,0,1,0] 55 | dy = [0,1,0,-1] 56 | 57 | testCase = int(input()) 58 | 59 | for tc in range(1,testCase+1): 60 | n = int(input()) 61 | board = [[5]*(n+1)] 62 | for _ in range(n): 63 | tmp = list(map(int,input().split())) 64 | tmp.insert(0,5) 65 | tmp.append(5) 66 | board.append(tmp) 67 | board.append([5]*(n+1)) 68 | wHole = {} 69 | for i in range(1,n+1): 70 | for j in range(1,n+1): 71 | if board[i][j]>=6: 72 | if board[i][j] in wHole: 73 | wHole[wHole[board[i][j]]]='{} {}'.format(i,j) 74 | wHole['{} {}'.format(i,j)]=wHole[board[i][j]] 75 | else: 76 | wHole[board[i][j]]='{} {}'.format(i,j) 77 | 78 | ans = 0 79 | for i in range(1,n+1): 80 | for j in range(1,n+1): 81 | if board[i][j]==0: 82 | for k in range(4): 83 | tmp = move(i,j,board,k,wHole) 84 | if tmp > ans: 85 | ans = tmp 86 | 87 | print('#{} {}'.format(tc,ans)) 88 | -------------------------------------------------------------------------------- /게리멘더링2.py: -------------------------------------------------------------------------------- 1 | # 5개의 선거구로 나눠야 한다. 2 | # 선거구는 적어도 하나를 포함 3 | # 한 선거구의 지역은 모두 연결 4 | # 중간에 통하는 인접한 구역은 0개 이 5 | 6 | import sys 7 | input = sys.stdin.readline 8 | from collections import deque 9 | dx = [0,0,-1,1] 10 | dy = [1,-1,0,0] 11 | ans = 987654321 12 | def check_make_5_loc(x,y,d1,d2): 13 | # 경계선1 14 | for i in range(d1 + 1): 15 | if not (1<=x+i<=n and 1<=y-i<=n): 16 | return False 17 | # 경계선2 18 | for i in range(d2 + 1): 19 | if not (1 <= x + i <= n and 1 <= y + i <= n): 20 | return False 21 | # 경계선3 22 | for i in range(d2 + 1): 23 | if not (1 <= x + d1 + i <= n and 1 <= y - d1 + i <= n): 24 | return False 25 | # 경계선4 26 | for i in range(d1 + 1): 27 | if not (1 <= x + d2 + i <= n and 1 <= y + d2 - i <= n): 28 | return False 29 | return True 30 | 31 | def get_sub(area): 32 | ingu=[0]*5 33 | 34 | for i in range(1,n+1): 35 | for j in range(1,n+1): 36 | ingu[area[i][j]-1]+=people[i][j] 37 | 38 | ingu.sort() 39 | return ingu[-1]-ingu[0] 40 | 41 | def div_area(d1,d2): 42 | global ans 43 | for x in range(1,n+1): 44 | for y in range(1,n+1): 45 | area = [[0] * (n + 1) for _ in range(n+1)] 46 | if x+d1+d2<=n and y-d1