diff --git a/hayeong/week3/14503.py b/hayeong/week3/14503.py new file mode 100644 index 0000000..d9ff4a7 --- /dev/null +++ b/hayeong/week3/14503.py @@ -0,0 +1,42 @@ +# 14503 +# 로봇청소기 + +import sys + +# 회전벡터 -> 반시계로 회전 +dCircle = [3, 0, 1, 2] + +# 방향벡터 +dr = [-1, 0, 1, 0] +dc = [0, 1, 0, -1] + +input = sys.stdin.readline +N, M = map(int, input().split(" ")) +r, c, d = map(int, input().split(" ")) +room = [] + +for _ in range(N): + room.append(list(map(int, input().split(" ")))) + +answer = 0 + +while True: + if room[r][c] == 0: # 1. 현재 칸이 아직 청소가 안되어 있는 경우 + # 현재 칸 청소 + room[r][c] = 2 + answer += 1 + elif room[r-1][c] and room[r+1][c] and room[r][c+1] and room[r][c-1]: # 2. 주변 4칸 중 청소되지 않은 빈 칸이 없는 경우 + # 바라보는 방향을 유지한채로 한칸 후진 + r -= dr[d] + c -= dc[d] + + if room[r][c] == 1: # 벽일 경우 + break + else: # 3. 주변 4 칸 중 청소되지 않은 칸이 있을 경우 + d = dCircle[d] # 반시계 방향으로 회전 + while room[r+dr[d]][c+dc[d]]: # 바라보는 방향을 기준으로 앞쪽 칸이 청소되어 있으면 + d = dCircle[d] # 청소 안된 빈 칸이 나올 때까지 반시계로 회전 + r += dr[d] # 앞이 청소 안된 빈칸인 경우 한 칸 전진 + c += dc[d] + +print(answer) diff --git a/hayeong/week3/2636.py b/hayeong/week3/2636.py new file mode 100644 index 0000000..e6c8216 --- /dev/null +++ b/hayeong/week3/2636.py @@ -0,0 +1,55 @@ +# 2636 +# 치즈 + +import sys +from collections import deque + +input = sys.stdin.readline + +def bfs(start_x, start_y, cheeses): + dx = [-1, 1, 0, 0] # 방향 벡터 + dy = [0, 0, -1, 1] + + queue = deque([(start_x, start_y)]) + visited = set([(start_x, start_y)]) + melt = deque([]) + + while queue: + x, y = queue.popleft() + + for i in range(4): + nx = x + dx[i] + ny = y + dy[i] + + if 0 <= nx < len(cheeses) and 0 <= ny < len(cheeses[0]): + if (nx, ny) not in visited: + visited.add((nx, ny)) + if cheeses[nx][ny] == 0: # 공기라면 계속 탐색해야하므로 queue에 추가 + queue.append((nx, ny)) + elif cheeses[nx][ny] == 1: # 치즈라면 녹여야하니까 melt에 추가 + melt.append((nx, ny)) + + + for x, y in melt: # 한번에 녹여야 함 -> 먼저 녹이면 공기로 인식할 수 있기 때문 + cheeses[x][y] = 0 + return len(melt) + +N, M = map(int, input().split(" ")) + +cheeses = [] +cnt = 0 + +for i in range(N): + cheeses.append(list(map(int, input().split()))) + cnt += sum(cheeses[i]) # 치즈 개수만 세기 + +time = 1 + +while True: + meltCnt = bfs(0, 0, cheeses) + cnt -= meltCnt + if cnt == 0: + print(time) + print(meltCnt) + break + time += 1 \ No newline at end of file diff --git a/hayeong/week3/7409.py b/hayeong/week3/7409.py new file mode 100644 index 0000000..89dfef3 --- /dev/null +++ b/hayeong/week3/7409.py @@ -0,0 +1,29 @@ +# 7490 +# 0 만들기 +import sys + +input = sys.stdin.readline + +k = int(input()) + +def dfs(index, temp): + # 반환조건 : index가 끝까지 갔을 때 + if index == N: + if eval(temp.replace(' ', '')) == 0: + answer.append(temp) + return + else: + index += 1 + dfs(index, temp + ' ' + str(index)) + dfs(index, temp + '+' + str(index)) + dfs(index, temp + '-' + str(index)) + + +for _ in range(k): + N = int(input()) + answer = [] + index = 1 + dfs(index, '1') + for x in sorted(answer): + print(x) + print() \ No newline at end of file