From d32adb78b11687d3b70eb24573ee1009653d9aa5 Mon Sep 17 00:00:00 2001 From: YoonYn9915 Date: Tue, 1 Apr 2025 19:52:45 +0900 Subject: [PATCH 1/3] =?UTF-8?q?[BOJ]=20=EC=A0=81=EB=A1=9D=EC=83=89?= =?UTF-8?q?=EC=95=BD=20/=20=EA=B3=A8=EB=93=9C=205=20/=2045=EB=B6=84(?= =?UTF-8?q?=EC=9D=B4=EC=A0=84=EC=97=90=20=ED=92=80=EC=97=88=EB=8D=98=20?= =?UTF-8?q?=EB=AC=B8=EC=A0=9C=EC=9D=B8=EB=8D=B0=20=EB=8B=A4=EC=8B=9C=20?= =?UTF-8?q?=ED=92=80=EC=96=B4=EB=B4=84)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.acmicpc.net/problem/10026 --- ...01\353\241\235\354\203\211\354\225\275.py" | 95 +++++++++---------- 1 file changed, 43 insertions(+), 52 deletions(-) diff --git "a/YoonYn9915/Graph/2024-07-01-[\353\260\261\354\244\200]-#10026-\354\240\201\353\241\235\354\203\211\354\225\275.py" "b/YoonYn9915/Graph/2024-07-01-[\353\260\261\354\244\200]-#10026-\354\240\201\353\241\235\354\203\211\354\225\275.py" index 651a56be..0aa00a9e 100644 --- "a/YoonYn9915/Graph/2024-07-01-[\353\260\261\354\244\200]-#10026-\354\240\201\353\241\235\354\203\211\354\225\275.py" +++ "b/YoonYn9915/Graph/2024-07-01-[\353\260\261\354\244\200]-#10026-\354\240\201\353\241\235\354\203\211\354\225\275.py" @@ -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}") From 95b64267456d92dbd09337835c642ca7083622ce Mon Sep 17 00:00:00 2001 From: YoonYn9915 Date: Sat, 5 Apr 2025 23:01:39 +0900 Subject: [PATCH 2/3] =?UTF-8?q?[BOJ]=20=EB=B2=84=EC=84=AF=20=EB=86=8D?= =?UTF-8?q?=EC=9E=A5=20/=20=EC=8B=A4=EB=B2=84=201=20/=2080=EB=B6=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.acmicpc.net/problem/27737 --- ...4\354\204\257 \353\206\215\354\236\245.py" | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 "YoonYn9915/Graph/2025-04-01-[\353\260\261\354\244\200]-#27737-\353\262\204\354\204\257 \353\206\215\354\236\245.py" diff --git "a/YoonYn9915/Graph/2025-04-01-[\353\260\261\354\244\200]-#27737-\353\262\204\354\204\257 \353\206\215\354\236\245.py" "b/YoonYn9915/Graph/2025-04-01-[\353\260\261\354\244\200]-#27737-\353\262\204\354\204\257 \353\206\215\354\236\245.py" new file mode 100644 index 00000000..4ab56739 --- /dev/null +++ "b/YoonYn9915/Graph/2025-04-01-[\353\260\261\354\244\200]-#27737-\353\262\204\354\204\257 \353\206\215\354\236\245.py" @@ -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}") From 6f11734f02dd9cd5c2f5b4dba30f1e7215c63ee1 Mon Sep 17 00:00:00 2001 From: YoonYn9915 Date: Sun, 6 Apr 2025 00:12:24 +0900 Subject: [PATCH 3/3] =?UTF-8?q?[PGS]=20=EB=8B=A8=EC=96=B4=20=EB=B3=80?= =?UTF-8?q?=ED=99=98/=20level=203=20/=2045=EB=B6=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://school.programmers.co.kr/learn/courses/30/lessons/43163 --- ...0\354\226\264 \353\263\200\355\231\230.py" | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 "YoonYn9915/Graph/2025-04-06-[\355\224\204\353\241\234\352\267\270\353\236\230\353\250\270\354\212\244]-#43163-\353\213\250\354\226\264 \353\263\200\355\231\230.py" diff --git "a/YoonYn9915/Graph/2025-04-06-[\355\224\204\353\241\234\352\267\270\353\236\230\353\250\270\354\212\244]-#43163-\353\213\250\354\226\264 \353\263\200\355\231\230.py" "b/YoonYn9915/Graph/2025-04-06-[\355\224\204\353\241\234\352\267\270\353\236\230\353\250\270\354\212\244]-#43163-\353\213\250\354\226\264 \353\263\200\355\231\230.py" new file mode 100644 index 00000000..14c8954c --- /dev/null +++ "b/YoonYn9915/Graph/2025-04-06-[\355\224\204\353\241\234\352\267\270\353\236\230\353\250\270\354\212\244]-#43163-\353\213\250\354\226\264 \353\263\200\355\231\230.py" @@ -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 \ No newline at end of file