Skip to content

Commit 449b815

Browse files
authored
Merge pull request #101 from zaqquum/main
Hongjoo/12월 1주차 / 3문제
2 parents 0327057 + 0d02a2d commit 449b815

File tree

4 files changed

+186
-0
lines changed

4 files changed

+186
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def solution(s):
2+
answer = ""
3+
s = s.split(' ') # 1.단어별로 splite 된 list
4+
for i in range(len(s)) : #2.각 단어별로 맨 앞글자 대문자,그외 소문자로 변환
5+
#capitalize : 첫문자는 대문자,그외는 소문자로 만듦
6+
s[i] = s[i].capitalize()
7+
answer = " ".join(s)#3. 분리해둔 단어들을 한개의 str로 합치기
8+
9+
return answer

Hongjoo/백준/뱀.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
"""
2+
condition
3+
1. NxN map , some apple , N+1 walls ,
4+
2. snack 's time : size = 0 : 1 , right way
5+
6+
Rule
7+
1.먼저 뱀은 몸길이를 늘려 머리를 다음칸에 위치시킨다.
8+
2.만약 벽,자기자신의 몸과 부딪히면 게임이 끝난다. =game over 조건
9+
<사과 - 몸길이 변화 >
10+
3.만약 이동한 칸에 사과 O => 그 칸에 있던 사과가 없어지고 꼬리 stay , 머리 길어짐 => 즉 몸길이 변함
11+
4.만약 이동한 칸에 사과 X => 몸길이를 줄여서 꼬리가 위치한 칸을 비워준다(즉, 담칸으로 이동만 함). 즉, 몸길이는 변하지 않는다.
12+
-> 머리 부터 ,그리고 꼬리 이동(사과 유무)
13+
#Variable
14+
1. time , 2. 뱀이 차지하는 영역 : stack , 3.
15+
16+
Flow
17+
1. 입력을 통해 map, 이동 반경
18+
19+
20+
"""
21+
import sys
22+
23+
''' 1. 입력 값 변수에 할당(field구축 , 뱀의 방향 정보)
24+
- field : N+2
25+
빈 곳 = 0
26+
벽, 뱀의 위치 = -1
27+
사과 = 1
28+
'''
29+
30+
N = int(sys.stdin.readline()) # 보드 크기 => N+2*(q)
31+
K = int(sys.stdin.readline()) # 사과 개수
32+
33+
field = [[0 for _ in range(N+2)] for k in range(N+2)]
34+
# 뱀의 첫 위치 & 벽
35+
field[1][1] = -1
36+
for i in range(len(field)):
37+
for j in range(len(field[0])) :
38+
if i in [0, N+1] or j in [0,N+1] :
39+
field[i][j] = -1
40+
41+
# 사과 위치
42+
for k in range(K) :
43+
i , j = map(int, sys.stdin.readline().split())
44+
field[i][j] = 1
45+
46+
# 뱀의 방향 정보 -> rotate_time=[time, direction] # 방향을 이동하기 전에 확인하여 t+1 로 저장
47+
48+
L = int(sys.stdin.readline())
49+
rotate_time = []
50+
for l in range(L):
51+
t , d = sys.stdin.readline().split()
52+
rotate_time.append([int(t)+1,d])
53+
54+
55+
"""
56+
2. 게임 start
57+
- 뱀의 현재 차지하고 있는 위치 : snack = [[머리 좌표] , [몸통 좌표들],...,[꼬리 좌표]]
58+
- & 차지하는 field = -1
59+
(1) 뱀이 도착한 위치의 field 번호 = -1(본인,벽) 이 아닐때 까지 이동
60+
"""
61+
def change_int_direction(time, current_direction) : # 방향 변환
62+
direction_map = ['r','d','l','t'] * 2
63+
# direction_dict = {'r,':[0,1] ,'d' :[-1,0] ,'l':[0,-1] , 't':[1,0] }
64+
for ch_t, ch_d in rotate_time :
65+
if time == ch_t : # {'r,':[0,1] ,'d' :[-1,0] ,'l':[0,-1] , 't':[1,0] }
66+
# print(f"change direction ")
67+
idx = direction_map.index(current_direction)
68+
if ch_d == "D" : #right
69+
current_direction = direction_map[idx + 1]
70+
elif ch_d == "L" :#left
71+
current_direction = direction_map[idx - 1]
72+
break
73+
74+
# direction = direction_dict[current_direction] # [0,1]
75+
return current_direction # [0,1]
76+
from collections import deque
77+
time = 0
78+
snack = deque() # idx =0 머리 , -1 꼬리
79+
snack_direction = 'r' # r,d,l, u
80+
snack.append([1,1])
81+
direction_dict = {'r':[0,1] ,'d' :[1,0] ,'l':[0,-1] , 't':[-1,0] }
82+
while(1) :
83+
time += 1
84+
# print(f"##time : {time}")
85+
# 사과 확인
86+
head_x , head_y = snack[0]
87+
#1. head 이동 - snack 영역 추가
88+
snack_direction = change_int_direction(time, snack_direction)
89+
move_x , move_y = direction_dict[snack_direction]
90+
head_x += move_x ; head_y += move_y
91+
# print(f"- snack_direction : {snack_direction} # {move_x},{move_y}")
92+
# print(f"new head : {head_x} , {head_y}")
93+
#2.현재 head 위치하는 곳의 조건 확인
94+
if field[head_x][head_y] == -1 : #game over
95+
# print(f"!!!gameover : ")
96+
# print(field)
97+
# print(f"{head_x} , {head_y} = {field[head_x][head_y]}")
98+
break
99+
elif field[head_x][head_y] == 0 : # safe(빈곳)
100+
tail_x , tail_y = snack.pop() #W 꼬리 이동
101+
field[tail_x][tail_y] = 0
102+
# field[head_x][head_y] == 0,1 (사과, 빈곳 )공통점 - 머리 이동 ,
103+
field[head_x][head_y] = -1 #field 값 변경(뱀이 차지함) - 사과 먹음
104+
snack.appendleft([head_x,head_y]) # head 추가
105+
# print(snack)
106+
print(time)

