Skip to content

Commit 6de432d

Browse files
authored
Merge pull request #214 from AlgorithmStudy-Allumbus/minjeong3
Minjeong / 5월 4주차 / 3문제
2 parents 773e4cd + c069298 commit 6de432d

File tree

3 files changed

+165
-0
lines changed

3 files changed

+165
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import sys
2+
from collections import deque
3+
4+
input = sys.stdin.readline
5+
6+
# 1. 상과 왕 초기 위치
7+
s_r, s_c = map(int, input().split()) # 상의 위치
8+
k_r, k_c = map(int, input().split()) # 왕의 위치
9+
10+
# 장기판 (10x9)
11+
grid = [[0 for _ in range(9)] for _ in range(10)]
12+
visited = [[False for _ in range(9)] for _ in range(10)]
13+
14+
# 8가지 이동 경로
15+
directions = [
16+
# 상
17+
((-1, 0), (-1, 1), (-1, 1)), # 오른쪽 위
18+
((-1, 0), (-1, -1), (-1, -1)), # 왼쪽 위
19+
# 하
20+
((1, 0), (1, 1), (1, 1)), # 오른쪽 아래
21+
((1, 0), (1, -1), (1, -1)), # 왼쪽 아래
22+
# 좌
23+
((0, -1), (-1, -1), (-1, -1)), # 왼쪽 위
24+
((0, -1), (1, -1), (1, -1)), # 왼쪽 아래
25+
# 우
26+
((0, 1), (-1, 1), (-1, 1)), # 오른쪽 위
27+
((0, 1), (1, 1), (1, 1)) # 오른쪽 아래
28+
]
29+
30+
31+
def bfs():
32+
queue = deque([(s_r, s_c)]) # 상 위치부터 시작
33+
34+
while queue:
35+
r, c = queue.popleft()
36+
37+
# 왕에게 도달한 경우
38+
if (r, c) == (k_r, k_c):
39+
return grid[r][c]
40+
41+
for d1, d2, d3 in directions:
42+
nr1, nc1 = r + d1[0], c + d1[1]
43+
nr2, nc2 = nr1 + d2[0], nc1 + d2[1]
44+
nr3, nc3 = nr2 + d3[0], nc2 + d3[1]
45+
46+
if (0 <= nr3 < 10 and 0 <= nc3 < 9) and not visited[nr3][nc3]:
47+
if (nr1, nc1) == (k_r, k_c) or (nr2, nc2) == (k_r, k_c):
48+
# 경로에 왕이 있다면 못 감
49+
continue
50+
51+
visited[nr3][nc3] = True
52+
grid[nr3][nc3] += grid[r][c] + 1
53+
queue.append((nr3, nc3))
54+
55+
return -1 # 도달 불가
56+
57+
58+
print(bfs())
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import sys
2+
from collections import deque
3+
input = sys.stdin.readline
4+
5+
FIELD_X = 12
6+
FIELD_Y = 6
7+
8+
# 1. 입력
9+
field = [list(input().strip()) for _ in range(FIELD_X)]
10+
11+
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
12+
combo = 0
13+
14+
# 상하좌우로 동일한 블록을 탐색해 해당 좌표들을 가진 리스트 반환
15+
def bfs(x, y):
16+
queue = deque([(x, y)])
17+
color = field[x][y]
18+
visited[x][y] = True
19+
same_blocks = [(x, y)] # 같은 블록의 좌표 리스트
20+
21+
while queue:
22+
x, y = queue.popleft()
23+
for dx, dy in directions:
24+
nx, ny = x + dx, y + dy
25+
if (0 <= nx < FIELD_X and 0 <= ny < FIELD_Y) and \
26+
field[nx][ny] == color and not visited[nx][ny]:
27+
queue.append((nx, ny))
28+
visited[nx][ny] = True
29+
same_blocks.append((nx, ny))
30+
31+
return same_blocks
32+
33+
34+
# 동일한 블록 제거
35+
def delete(same_blocks):
36+
for x, y in same_blocks:
37+
field[x][y] = '.'
38+
39+
40+
# 반복문 돌면서 위에서 아래로 블록 내리기
41+
def down():
42+
for y in range(FIELD_Y):
43+
for x in range(10, -1, -1):
44+
for k in range(FIELD_X - 1, x, -1):
45+
if field[x][y] != '.' and field[k][y] == '.':
46+
field[k][y] = field[x][y]
47+
field[x][y] = '.'
48+
49+
50+
while True:
51+
pang = False
52+
visited = [[False for _ in range(FIELD_Y)] for _ in range(FIELD_X)]
53+
54+
for i in range(FIELD_X):
55+
for j in range(FIELD_Y):
56+
if field[i][j] != '.' and not visited[i][j]:
57+
same_blocks = bfs(i, j)
58+
59+
# 동일한 블록이 4개 이상일 경우 터트리기
60+
if len(same_blocks) >= 4:
61+
pang = True
62+
delete(same_blocks)
63+
64+
65+
# 터뜨린 블록이 있으면 밑으로 내리기
66+
if pang:
67+
down()
68+
combo += 1
69+
else:
70+
# 더이상 터뜨릴 게 없다면 종료
71+
break
72+
73+
print(combo)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.util.Arrays;
2+
import java.util.PriorityQueue;
3+
4+
class Solution {
5+
public int solution(int[] scoville, int K) {
6+
int answer = 0;
7+
Arrays.sort(scoville);
8+
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
9+
10+
// scoville 배열을 minHeap으로 옮기기
11+
for (int s: scoville) {
12+
minHeap.offer(s);
13+
}
14+
15+
// minHeap의 크기가 1보다 작아질 때까지 반복
16+
while (minHeap.size() > 1) {
17+
// 이미 K보다 크다면 바로 return
18+
if (minHeap.peek() >= K) {
19+
return answer;
20+
}
21+
int a = minHeap.poll();
22+
int b = minHeap.poll();
23+
minHeap.offer(a + b * 2);
24+
answer++;
25+
}
26+
27+
// 마지막 하나도 확인해야 한다.
28+
if (minHeap.peek() >= K) {
29+
return answer;
30+
}
31+
32+
return -1;
33+
}
34+
}

0 commit comments

Comments
 (0)