Skip to content

Commit d653d5b

Browse files
committed
2 parents 8699457 + d974637 commit d653d5b

11 files changed

+418
-0
lines changed

Hongjoo/lv2/오픈채팅방.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""2024.12.14, 30min , #구현"""
2+
def solution(record):
3+
answer = []
4+
# 1. history와 user_db DB 정의
5+
user_db = {} # key=uid , value = username
6+
history = [] # ["Enter / Leave" , uid ]
7+
8+
#2. history와 user_db 기록 시작
9+
for r in record :
10+
command = r.split() # command = [동사V ,userid ,(username)]
11+
v = command[0]
12+
uid = command[1]
13+
# 2-1 Enter : user_db 추가 , history 추가
14+
if v == 'Enter' :
15+
user_db[uid] = command[2] # user_db[uid] = username
16+
history.append([uid ,v]) # history: [uid , "Enter" ]
17+
#2-2 Leave :history 추가
18+
elif v == 'Leave':
19+
history.append([uid,v]) # history: [uid , "Leave" ]
20+
#2-3 Change : user_db 수정(username 변경)
21+
elif v == 'Change':
22+
user_db[uid] = command[2] #user_db[uid] = new_username
23+
24+
# 3.user_db, histoy 기반으로 문자열 배열 result를 만들기
25+
result = list()
26+
# 영어 v 을 한국어 동사로 번역
27+
en_to_ko = {'Enter':'들어왔습니다.','Leave' : '나갔습니다.'}
28+
for uid , h_v in history :
29+
user_name = user_db[uid]
30+
result.append(f"{user_name}님이 {en_to_ko[h_v]}" )
31+
32+
return result

