Skip to content

구현: 예제 및 실전문제 해결 #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
34 changes: 34 additions & 0 deletions DFS BFS/5-10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
N,M = map(int,input().split())

lst = []
for i in range(N):
lst.append(list(map(int,input())))

cnt = 0

#상하좌우
dx = [-1,1,0,0]
dy = [0,0,-1,1]

def dfs(x,y):
#들른 곳 좌표
lst[x][y] = 1

#상하좌우 이동
for i in range(4):
lx = x + dx[i]
ly = y + dy[i]

#내부에 있을 때
if(lx >= 0 and lx < N and ly >= 0 and ly < M):
if (lst[lx][ly] == 0):
dfs(lx,ly)


for i in range(N):
for j in range(M):
if (lst[i][j] == 0):
dfs(i,j)
cnt += 1

print(cnt)
36 changes: 36 additions & 0 deletions DFS BFS/5-11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from collections import deque

N,M = map(int,input().split())

lst = []
for i in range(N):
lst.append(list(map(int,input())))

#상하좌우
dx = [-1,1,0,0]
dy = [0,0,-1,1]

def bfs(x,y):
queue = deque()
queue.append((x,y))

#큐가 빌 때까지
while queue:
x,y = queue.popleft()

for i in range(4):
lx = x + dx[i]
ly = y + dy[i]

#내부 제한
if(lx < 0 or ly < 0 or lx >= N or ly >= M):
continue
if(lst[lx][ly] == 0):
continue
#첫 방문시 기록
if(lst[lx][ly] == 1):
lst[lx][ly] = lst[x][y] + 1
queue.append((lx,ly))
return lst[N-1][M-1]

print(bfs(0,0))
29 changes: 29 additions & 0 deletions Implementation/4-1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
n = int(input())

###입력키 리스트 받기
key = list(map(str,input().split()))

x = 1
y = 1

for i in key:
###사각형 크기보다 작을 때 수행
if(x < n and y < n):
if(i == "R"):
x += 1
elif(i == "L"):
x -= 1
elif(i == "U"):
y -= 1
elif(i == "D"):
y += 1
###사각형 크기보다 크면 넘어가기
else:
continue
###좌표가 0일 때 처리
if (x == 0):
x += 1
elif (y == 0):
y += 1

print(y, x)
17 changes: 17 additions & 0 deletions Implementation/4-2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
h = int(input())

cnt = 0

###h시간, 60분, 60초 동안 반복
for i in range(h+1):
for j in range(60):
for k in range(60):
###개수를 세기 위한 연산
if((i//10 == 3 or i%10 == 3)):
cnt += 1
elif((j//10 == 3 or j%10 == 3)):
cnt += 1
elif((k//10 == 3 or k%10 == 3)):
cnt += 1

print(cnt)
22 changes: 22 additions & 0 deletions Implementation/4-3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
###체스판 좌표
row = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
col = [1, 2, 3, 4 ,5, 6, 7, 8]

data = list(input())

###위치를 좌표화
loc = [row.index(data[0]), col.index(int(data[1]))]

###나이트가 이동하는 경우의 수
mov = [[-2,-1], [-2,1], [2,1], [2,-1], [1,-2], [1,2], [-1,2], [-1,-2]]

###더한 후 0보다 크면 카운트
cnt = 0
for x,y in mov:
x += loc[0]
y += loc[1]

if (x > 0 and y > 0):
cnt += 1

print(cnt)
55 changes: 55 additions & 0 deletions Implementation/4-4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
N, M = map(int,input().split())
x, y, d = map(int,input().split())

###맵 입력
maps = []
for i in range(N):
maps.append(list(map(int,input().split())))

###방향에 따른 이동
dx = [-1,0,1,0]
dy = [0,1,0,-1]

###처음 위치 표시
maps[x][y] = 1
cnt = 1
dir = 0

while (1):
###서쪽 바라보기 // 반복
d -= 1
if (d < 0):
d = 3

###이동 후 위치
lx = x + dx[d]
ly = y + dy[d]

###가보지 않은 곳
if (maps[lx][ly] == 0):
maps[lx][ly] = 1

x = lx
y = ly

cnt += 1
dir = 0

continue
else:
dir += 1

###4방향 모두 가본 칸 or 바다
if(dir == 4):
lx = x - dx[d]
ly = y - dy[d]

if(maps[lx][ly] == 0):
x = lx
y = ly

else:
break


print(cnt)
10 changes: 10 additions & 0 deletions Sort/6-10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
N = int(input())

lst = []
for i in range(N):
lst.append(int(input()))

lst.sort(reverse = True)

lst = list(map(str,lst))
print(' '.join(lst))
16 changes: 16 additions & 0 deletions Sort/6-11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
N = int(input())


#리스트 입력
lst = []

for i in range(N):
data = input().split()
lst.append([data[0],int(data[1])])
print(lst)

lst.sort(key = lambda x : x[1])

for i in lst:
print(i[0],end=' ')

17 changes: 17 additions & 0 deletions Sort/6-12.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
N, K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))

#리스트 정렬
A.sort()
B.sort(reverse = True)

#비교 후 바꾸기
for i in range(K):
if(A[i] < B[i]):
A[i] = B[i]

else:
break

print(sum(A))