Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Hongjoo/lv2/멀리뛰기.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
answer = 0
def solution(n):

def backtracking(path,lv):
# 재귀 종료
global answer
if lv >= len(path):
if sum(path)==n :
answer += 1
# print(f"{lv} : {path} >{answer}")
return 0
#자식 노드 이동
for x in [1,2] :
if sum(path) + x <= n :
path[lv] = x
backtracking(path , lv+1)
# backtracking
path[lv] = 0

for m in range(1,n+1) :
backtracking([0]*m , 0)
return answer%1234567
90 changes: 90 additions & 0 deletions Hongjoo/백준/뿌요뿌요.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
"""
20.19
https://www.acmicpc.net/problem/11559
BOJ.#11559_Gold4

#problem
- goal) 연쇄 "연속" 횟수 구하기
[condition]
- 1연쇄 : 해당 turn 에서 상하좌우 4개 연결 -> 삭제
- 삭제 후 위에 있는 element 는 하강

- 입력: 현 filed 상황(12x6)
- (빈공간) / R, G, B, P, Y
- 빈공간 : 0
- 색상 : R,G, B, P, Y = 1,2,3,4,5
[flow] # BFS
1. 상하좌우 연쇄 확인
- 연쇄 확인
- 삭제
- 연쇄 횟수 증가

"""
import sys
from collections import deque
input = sys.stdin.readline
#1. field 현황 리스트에 담기
field = [list(input())[:-1] for _ in range(12)]

#2. bfs
dy = [-1,1,0,0]
dx =[0,0,-1,1]


def refine_field(x) : # 중간 빈자리
stack = deque()
#1.아레=> 위로 스택에 뿌요뿌요 순서대로 축척하기
for ny in range(11, -1,-1):
if field[ny][x] != ".":
stack.append(field[ny][x])
for ny in range(11, -1, -1) :
if stack :
field[ny][x] = stack.popleft()
else :
field[ny][x] = "."
Comment on lines +34 to +44
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 위에서 아래로 내리는 코드가 아래 첨부한 코드처럼 3중 반복문을 도는데, 홍주님 코드처럼 stack을 사용하게 되면 O(N)으로 할 수 있군요!! 감사합니다!!

def down():
    for y in range(FIELD_Y):
        for x in range(10, -1, -1):
            for k in range(FIELD_X - 1, x, -1):
                if field[x][y] != '.' and field[k][y] == '.':
                    field[k][y] = field[x][y]
                    field[x][y] = '.'


