-
Notifications
You must be signed in to change notification settings - Fork 5
Minjeong / 5월 4주차 / 3문제 #214
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import sys | ||
| from collections import deque | ||
|
|
||
| input = sys.stdin.readline | ||
|
|
||
| # 1. 상과 왕 초기 위치 | ||
| s_r, s_c = map(int, input().split()) # 상의 위치 | ||
| k_r, k_c = map(int, input().split()) # 왕의 위치 | ||
|
|
||
| # 장기판 (10x9) | ||
| grid = [[0 for _ in range(9)] for _ in range(10)] | ||
| visited = [[False for _ in range(9)] for _ in range(10)] | ||
|
|
||
| # 8가지 이동 경로 | ||
| directions = [ | ||
| # 상 | ||
| ((-1, 0), (-1, 1), (-1, 1)), # 오른쪽 위 | ||
| ((-1, 0), (-1, -1), (-1, -1)), # 왼쪽 위 | ||
| # 하 | ||
| ((1, 0), (1, 1), (1, 1)), # 오른쪽 아래 | ||
| ((1, 0), (1, -1), (1, -1)), # 왼쪽 아래 | ||
| # 좌 | ||
| ((0, -1), (-1, -1), (-1, -1)), # 왼쪽 위 | ||
| ((0, -1), (1, -1), (1, -1)), # 왼쪽 아래 | ||
| # 우 | ||
| ((0, 1), (-1, 1), (-1, 1)), # 오른쪽 위 | ||
| ((0, 1), (1, 1), (1, 1)) # 오른쪽 아래 | ||
| ] | ||
|
|
||
|
|
||
| def bfs(): | ||
| queue = deque([(s_r, s_c)]) # 상 위치부터 시작 | ||
|
|
||
| while queue: | ||
| r, c = queue.popleft() | ||
|
|
||
| # 왕에게 도달한 경우 | ||
| if (r, c) == (k_r, k_c): | ||
| return grid[r][c] | ||
|
|
||
| for d1, d2, d3 in directions: | ||
| nr1, nc1 = r + d1[0], c + d1[1] | ||
| nr2, nc2 = nr1 + d2[0], nc1 + d2[1] | ||
| nr3, nc3 = nr2 + d3[0], nc2 + d3[1] | ||
|
|
||
| if (0 <= nr3 < 10 and 0 <= nc3 < 9) and not visited[nr3][nc3]: | ||
| if (nr1, nc1) == (k_r, k_c) or (nr2, nc2) == (k_r, k_c): | ||
| # 경로에 왕이 있다면 못 감 | ||
| continue | ||
|
|
||
| visited[nr3][nc3] = True | ||
| grid[nr3][nc3] += grid[r][c] + 1 | ||
| queue.append((nr3, nc3)) | ||
|
|
||
| return -1 # 도달 불가 | ||
|
|
||
|
|
||
| print(bfs()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import sys | ||
| from collections import deque | ||
| input = sys.stdin.readline | ||
|
|
||
| FIELD_X = 12 | ||
| FIELD_Y = 6 | ||
|
|
||
| # 1. 입력 | ||
| field = [list(input().strip()) for _ in range(FIELD_X)] | ||
|
|
||
| directions = [(0, 1), (1, 0), (0, -1), (-1, 0)] | ||
| combo = 0 | ||
|
|
||
| # 상하좌우로 동일한 블록을 탐색해 해당 좌표들을 가진 리스트 반환 | ||
| def bfs(x, y): | ||
| queue = deque([(x, y)]) | ||
| color = field[x][y] | ||
| visited[x][y] = True | ||
| same_blocks = [(x, y)] # 같은 블록의 좌표 리스트 | ||
|
|
||
| while queue: | ||
| x, y = queue.popleft() | ||
| for dx, dy in directions: | ||
| nx, ny = x + dx, y + dy | ||
| if (0 <= nx < FIELD_X and 0 <= ny < FIELD_Y) and \ | ||
| field[nx][ny] == color and not visited[nx][ny]: | ||
| queue.append((nx, ny)) | ||
| visited[nx][ny] = True | ||
| same_blocks.append((nx, ny)) | ||
|
|
||
| return same_blocks | ||
|
|
||
|
|
||
| # 동일한 블록 제거 | ||
| def delete(same_blocks): | ||
| for x, y in same_blocks: | ||
| field[x][y] = '.' | ||
|
|
||
|
|
||
| # 반복문 돌면서 위에서 아래로 블록 내리기 | ||
| def down(): | ||
| for y in range(FIELD_Y): | ||
| for x in range(10, -1, -1): | ||
| for k in range(FIELD_X - 1, x, -1): | ||
| if field[x][y] != '.' and field[k][y] == '.': | ||
| field[k][y] = field[x][y] | ||
| field[x][y] = '.' | ||
|
|
||
|
|
||
| while True: | ||
| pang = False | ||
| visited = [[False for _ in range(FIELD_Y)] for _ in range(FIELD_X)] | ||
|
|
||
| for i in range(FIELD_X): | ||
| for j in range(FIELD_Y): | ||
| if field[i][j] != '.' and not visited[i][j]: | ||
| same_blocks = bfs(i, j) | ||
|
|
||
| # 동일한 블록이 4개 이상일 경우 터트리기 | ||
| if len(same_blocks) >= 4: | ||
| pang = True | ||
| delete(same_blocks) | ||
|
|
||
|
|
||
| # 터뜨린 블록이 있으면 밑으로 내리기 | ||
| if pang: | ||
| down() | ||
| combo += 1 | ||
| else: | ||
| # 더이상 터뜨릴 게 없다면 종료 | ||
| break | ||
|
|
||
| print(combo) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import java.util.Arrays; | ||
| import java.util.PriorityQueue; | ||
|
|
||
| class Solution { | ||
| public int solution(int[] scoville, int K) { | ||
| int answer = 0; | ||
| Arrays.sort(scoville); | ||
| PriorityQueue<Integer> minHeap = new PriorityQueue<>(); | ||
|
|
||
| // scoville 배열을 minHeap으로 옮기기 | ||
| for (int s: scoville) { | ||
| minHeap.offer(s); | ||
| } | ||
|
|
||
| // minHeap의 크기가 1보다 작아질 때까지 반복 | ||
| while (minHeap.size() > 1) { | ||
| // 이미 K보다 크다면 바로 return | ||
| if (minHeap.peek() >= K) { | ||
| return answer; | ||
| } | ||
| int a = minHeap.poll(); | ||
| int b = minHeap.poll(); | ||
| minHeap.offer(a + b * 2); | ||
| answer++; | ||
| } | ||
|
|
||
| // 마지막 하나도 확인해야 한다. | ||
| if (minHeap.peek() >= K) { | ||
| return answer; | ||
| } | ||
|
|
||
| return -1; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
중간에 있는 빈공간(.) 을 for문으로 바로 윗 칸의 내용과 swap 하여 빈공간을 점점 위로 올리는 식으로 동작하는 것이 인상적이였습니다.
저는 아래 부터 stack으로 뿌요뿌요만 축척한 후 다시 아래부터 재배치하는 식으로 구현했는데 다른 알고리즘으로 동작 할 수 있네요