Hongjoo/백준/트럭.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 실패
2+
import sys
3+
n, w, l = map(int, sys.stdin.readline().split())
4+
truck = list(map(int, sys.stdin.readline().split()))
5+
time, bridge = 0, [0]*w #1. 시간변수와 다리의 길이를 입력받은 횟수만큼 리스트에 저장
6+
while bridge: #2. 다리의 길이가 0이 아닐때까지 반복
7+
time += 1 #3 time +1
8+
bridge.pop(0) #4.다리의 첫번째 원소 제거
9+
if truck: #5.bridge리스트를 추가하는건 truck리스트가 남아있을때까지
10+
if sum(bridge) + truck[0] <= l: #6기존 다리의 원소의 합 + 가장 왼쪽의 트럭'이 다리의 최대하중과 같거나 작다면.
11+
bridge.append(truck.pop(0))
12+
else: #7. 다리에 트럭이 있지만 다리에 올리지 못할 경우 다리 공간 유지하기 위해 0을 삽입
13+
bridge.append(0)
14+
print(time)

Hongjoo/백준/평범한배낭.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
N :물건 개수 , K : 제한 무게
3+
W , V : 개별 물건 무게 , 가지
4+
goal: max V
5+
6+
6 4 3 5
7+
13 8 6 12
8+
9+
-> 13/6 ,8/4 , 6/3 , 5/2
10+
goal : 낮은 무게부터 넣기
11+
[[3, 6], [4, 8], [5, 12], [6, 13]]
12+
1. sum(W) <= K인 조합 찾기
13+
2. w확인?
14+
"""
15+
#1. 변수 입력 받기
16+
import sys
17+
objects = list()
18+
N , K = map(int,sys.stdin.readline().split() )
19+
for i in range(N) :
20+
W , V = map(int,sys.stdin.readline().split() )
21+
objects.append([W,V])
22+
23+
# 가벼운 순으로 나열
24+
objects = sorted(objects)
25+
# print(objects)
26+
bags = list()
27+
#2. two point
28+
# start + window
29+
# for start in range(len(objects)) :
30+
# for end in range(1,len(objects)) : # end point
31+
start = 0 ; end= 1
32+
max_values = 0
33+
# 물건 1개의 무게 < target weight
34+
35+
while start < N and start < end and objects[end-1][0] <= K :
36+
37+
Subset = objects[start : end] # length = start -end
38+
currnet_weight = 0 ; current_values = 0
39+
# print(Subset)
40+
41+
for w,v in Subset :
42+
currnet_weight += w
43+
current_values += v
44+
45+
if currnet_weight <= K :# K 보다 현재 배낭 무게가 가벼우면 업데이트 진행
46+
max_values = max(max_values , current_values)
47+
end += 1
48+
elif currnet_weight <= K or end > N:# current_weight > k 이거나 end == N 이면 초기화
49+
current_values = 0
50+
start += 1
51+
end = start +1
52+
# print("end",end )
53+
# if end > N:
54+
# start += 1
55+
# end = start +1
56+
# # print(f"update { max_values}")
57+
print(max_values)

0 commit comments

Comments
 (0)