# 연쇄 확인 및 터짐
def bfs(sy,sx):
q = deque()
q.append([sy,sx])
Comment on lines +48 to +49
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분은 queue=deque([[sy, sx])로 줄일 수 있을 것 같아요!

pop_positions = [[sy,sx]]
cur_color = field[sy][sx]
visited.append([sy,sx])
while q :
cy,cx = q.popleft()
for d in range(4):
ny ,nx = cy + dy[d] ,cx + dx[d]
# 같은 색상 -> 삭제 등록하기
if 0<= ny<12 and 0<= nx < 6 and [ny,nx] not in visited and field[ny][nx] == cur_color :
q.append([ny,nx])
pop_positions.append([ny,nx])
visited.append([ny,nx])

#2) 터짐 확인
if len(pop_positions) >= 4 :
for y,x in pop_positions :
field[y][x] = "."
return True
return False# 안 터짐

answer = 0
flag = True
k = 0
while flag :
visited = []
flag =False
#1. 전체 field에서 뿌요뿌요 탐색
for i in range(12):
for j in range(6):
if field[i][j]!="." and [i,j] not in visited:
now_f = bfs(i,j) # 2.해당 위치에서 터짐 여부 확인
flag = flag or now_f
#2. 연쇄 계수 추가
if not flag : # False - 안터짐
break
else :
for row in range(6):
refine_field(row)
# 현재 turn 에서 1번 이상 터짐
answer += 1
print(answer)
50 changes: 50 additions & 0 deletions Hongjoo/백준/장군.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

import sys
from collections import deque
INF = 1e9
# 상하좌우
dy = [-1,1,0,0]
dx = [0,0,-1,1]

dl= [[-1,1],[1,1],[1,-1],[-1,-1]] # 우상 . 우하 / 좌하 / 좌상
dl_combined = [0,3,1,2,2,3,0,1]
Comment on lines +6 to +10
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

장군 문제는 8가지의 이동 경로를 어떻게 처리하느냐가 관건이었던 것 같아요! 그만큼 발제자님, 저, 홍주님의 경로 표현 방식이 다 다른 게 인상적이네요

저는 아래와 같이 표현했어요!

# 8가지 이동 경로
directions = [
    # 상
    ((-1, 0), (-1, 1), (-1, 1)),    # 오른쪽 위
    ((-1, 0), (-1, -1), (-1, -1)),  # 왼쪽 위
    # 하
    ((1, 0), (1, 1), (1, 1)),       # 오른쪽 아래
    ((1, 0), (1, -1), (1, -1)),     # 왼쪽 아래
    # 좌
    ((0, -1), (-1, -1), (-1, -1)),  # 왼쪽 위
    ((0, -1), (1, -1), (1, -1)),    # 왼쪽 아래
    # 우
    ((0, 1), (-1, 1), (-1, 1)),     # 오른쪽 위
    ((0, 1), (1, 1), (1, 1))        # 오른쪽 아래
]

sy ,sx= map(int,sys.stdin.readline().split())
ty,tx = map(int, sys.stdin.readline().split())

field = [[INF]*9 for _ in range(10)]
field[sy][sx] = 0
field[ty][tx] = -1

q = deque()
q.append([sy,sx])

def check_bound(y,x) :
if 0<= y <=9 and 0<= x <=8 and field[y][x]!=-1 : # 기물 x , field 안에 , 방문 상관x => 왕 위치만 안됨x
return True
return False
def check_bound2(y,x) :
if 0<= y <=9 and 0<= x <=8 : # 기물 가능
return True
return False

while q :
cy,cx = q.popleft()
for i in range(8):
ny1, nx1 = cy +dy[i//2] , cx+dx[i//2]
ny2, nx2 = ny1 + dl[dl_combined[i]][0] , nx1 + dl[dl_combined[i]][1]
ny3 , nx3 = ny2 + dl[dl_combined[i]][0] , nx2 + dl[dl_combined[i]][1]

if check_bound(ny1, nx1) and check_bound(ny2, nx2) and check_bound2(ny3, nx3) :
if ny3 == ty and nx3 == tx :
# print(f"succes====")
print(field[cy][cx] +1)

exit()
elif field[cy][cx] +1 <= field[ny3][nx3] :
field[ny3][nx3] =field[cy][cx] +1 # 업데이트
# print(f"{cy}{cx} -> {i}: {ny1}{nx1} / {ny2}{nx2} / {ny3}, {nx3} => field{field[ny3][nx3]} ")

q.append([ny3,nx3])
# print(f"{cy}{cx} -> {i}: {ny1}{nx2} / {ny2}{nx2} / {ny3}, {nx3} => field{field[ny3][nx3]} ")

print(field[ty][tx])
60 changes: 60 additions & 0 deletions Hongjoo/백준/토마토.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""
https://www.acmicpc.net/problem/7576
# BFS, DFS
(1) filed 값 중 1인 값 찾기
(2) 상하좌우로 -1 빼고 확산
(3) flag로 해당 이잔 turn에서 확산 유무를 판단 -> 안되면 turn 종료

Comment on lines +1 to +7
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

토마토 문제는 지난 주에 제가 발제했던 문제였는데, 로직을 잘 구현하셨네요!!
정답 풀이는 아래 발제 자료를 참고해주시면 되겠습니당!
바로가기

"""
# 입력
import sys
from collections import deque

input = sys.stdin.readline
N,M = map(int, input().split())

field = [list(map(int,input().split())) for _ in range(M) ]
# print(field)
dy = [-1,1,0,0]
dx = [-0,0,-1,1]
# BFS


lv = 0
burn = []
q = deque()
for m in range(M) :
for n in range(N) :
# print(m , n)
if field[m][n] == 1:
# 안 익은 토마토 익히기
q.append([m,n])

while q :
# 각 turn 마다 토마도 전염시키기

# print(f"{t}턴 - {m}{n}")
#[2] 익은 토마토 확산시키기
# t+= 1
y,x = q.popleft()
for d in range(4):
ny = y + dy[d] ; nx = x + dx[d]
if 0<= ny <M and 0 <= nx < N and field[ny][nx] == 0:
field[ny][nx] = field[y][x] +1
q.append([ny,nx])

max_num = 0
for i in range(M) :
for j in range(N):
if field[i][j]== 0 :
print(-1)
exit()
max_num = max(max_num , field[i][j])
print(max_num-1)

# burn.append([n,m])
# q = deque()
# for sy,sx in burn :
# q.append([sy,sx])
# while q :