diff --git "a/_WeeklyChallenges/W32-[Graph(DFSBFS)]/Assignment_BOJ_2468_\341\204\213\341\205\241\341\206\253\341\204\214\341\205\245\341\206\253\341\204\213\341\205\247\341\206\274\341\204\213\341\205\247\341\206\250.py" "b/_WeeklyChallenges/W32-[Graph(DFSBFS)]/Assignment_BOJ_2468_\341\204\213\341\205\241\341\206\253\341\204\214\341\205\245\341\206\253\341\204\213\341\205\247\341\206\274\341\204\213\341\205\247\341\206\250.py" new file mode 100644 index 0000000..5e5ba32 --- /dev/null +++ "b/_WeeklyChallenges/W32-[Graph(DFSBFS)]/Assignment_BOJ_2468_\341\204\213\341\205\241\341\206\253\341\204\214\341\205\245\341\206\253\341\204\213\341\205\247\341\206\274\341\204\213\341\205\247\341\206\250.py" @@ -0,0 +1,100 @@ +""" +BOJ #2468. 안전 영역 (실버2) +https://www.acmicpc.net/problem/2468 +유형: Graph, DFS, BFS +""" + +""" +DFS 풀이 +""" +import sys +sys.setrecursionlimit(100000) + +input = sys.stdin.readline + + +directions = [(-1, 0), (0, 1), (1, 0), (0, -1)] +def dfs(x, y): + visited[x][y] = True + for dx, dy in directions: + nx, ny = x + dx, y + dy + if 0 <= nx < N and 0 <= ny < N and \ + not visited[nx][ny] and map[nx][ny] > h: + dfs(nx, ny) + + +N = int(input()) +map = [list(map(int, input().split())) for _ in range(N)] +max_height = 0 + +# 맵 내의 최대값 구하기 +for m in map: + max_height = max(max_height, max(m)) + +answer = 0 + +for h in range(0, max_height + 1): # 물이 잠기지 않는 상황을 고려하여 0부터 시작한다. + visited = [[False for _ in range(N)] for _ in range(N)] + count = 0 + + for i in range(N): + for j in range(N): + if not visited[i][j] and map[i][j] > h: + dfs(i, j) + count += 1 + + answer = max(answer, count) + +print(answer) + +""" +BFS 풀이 +""" +from collections import deque + +n = int(input()) +graph = [] +maxNum = 0 + +for i in range(n): + graph.append(list(map(int, input().split()))) + for j in range(n): + if graph[i][j] > maxNum: + maxNum = graph[i][j] + +dx = [0, 0, 1, -1] +dy = [1, -1, 0, 0] + + +def bfs(a, b, value, visited): + q = deque() + q.append((a, b)) + visited[a][b] = 1 + + while q: + x, y = q.popleft() + + for i in range(4): + nx = x + dx[i] + ny = y + dy[i] + if 0 <= nx < n and 0 <= ny < n: + if graph[nx][ny] > value and visited[nx][ny] == 0: + visited[nx][ny] = 1 + q.append((nx, ny)) + + +result = 0 +for i in range(maxNum): + visited = [[0] * n for i in range(n)] + cnt = 0 + + for j in range(n): + for k in range(n): + if graph[j][k] > i and visited[j][k] == 0: + bfs(j, k, i, visited) + cnt += 1 + + if result < cnt: + result = cnt + +print(result) \ No newline at end of file diff --git a/_WeeklyChallenges/W32-[Graph(DFSBFS)]/README.md b/_WeeklyChallenges/W32-[Graph(DFSBFS)]/README.md new file mode 100644 index 0000000..cd903be --- /dev/null +++ b/_WeeklyChallenges/W32-[Graph(DFSBFS)]/README.md @@ -0,0 +1,18 @@ +## 🚀7월 5주차 (7/29) 스터디 발제 주제: Graph (DFS/BFS) +> 발제자: 김민정 (@Mingguriguri) + +> [!NOTE] +> 주제: Graph (DFS/BFS) + +### 🗂️ 스터디 자료 +- PDF: [바로가기 +](Study_PGS_81302.pdf) + + +### 📖 문제 +- [프로그래머스 #81302. 거리두기 확인하기](https://www.acmicpc.net/problem/1759): Graph (DFS/BFS) / Level 2 +- 정답 코드: [Study_PGS_81302_거리두기확인하기.py](Study_PGS_81302_거리두기확인하기.py) + +### 💻 과제 +- [백준 #2468. 안전구역](https://www.acmicpc.net/problem/2468): Graph (DFS/BFS) / 실버1 +- 정답 코드: [Assignment_BOJ_2468_안전구역.py](Assignment_BOJ_2468_안전구역.py) diff --git a/_WeeklyChallenges/W32-[Graph(DFSBFS)]/Study_PGS_81302.pdf b/_WeeklyChallenges/W32-[Graph(DFSBFS)]/Study_PGS_81302.pdf new file mode 100644 index 0000000..908c6c5 Binary files /dev/null and b/_WeeklyChallenges/W32-[Graph(DFSBFS)]/Study_PGS_81302.pdf differ diff --git "a/_WeeklyChallenges/W32-[Graph(DFSBFS)]/Study_PGS_81302_\341\204\200\341\205\245\341\204\205\341\205\265\341\204\203\341\205\256\341\204\200\341\205\265\341\204\222\341\205\252\341\206\250\341\204\213\341\205\265\341\206\253\341\204\222\341\205\241\341\204\200\341\205\265.py" "b/_WeeklyChallenges/W32-[Graph(DFSBFS)]/Study_PGS_81302_\341\204\200\341\205\245\341\204\205\341\205\265\341\204\203\341\205\256\341\204\200\341\205\265\341\204\222\341\205\252\341\206\250\341\204\213\341\205\265\341\206\253\341\204\222\341\205\241\341\204\200\341\205\265.py" new file mode 100644 index 0000000..302adef --- /dev/null +++ "b/_WeeklyChallenges/W32-[Graph(DFSBFS)]/Study_PGS_81302_\341\204\200\341\205\245\341\204\205\341\205\265\341\204\203\341\205\256\341\204\200\341\205\265\341\204\222\341\205\252\341\206\250\341\204\213\341\205\265\341\206\253\341\204\222\341\205\241\341\204\200\341\205\265.py" @@ -0,0 +1,103 @@ +""" +PGS #81032. 거리두기 확인하기 (Level 2) +https://school.programmers.co.kr/learn/courses/30/lessons/81302 +유형: Brute Force, Graph, BFS +""" + +""" +풀이1: 완전 탐색 +""" + + +def solution(places): + def is_safe(place): + for i in range(5): + for j in range(5): + if place[i][j] != 'P': + continue + + for dx, dy in dirs: + ni, nj = i + dx, j + dy + if not (0 <= ni < 5 and 0 <= nj < 5): + continue + if place[ni][nj] != 'P': + continue + + dist = abs(dx) + abs(dy) + if dist == 1: + return 0 # 거리 1에서 바로 P면 위반 + elif dist == 2: + # 파티션 여부 확인 + if dx == 0: # 수평 + if place[i][j + dy // 2] != 'X': + return 0 + elif dy == 0: # 수직 + if place[i + dx // 2][j] != 'X': + return 0 + else: # 대각선 + if place[i][nj] != 'X' or place[ni][j] != 'X': + return 0 + return 1 + + dirs = [ + (-1, 0), (1, 0), (0, -1), (0, 1), # 거리 1 + (-2, 0), (2, 0), (0, -2), (0, 2), # 일직선 거리 2 + (-1, -1), (-1, 1), (1, -1), (1, 1) # 대각선 거리 2 + ] + + answer = [] + for place in places: + answer.append(is_safe(place)) + + return answer + + +""" +풀이2: BFS +""" +from collections import deque + + +def bfs(p): + start = [] + + for i in range(5): # 시작점이 되는 P 좌표 구하기 + for j in range(5): + if p[i][j] == 'P': + start.append([i, j]) + + for s in start: + queue = deque([s]) # 큐에 초기값 + visited = [[0] * 5 for i in range(5)] # 방문 처리 리스트 + distance = [[0] * 5 for i in range(5)] # 경로 길이 리스트 + visited[s[0]][s[1]] = 1 + + while queue: + y, x = queue.popleft() + + dx = [-1, 1, 0, 0] # 좌우 + dy = [0, 0, -1, 1] # 상하 + + for i in range(4): + nx = x + dx[i] + ny = y + dy[i] + + if 0 <= nx < 5 and 0 <= ny < 5 and visited[ny][nx] == 0: + + if p[ny][nx] == 'O': + queue.append([ny, nx]) + visited[ny][nx] = 1 + distance[ny][nx] = distance[y][x] + 1 + + if p[ny][nx] == 'P' and distance[y][x] <= 1: + return 0 + return 1 + + +def solution(places): + answer = [] + + for i in places: + answer.append(bfs(i)) + + return answer