728x90
7569. 토마토
from collections import deque
import sys
input = sys.stdin.readline
dz = [0, 0, 0, 0, -1, 1]
dy = [-1, 1, 0, 0, 0, 0]
dx = [0, 0, -1, 1, 0, 0]
def bfs():
global ripe
day = 0
while queue:
qpop = queue.popleft()
a, b, c, day = qpop[0], qpop[1], qpop[2], qpop[3]
visited[a][b][c] = True
for d in range(6):
if a + dz[d] in range(H) and b + dy[d] in range(N) and c + dx[d] in range(M):
if tomato[a + dz[d]][b + dy[d]][c + dx[d]] == 0 and not visited[a + dz[d]][b + dy[d]][c + dx[d]]:
visited[a + dz[d]][b + dy[d]][c + dx[d]] = True
tomato[a + dz[d]][b + dy[d]][c + dx[d]] = 1
ripe += 1
queue.append((a + dz[d], b + dy[d], c + dx[d], day + 1))
if ripe != cnt_0:
print(-1)
else:
print(day)
M, N, H = map(int, input().split())
tomato = [[list(map(int, input().split())) for _ in range(N)] for __ in range(H)]
visited = [[[False] * M for _ in range(N)] for __ in range(H)]
queue = deque()
cnt_0 = 0
for z in range(H):
for y in range(N):
for x in range(M):
if tomato[z][y][x] == 0:
cnt_0 += 1
if tomato[z][y][x] == 1:
queue.append([z, y, x, 0])
ripe = 0
bfs()
단순 bfs문제지만 3차원 배열인 점이 다름. 막혀있는 부분이 있음을 잘 고려해야 한다. 큐에 인덱스까지 포함되어 있는 경우에 변수를 하나 할당해서 써야 보기도 깔끔하고 시간복잡도 측면에서도 도움이 되지 않을까...싶다. 앞으로 신경쓰기
14503. 로봇 청소기
import sys
input = sys.stdin.readline
dy = [-1, 0, 1, 0]
dx = [0, 1, 0, -1]
N, M = map(int, input().split())
R, C, D = map(int, input().split())
room = [list(map(int, input().split())) for _ in range(N)]
cnt = 0
now = [R, C, D]
while True:
r, c, d = now[0], now[1], now[2]
if not room[r][c]:
room[r][c] = 2
cnt += 1
find = False
for i in range(4):
if r + dy[i] in range(1, N - 1) and c + dx[i] in range(1, M - 1):
if not room[r + dy[i]][c + dx[i]]:
find = True
if find:
now = [r, c, (d + 3) % 4]
if r + dy[(d + 3) % 4] in range(1, N - 1) and c + dx[(d + 3) % 4] in range(1, M - 1):
if not room[r + dy[(d + 3) % 4]][c + dx[(d + 3) % 4]]:
now = [r + dy[(d + 3) % 4], c + dx[(d + 3) % 4], (d + 3) % 4]
else:
if room[r - dy[d]][c - dx[d]] == 1:
break
now = [r - dy[d], c - dx[d], d]
print(cnt)
문제에 로직이 다 나와있어서 그대로 구현만 하면 되는 문젠데, 문제를 착각해서 방문처리랑 벽을 구분 못했다가 여러번 틀렸다. 문제는 반드시 꼼꼼하게 읽자
14891. 톱니바퀴
from collections import deque
import sys
input = sys.stdin.readline
def rotate(n):
if direction == 1:
gears[n].appendleft(gears[n].pop())
else:
gears[n].append(gears[n].popleft())
gears = [0] + [deque(list(map(int, input().strip()))) for _ in range(4)]
K = int(input())
for i in range(K):
gear_num, direction = map(int, input().split())
if gear_num == 1:
if gears[1][2] != gears[2][6]:
if gears[2][2] != gears[3][6]:
if gears[3][2] != gears[4][6]:
direction = -direction
rotate(4)
direction = -direction
rotate(3)
direction = -direction
rotate(2)
direction = -direction
rotate(1)
if gear_num == 2:
if gears[1][2] != gears[2][6]:
direction = -direction
rotate(1)
direction = -direction
if gears[2][2] != gears[3][6]:
if gears[3][2] != gears[4][6]:
rotate(4)
direction = -direction
rotate(3)
direction = -direction
rotate(2)
if gear_num == 3:
if gears[3][2] != gears[4][6]:
direction = -direction
rotate(4)
direction = -direction
if gears[2][2] != gears[3][6]:
if gears[1][2] != gears[2][6]:
rotate(1)
direction = -direction
rotate(2)
direction = -direction
rotate(3)
if gear_num == 4:
if gears[3][2] != gears[4][6]:
if gears[2][2] != gears[3][6]:
if gears[1][2] != gears[2][6]:
direction = -direction
rotate(1)
direction = -direction
rotate(2)
direction = -direction
rotate(3)
direction = -direction
rotate(4)
ans = 0
for j in range(1, 5):
ans += 2**(j-1) * gears[j][0]
print(ans)
빡구현인데 좀 짧게 해보려다가 조건 다는거나 하나씩 구현하는거나 별반 차이 없을것 같아서 결국 노가다로 구현했다. 마찬가지로 시계, 반시계 방향 헷갈려서 또 몇 번 헤맸다... 왤케 조심성이 없을까 나는
1004. 어린 왕자
import sys
input = sys.stdin.readline
def isin(cx, cy, r, x, y):
if (cx - x) ** 2 + (cy - y) ** 2 < r ** 2:
return True
return False
T = int(input())
for tc in range(T):
x1, y1, x2, y2 = map(int, input().split())
n = int(input())
cnt = 0
for planet in range(n):
cx, cy, r = map(int, input().split())
if isin(cx, cy, r, x1, y1) != isin(cx, cy, r, x2, y2):
cnt += 1
print(cnt)
처음으로 풀어본 기하 문젠데 재밌다! 물론 난이도가 낮은 문제라 그렇겠지만... 가끔 환기 용도로 이런 문제 풀어보는 것도 괜찮을 것 같다. 약간 구현 느낌이 나기도 하고
728x90
'알고리즘 > 백준' 카테고리의 다른 글
[백준]230303-05 문제풀이 (0) | 2023.03.06 |
---|---|
[백준]230302 문제풀이 (2) | 2023.03.02 |
[백준]230226 문제풀이 (0) | 2023.02.26 |
[백준]230225 문제풀이 (0) | 2023.02.25 |
[백준]230223-24 문제풀이 (0) | 2023.02.24 |
댓글