-
Notifications
You must be signed in to change notification settings - Fork 5
YoonYn9915 /4월 1주차 /3문제 #164
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
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -1,68 +1,59 @@ | ||
| from collections import deque | ||
|
|
||
| # 2차원 배열을 이용한 bfs 구현 | ||
| def bfs(x, y): | ||
| dx = [-1, 1, 0, 0] | ||
| dy = [0, 0, -1, 1] | ||
|
|
||
| # 2차원 배열에서 현재 노드의 상하좌우를 검사하기 위한 dx와 dy | ||
| dx = [0, -1, 0, 1] | ||
| dy = [-1, 0, 1, 0] | ||
|
|
||
| # queue를 이용한 bfs 그래프 탐색 | ||
| queue.append((x, y)) | ||
| visited[x][y] = 1 | ||
| def bfs(graph, visited): | ||
| border = 0 | ||
| queue = deque() | ||
|
|
||
| # 영역 개수 계산 | ||
| for i in range(1, N + 1): | ||
| for j in range(1, N + 1): | ||
| # 현재 칸을 아직 방문하지 않았다면 현재 칸부터 bfs 탐색 시작 | ||
| if visited[i][j] == 0: | ||
| # 영역 개수 한 개 증가 | ||
| border += 1 | ||
| queue.append((i, j)) | ||
|
|
||
| while queue: | ||
| (x, y) = queue.popleft() | ||
| while queue: | ||
| (x, y) = queue.popleft() | ||
| # 방문처리 | ||
| visited[x][y] = 1 | ||
|
|
||
| # queue에서 pop한 현재 노드를 기준으로 상하좌우 탐색 | ||
| for k in range(4): | ||
| row = x + dx[k] | ||
| col = y + dy[k] | ||
| # 상하좌우 인접한 칸으로 이동 | ||
| for k in range(4): | ||
| nx = x + dx[k] | ||
| ny = y + dy[k] | ||
|
|
||
| # 탐색 조건 | ||
| # 1. 현재 노드가 배열의 인덱스 범위 안인지 0 <= x < n and 0 <= y < n | ||
| # 2. 같은 색인지 | ||
| # 3. 방문하지 않았는지 | ||
| if 0 <= row < n and 0 <= col < n: | ||
| if arr[x][y] == arr[row][col] and visited[row][col] == 0: | ||
| queue.append((row, col)) | ||
| visited[row][col] = 1 | ||
| # 칸이 보드 밖으로 넘어가지 않았는지,인접한 칸이 같은 색인지,아직 방문하지 않았는지 확인 | ||
| if (1 <= nx <= N and 1 <= ny <= N) and graph[x][y] == graph[nx][ny] and visited[nx][ny] == 0: | ||
| # 해당 칸 방문처리 | ||
| queue.append((nx, ny)) | ||
| visited[nx][ny] = 1 | ||
|
|
||
| return border | ||
|
|
||
| # 초기값 세팅 | ||
| n = int(input()) | ||
| visited = [[0] * n for _ in range(n)] | ||
| arr = [list(input()) for _ in range(n)] | ||
| queue = deque() | ||
|
|
||
| # 적록색약이 아닌 경우의 답 | ||
| answerForNormal = 0 | ||
| # 입력받기 | ||
| N = int(input()) | ||
| graph = [[0] * (N + 1)] | ||
| visited = [[0] * (N + 1) for _ in range(N + 1)] | ||
|
|
||
| # 적록색약인 경우의 답 | ||
| answerForColorBlindness = 0 | ||
| for _ in range(N): | ||
| graph.append([0] + list(input())) | ||
|
|
||
| # 정상인이 보는 영역 개수 반환 | ||
| num_of_normal = bfs(graph, visited) | ||
|
|
||
| # 적록색약이 아닌 경우 | ||
| for i in range(n): | ||
| for j in range(n): | ||
| if visited[i][j] == 0: | ||
| bfs(i, j) | ||
| answerForNormal += 1 | ||
| # 적록색약이 보는 영역 개수 반환 적록색약은 R,G를 구분하지 못하므로 모든 R을 G로 변환 | ||
| for i in range(1, N + 1): | ||
| for j in range(1, N + 1): | ||
| if graph[i][j] == 'R': | ||
| graph[i][j] = 'G' | ||
|
|
||
| visited = [[0] * (N + 1) for _ in range(N + 1)] | ||
| num_of_abnormal = bfs(graph, visited) | ||
|
|
||
| # 적록색약인 경우 R과 G는 같으므로 | ||
|
|
||
| for i in range(n): | ||
| for j in range(n): | ||
| if arr[i][j] == 'G': | ||
| arr[i][j] = 'R' | ||
|
|
||
| visited = [[0] * n for _ in range(n)] | ||
| for i in range(n): | ||
| for j in range(n): | ||
| if not visited[i][j]: | ||
| bfs(i,j) | ||
| answerForColorBlindness += 1 | ||
|
|
||
| print(answerForNormal, answerForColorBlindness) | ||
| print(f"{num_of_normal} {num_of_abnormal}") |
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,90 @@ | ||
| ''' | ||
|
|
||
|
|
||
| 농부 해강이는 N X N칸으로 이루어진 나무판에서 버섯 농사를 짓는다. 나무판은 버섯이 자랄 수 있는 칸과 없는 칸으로 이루어져 있다. | ||
|
|
||
|
|
||
| 각 버섯 포자는 포자가 심어진 칸을 포함해 최대 K개의 연결된 (버섯이 자랄 수 있는) 칸에 버섯을 자라게 한다. | ||
| 이때 연결된 칸은 상하좌우로 적어도 한 변을 공유하는 칸들의 집합이라고 정의한다. | ||
|
|
||
| 또한 한 칸에 버섯 포자를 여러 개 겹쳐서 심을 수 있으며, 만약 x개의 버섯 포자를 겹쳐 심으면 포자가 심어진 칸을 포함해 최대 | ||
| x X K개의 연결된 (버섯이 자랄 수 있는) 칸에 버섯이 자란다. | ||
|
|
||
|
|
||
| 해강이는 버섯 포자를 심을 때 최소 개수로만 심으려고 한다. | ||
| 해강이가 농사가 가능할지 판단하고, 농사가 가능하다면 남은 버섯 포자의 개수를 출력하시오. | ||
| 버섯 포자를 하나라도 사용하고 버섯이 자랄 수 있는 모든 칸에 버섯이 전부 자랐을 때 농사가 가능하다고 정의한다. | ||
|
|
||
| 1. N X N 칸에서 버섯을 심어야 함. 이때 심은 버섯은 상하좌우 최대 K칸으로 확산. | ||
| (버섯을 심은 칸을 시작으로 BFS 탐색 진행하면 해당 칸으로부터 버섯을 심을 수 있는 인접한 칸이 몇개인지 알수 있음) | ||
|
|
||
| 2. 2차원 배열을 순회하며 칸이 0이고 아직 방문하지 않았다면 거기서부터 BFS 탐색 시작. | ||
| 3. BFS 탐색으로 해당 칸으로부터 상하좌우 인접한 0(버섯 농사 가능 칸)이 몇 개인지 파악후 K개로 나누면 해당 구역에 몇개의 버섯 포자가 필요한지 계산 가능 | ||
| 4. 2-3을 반복하며 모든 버섯 농사 가능 구역을 세며 총 몇개의 버섯 포자가 필요한지 계산 | ||
| 5. 필요한 버섯 포자 개수가 M보다 크면 남은 버섯 개수를 출력 그렇지 않다면 IMPOSSIBLE 출력. | ||
| 5-1. 이떄 버섯 포자를 하나도 사용하지 않아도 IMPOSSIBLE 출력 | ||
|
|
||
| ''' | ||
|
|
||
| from collections import deque | ||
|
|
||
|
|
||
| def bfs(i, j, visited): | ||
| dx = [-1, 1, 0, 0] | ||
| dy = [0, 0, -1, 1] | ||
|
|
||
| queue = deque() | ||
|
|
||
| # 시작점 방문처리 | ||
| queue.append((i, j)) | ||
| visited[i][j] = 1 | ||
|
|
||
| # 시작점(x,y)으로부터 인접한 버섯 농사 가능 칸의 개수 (시작점 포함) | ||
| num = 1 | ||
|
|
||
| while queue: | ||
| x, y = queue.popleft() | ||
|
|
||
| # 상하좌우 인접한 칸에 버섯 농사 가능한지 보기 | ||
| for k in range(4): | ||
| nx = x + dx[k] | ||
| ny = y + dy[k] | ||
|
|
||
| # 그래프 범위에 있는지, 버섯 농사가 가능한지, 아직 방문하지 않았는지 확인 | ||
| if (0 <= nx <= N - 1 and 0 <= ny <= N - 1) and graph[nx][ny] == 0 and visited[nx][ny] == 0: | ||
| # 방문처리 | ||
| queue.append((nx, ny)) | ||
| visited[nx][ny] = 1 | ||
| # 버섯 농사 가능한 칸의 개수 증가 | ||
| num += 1 | ||
|
|
||
| # 해당 구역에 필요한 버섯 포자 개수 반환 | ||
| if num % K == 0: | ||
| return num // K | ||
| else: | ||
| return (num // K) + 1 | ||
|
|
||
|
|
||
| # 입력받기 | ||
| N, M, K = map(int, input().split()) | ||
|
|
||
| # 나무판 배열 | ||
| graph = [] | ||
| # 나무판 방문 배열 | ||
| visited = [[0] * N for _ in range(N)] | ||
|
|
||
| mushroom_count = 0 | ||
|
|
||
| for _ in range(N): | ||
| graph.append(list(map(int, input().split()))) | ||
|
|
||
| for i in range(N): | ||
| for j in range(N): | ||
| # 나무판이 버섯을 심을 수 있고, 아직 방문하지 않았으면 | ||
| if graph[i][j] == 0 and visited[i][j] == 0: | ||
| mushroom_count += bfs(i, j, visited) | ||
|
|
||
| if mushroom_count == 0 or mushroom_count > M: | ||
| print("IMPOSSIBLE") | ||
| else: | ||
| print(f"POSSIBLE\n{M - mushroom_count}") | ||
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,63 @@ | ||
|
|
||
| # 두 단어가 한글자만 다른지 확인하는 함수 | ||
| def count_same_letter(word1, word2): | ||
|
|
||
| count = 0 | ||
| word_length = len(word1) | ||
| # 모든 단어의 길이는 같으므로 단어의 길이만큼 반복문 돌면서 같은 글자 계산 | ||
| for i in range(word_length): | ||
| if word1[i] == word2[i]: | ||
| count += 1 | ||
|
|
||
| if word_length - 1 == count: | ||
| return True | ||
| else: | ||
| return False | ||
|
|
||
|
|
||
|
|
||
| def dfs(begin, target, words, depth): | ||
| global words_num | ||
| global min_value | ||
|
|
||
| # depth가 words의 크기보다 커지면 해당 탐색 종료 | ||
| if depth > words_num: | ||
| return | ||
|
|
||
| # begin과 target이 같다면 탐색 종료 | ||
| if begin == target: | ||
| # 현재 depth와 최솟값 비교 | ||
| min_value = min(min_value, depth) | ||
| return | ||
|
|
||
| copy_words = words[:] | ||
|
|
||
| for i in range(len(words)): | ||
| # 두 단어가 한글자만 다르다면 그 단어로 변환 | ||
| if count_same_letter(begin, words[i]): | ||
| # 단어 목록에서 현재 단어 제외하고 | ||
| removed_word = copy_words.pop(i) | ||
| # 다음 detph dfs 실행 | ||
| dfs(removed_word, target, copy_words, depth + 1) | ||
| # 배열 원복 | ||
| copy_words.insert(i, removed_word) | ||
|
|
||
|
|
||
|
|
||
|
|
||
| def solution(begin, target, words): | ||
|
|
||
| # target이 words에 없어서 변환할 수 없는 경우 | ||
| if target not in words: | ||
| return 0 | ||
|
|
||
| # 단어의 개수 | ||
| global words_num | ||
| words_num = len(words) | ||
|
|
||
| global min_value | ||
| min_value = 10000 | ||
|
|
||
| dfs(begin, target, words, 0) | ||
|
|
||
| return min_value |
Oops, something went wrong.
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.
풀이 과정을 주석으로 작성해주셔서 코드 이해하기 쉬웠습니다!