Hongjoo/백준/테트로미노.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""
2+
2024.12.14, 과제문제, 2hour
3+
"""
4+
5+
import sys
6+
7+
#0. 입력 변수를 통해 각 칸에 정수값을 가지는 NxM 행 graph 정의
8+
N,M = map(int,sys.stdin.readline().split())
9+
graph=[[0 for _ in range(M)] for k in range(N)]
10+
for i in range(N) :
11+
graph[i] = list( map(int,sys.stdin.readline().split()))
12+
13+
max_num = 0
14+
#2. graph 내 특정 칸(절대좌표 , (row,col)) 기준에서
15+
#가질 수 있는 19개 형태의 테트로미노 영역 속 정수들의 합의 최대 값 찾기
16+
#함수 내부에선 상대좌표(r,c)로 칸의 위치 파악
17+
def tetries_sum (row,col):
18+
sub_maxsum = 0
19+
#2-1. 19개 테트로미노는 크게 4가지 직사각형 형태의 영역으로 소분류
20+
for n,m in [[1,4],[2,3],[3,2],[4,1]]:
21+
sub_sum=0 # 특정 칸(row,col)에서 가질 수 있는 정수합 중 최대값
22+
23+
#2-2. 19개 중 graph(NxM) 안에서 가질 수 있는 테트로미노만 적용
24+
if row>=0 and row + n <=N and col>=0 and col+m <=M :
25+
#(1) 직사각형(4가지) 영역내 모든 정수값들의 합
26+
for r in range(n):
27+
for c in range(m):
28+
29+
sub_sum +=graph[row + r][col+ c]
30+
#(2) 2x3 과 3x2 형태 영역 중 빠져야 하는 2개 칸들의 정수값의 합 구하기
31+
if n==2 and m==3 or n==3 and m==2 :
32+
del_tmp = list()
33+
#a. 2x3 직사각형 영역 중 빠져야 하는 2개 칸들의 조합(상대좌표) : 10개
34+
if n == 2 and m == 3 :
35+
del_candidates = [[[0,0],[0,1]],[[0,1],[0,2]],[[1,0],[1,1]],[[1,1],[1,2]],
36+
[[0,0],[1,0]],[[0,2],[1,2]],[[0,0],[1,2]],[[0,2],[1,0]],[[0,0],[0,2]],[[1,0],[1,2]]
37+
]
38+
#b. 3x2 직사각형 영역 중 빠져야 하는 2개 칸들의 조합(상대좌표) : 10개
39+
elif n==3 and m == 2 :
40+
del_candidates = [[[0,0],[0,1]],
41+
[[0,0],[1,0]],[[0,1],[1,1]],[[1,0],[2,0]],[[1,1],[2,1]],[[2,0],[2,1]],
42+
[[0,0],[2,1]],[[2,0],[0,1]],[[0,0],[2,0]],[[0,1],[2,1]]]
43+
#(3) 빼야하는 2개 칸들의 조합들(10종류)에 대해 정수값의 합 구하기
44+
for n1,n2 in del_candidates:
45+
del_tmp.append(graph[row +n1[0]][col+ n1[1]] +graph[row+n2[0]][col+n2[1]])
46+
#(4) 2x3 또는 3x2 영역에서 합의 최대값을 구하기 위해 10개의 뺄 값 중 최소값으로 빼기
47+
sub_sum -= min(del_tmp)
48+
49+
sub_maxsum = max(sub_maxsum , sub_sum) # 최대값 업데이트
50+
51+
52+
else : #테트로미노가 graph 범위를 벗어남 -> skip
53+
continue
54+
return sub_maxsum # 절대좌표(row,col) 칸 기준에서 가질 수 있는 합의 최대
55+
56+
max_sum = 0 #전체 graph 내 합의 최대값
57+
# 1. graph 내 모든 칸을 순회
58+
for i in range(N) :
59+
for j in range(M) :
60+
new_sub_sum=tetries_sum(i,j) # 2. 절대 좌표(i,j) 칸 기준에서 가질 수 있는 합의 최대 값 계산
61+
max_sum= max(max_sum,new_sub_sum) #3.전체 graph 내 합의 최대값 업데이트
62+
print(max_sum)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
'''
2+
BOJ #13460. 구슬 탈출2 (골드2)
3+
https://www.acmicpc.net/problem/13460
4+
유형: BFS
5+
출처 : https://hstory0208.tistory.com/entry/Python%ED%8C%8C%EC%9D%B4%EC%8D%AC-%EB%B0%B1%EC%A4%80-13460-%EA%B5%AC%EC%8A%AC-%ED%83%88%EC%B6%9C2
6+
'''
7+
8+
from collections import deque
9+
import sys
10+
input = sys.stdin.readline
11+
12+
n, m = map(int, input().split()) # 행, 열
13+
14+
board = [list(input().rstrip()) for _ in range(n)]
15+
visited = []
16+
17+
dx = [1, -1, 0, 0]
18+
dy = [0, 0, -1, 1]
19+
cnt = 0
20+
21+
def getPos():
22+
rx, ry, bx, by = 0, 0, 0, 0
23+
for x in range(n):
24+
for y in range(m):
25+
if board[x][y] == "R":
26+
rx, ry = x, y
27+
if board[x][y] == "B":
28+
bx, by = x, y
29+
return rx, ry, bx, by
30+
31+
32+
def move(x, y, dx, dy):
33+
cnt = 0
34+
# 이동하는 위치가 벽이아니고, 구멍에 들어가지 않을 동안 반복
35+
while board[x + dx][y + dy] != "#" and board[x][y] != "O":
36+
x += dx
37+
y += dy
38+
cnt +=1
39+
return x, y, cnt
40+
41+
def bfs():
42+
rx, ry, bx, by = getPos()
43+
44+
q = deque()
45+
q.append((rx, ry, bx, by, 1))
46+
visited.append((rx, ry, bx, by))
47+
48+
while q:
49+
rx, ry, bx, by, result = q.popleft()
50+
51+
if result > 10:
52+
break
53+
54+
for i in range(4):
55+
nrx, nry, rcnt = move(rx, ry, dx[i], dy[i])
56+
nbx, nby, bcnt = move(bx, by, dx[i], dy[i])
57+
58+
# 파란 구슬이 구멍에 들어갈 경우
59+
if board[nbx][nby] == "O":
60+
continue
61+
62+
# 빨간 구슬이 들어갈 경우 성공
63+
if board[nrx][nry] == "O":
64+
print(result)
65+
return
66+
67+
# 둘이 겹쳐있을경우 더 많이 이동한녀석을 1칸 뒤로 보낸다.
68+
if nrx == nbx and nry == nby:
69+
if rcnt > bcnt:
70+
nrx -= dx[i]
71+
nry -= dy[i]
72+
else:
73+
nbx -= dx[i]
74+
nby -= dy[i]
75+
76+
# 탐색하지 않은 방향 탐색
77+
if (nrx, nry, nbx, nby) not in visited:
78+
visited.append((nrx, nry, nbx, nby))
79+
q.append((nrx, nry, nbx, nby, result + 1))
80+
print(-1)
81+
82+
bfs()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## 🚀 12월 3주차 (12/21) 스터디 발제 주제: BFS
2+
> 발제자: 김홍주
3+
4+
### 🗂️ 스터디 자료
5+
- PDF: 바로가기
6+
- ![image](https://github.com/user-attachments/assets/64d7c010-7a67-4049-b89c-435fb767d25a)
7+
8+
9+
### 📖 문제
10+
- [백준 #2206. 벽 부수고 이동하기](https://www.acmicpc.net/problem/2206): 구현, BFS / 골드3
11+
- 정답 코드: Study_BOJ_2206_벽부수고이동하기.py
12+
13+
### 💻 과제
14+
- [백준 #13460.구슬탈출2](https://www.acmicpc.net/problem/13460): BFS / 골드2
15+
- 정답 코드: Asssignment_BOJ_13460_구슬탈출2.py
242 KB
Binary file not shown.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'''
2+
BOJ #2206.벽 부수고 이동하기 (골드3)
3+
https://www.acmicpc.net/problem/2206
4+
유형: BFS,구현
5+
'''
6+
7+
import sys
8+
from collections import deque
9+
# 1. 입력 그래프
10+
N , M = map(int,sys.stdin.readline().split())
11+
graph = []
12+
for n in range(N) :
13+
graph.append(list(map(int, input())))
14+
15+
#방문 여부 & 시작노드로 부터 최단 경로 거리 ,벽 부순 횟수(3차원)
16+
visited = [[[0]*2 for _ in range(M)] for k in range(N)]
17+
18+
#상하좌우 -> 인접 노드
19+
dx = [0,0,1,-1]
20+
dy = [1,-1,0,0]
21+
22+
# 2. bfs 최단 거리
23+
def bfs():
24+
#초기 노드 queue 에 추가 , 방문 등록
25+
dq = deque()
26+
dq.append((0,0,0))
27+
visited[0][0][0] = 1
28+
29+
while dq :
30+
x,y,wall = dq.popleft()
31+
# 목적지(N,M) 도착한 경우,이동 횟수 출력
32+
if x == N-1 and y == M -1 :
33+
return visited[x][y][wall]
34+
35+
for i in range(4):#상하좌우로 다음 이동할 칸의 위치
36+
xx = x+ dx[i] ; yy = y+ dy[i]
37+
# (1)다음 이동할 곳이 graph 밖에 있는 경우
38+
if xx<0 or xx>= N or yy <0 or yy>= M:
39+
continue
40+
# (2) 다음 이동할 곳이 벽이고, 한번도 벽 안 뚤었을때
41+
if graph[xx][yy] == 1 and wall == 0 : # and not visited[xx][yy][1]
42+
visited[xx][yy][1] = visited[x][y][0]+1
43+
dq.append((xx,yy,1))
44+
45+
#(3)다음 이동할 곳이 벽이 아니고,한번도 방문 하지 않은 곳
46+
elif graph[xx][yy] == 0 and visited[xx][yy][wall] == 0 : # 그냥 감
47+
visited[xx][yy][wall] = visited[x][y][wall] +1
48+
dq.append((xx,yy,wall))
49+
50+
return -1 #BFS 종료될떄 까지 도착점에 도달하지 못하면 -1 출력하기
51+
52+
print(bfs())
53+
54+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
# 테트로미노의 전체 경우의 수를 저장
5+
tets = [
6+
# 1x4 막대
7+
[(0, 1), (0, 2), (0, 3)], [(1, 0), (2, 0), (3, 0)],
8+
# 2x2 정사각형
9+
[(0, 1), (1, 0), (1, 1)],
10+
# ㄹ자 모양
11+
[(1, 0), (1, 1), (2, 1)], [(0, 1), (1, 0), (1, -1)],
12+
# ㄹ자 대칭
13+
[(1, 0), (1, -1), (2, -1)], [(0, 1), (1, 1), (1, 2)],
14+
# ㄴ자 모양 (회전 포함)
15+
[(1, 0), (2, 0), (2, 1)], [(0, 1), (0, 2), (1, 0)],
16+
[(0, 1), (1, 1), (2, 1)], [(0, 1), (0, 2), (-1, 2)],
17+
[(1, 0), (2, 0), (2, -1)], [(0, 1), (0, 2), (1, 2)],
18+
# ㄴ자 대칭 (회전 포함)
19+
[(1, 0), (2, 0), (0, 1)], [(1, 0), (1, 1), (1, 2)],
20+
[(1, 0), (1, 1), (1, -1)], [(1, 0), (1, 1), (2, 0)],
21+
# ㅗ자 모양 (회전 포함)
22+
[(0, -1), (0, 1), (1, 0)], [(0, 1), (-1, 1), (1, 1)],
23+
[(0, -1), (0, 1), (-1, 0)], [(1, 0), (0, 1), (-1, 0)],
24+
]
25+
26+
# 주어진 좌표에서 특정 테트로미노를 배치했을 때의 합 계산
27+
def calc(i, j, tet):
28+
total = arr[i][j] # 시작 좌표의 값
29+
for di, dj in tet:
30+
ni, nj = i + di, j + dj
31+
# 범위를 벗어나면 무효
32+
if 0 <= ni < N and 0 <= nj < M:
33+
total += arr[ni][nj]
34+
else:
35+
return 0
36+
return total
37+
38+
# 모든 위치에서 가능한 테트로미노를 배치해 최대값 계산
39+
def max_tetromino_sum():
40+
max_sum = 0
41+
for i in range(N):
42+
for j in range(M):
43+
for tet in tets:
44+
max_sum = max(max_sum, calc(i, j, tet))
45+
return max_sum
46+
47+
48+
N, M = map(int, input().split())
49+
arr = [list(map(int, input().split())) for _ in range(N)]
50+
51+
print(max_tetromino_sum())
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
n = int(input())
2+
3+
num = [[0]*10 for _ in range(n+1)]
4+
num[0] = [1,1,1,1,1,1,1,1,0] #0행에 들어가는 값들을 계단수의 경우의수로 초기화
5+
6+
for i in range(1,n+1):
7+
for j in range(10): #0일때, 9일때, 나머지인 경우를 점화식을 토대로 코드 작성
8+
if j == 0:
9+
num[i][j] = num[i-1][1]
10+
elif j == 9:
11+
num[i][j] = num[i-1][8]
12+
else:
13+
num[i][j] = num[i-1][j-1] + num[i-1][j]
14+
15+
answer = sum(num[n]) % 100000000
16+
print(answer)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# 하노이 함수
2+
def hanoi_f(one, three, n):
3+
if n == 1:
4+
print(one, three)
5+
return
6+
7+
hanoi_f(one, 6 - one - three, n - 1) # 1단계 (1->2)
8+
print(one, three) # 2단계 (마지막원반 1->3)
9+
hanoi_f(6 - one - three, three, n - 1) # 3단계 (2->3)
10+
11+
12+
# 메인
13+
n = int(input())
14+
print(2 ** n - 1)
15+
if n <= 20:
16+
hanoi_f(1, 3, n)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
# 누적합 테이블 계산
5+
def get_prefix_sum(grid, N):
6+
prefix_sum = [[0] * (N+1) for _ in range(N+1)]
7+
for i in range(1, N+1):
8+
for j in range(1, N+1):
9+
prefix_sum[i][j] = (
10+
grid[i-1][j-1]
11+
+ prefix_sum[i-1][j]
12+
+ prefix_sum[i][j-1]
13+
- prefix_sum[i-1][j-1]
14+
)
15+
return prefix_sum
16+
17+
# 입력
18+
N, M = map(int, input().split())
19+
grid = [list(map(int, input().split())) for _ in range(N)]
20+
21+
# 누적합
22+
prefix_sum = get_prefix_sum(grid, N)
23+
24+
# 범위 합 계산
25+
for _ in range(M):
26+
x1, y1, x2, y2 = map(int, input().split())
27+
answer = (
28+
prefix_sum[x2][y2]
29+
- prefix_sum[x1-1][y2]
30+
- prefix_sum[x2][y1-1]
31+
+ prefix_sum[x1-1][y1-1]
32+
)
33+
print(answer)

0 commit comments

Comments
 (0)