diff --git "a/minjeong/Backtracking/2025-07-21-[\353\260\261\354\244\200]-#15683-\352\260\220\354\213\234.py" "b/minjeong/Backtracking/2025-07-21-[\353\260\261\354\244\200]-#15683-\352\260\220\354\213\234.py" new file mode 100644 index 0000000..4a01774 --- /dev/null +++ "b/minjeong/Backtracking/2025-07-21-[\353\260\261\354\244\200]-#15683-\352\260\220\354\213\234.py" @@ -0,0 +1,69 @@ +import sys +import copy + +input = sys.stdin.readline + +N, M = map(int, input().split()) +office = [list(map(int, input().split())) for _ in range(N)] + +# CCTV 위치 저장 +cctvs = [] +for i in range(N): + for j in range(M): + if 1 <= office[i][j] <= 5: + cctvs.append((i, j, office[i][j])) + +total_cctv = len(cctvs) # 전체 CCTV 개수 +min_blind_spot = 1e9 # 정답으로 반환할 사각지대 개수 + +# CCTV별 방향 설정 +directions = [(-1, 0), (0, 1), (1, 0), (0, -1)] # 상 -> 우 -> 하 -> 좌 +cctv_dirs = { + 1: [[0], [1], [2], [3]], + 2: [[0, 2], [1, 3]], + 3: [[0, 1], [1, 2], [2, 3], [3, 0]], + 4: [[0, 1, 2], [1, 2, 3], [2, 3, 0], [3, 0, 1]], + 5: [[0, 1, 2, 3]] +} + + +# 사각지대 개수 구하는 함수 +def count_zero(temp): + cnt = 0 + for i in range(N): + for j in range(M): + if temp[i][j] == 0: + cnt += 1 + return cnt + + +# DFS 함수 +def dfs(idx, office): + global min_blind_spot + # 종료 조건 + if idx == total_cctv: + # 종료할 때는 사각지대(= 0) 개수 세고, 최소값 갱신해야 한다. + blind_spot = count_zero(office) + min_blind_spot = min(min_blind_spot, blind_spot) + return + + x, y, number = cctvs[idx] + for cctv_dir in cctv_dirs[number]: + # temp에 현재 office 리스트 깊은 복사 + temp = copy.deepcopy(office) + + for dir in cctv_dir: + dx, dy = directions[dir] + nx, ny = x + dx, y + dy + + while 0 <= nx < N and 0 <= ny < M and temp[nx][ny] != 6: + if temp[nx][ny] == 0: + temp[nx][ny] = '#' # CCTV 감시OK + nx += dx + ny += dy + + dfs(idx + 1, temp) + + +dfs(0, office) +print(min_blind_spot) \ No newline at end of file diff --git "a/minjeong/Backtracking/2025-07-22-[\353\260\261\354\244\200]-#16987-\352\263\204\353\236\200\354\234\274\353\241\234\352\263\204\353\236\200\354\271\230\352\270\260.py" "b/minjeong/Backtracking/2025-07-22-[\353\260\261\354\244\200]-#16987-\352\263\204\353\236\200\354\234\274\353\241\234\352\263\204\353\236\200\354\271\230\352\270\260.py" new file mode 100644 index 0000000..cb04648 --- /dev/null +++ "b/minjeong/Backtracking/2025-07-22-[\353\260\261\354\244\200]-#16987-\352\263\204\353\236\200\354\234\274\353\241\234\352\263\204\353\236\200\354\271\230\352\270\260.py" @@ -0,0 +1,56 @@ +import sys + +input = sys.stdin.readline + +N = int(input()) # 계란의 수 +eggs = [list(map(int, input().split())) for _ in range(N)] # 계란의 내구도와 무게를 담은 리스트 + +max_broken = 0 # 최대 깨진 계란 수를 저장하는 변수 + + +def count_broken_eggs(eggs): + # 깨진 계란의 수 구하기 + count = 0 + for S, W in eggs: + if S <= 0: + count += 1 + return count + + +def dfs(cur_idx): + global max_broken + # 모든 계란을 한 번씩 든 경우 + if cur_idx == N: + # 깨진 계란의 수 세고 max_broken 갱신 + max_broken = max(max_broken, count_broken_eggs(eggs)) + return + + # 현재 계란이 깨졌다면 다음 계란으로 넘어간다. + if eggs[cur_idx][0] <= 0: + dfs(cur_idx + 1) + return + + broken = False # 현재 계란으로 다른 계란을 깼는지 여부 + for i in range(N): + if i == cur_idx or eggs[i][0] <= 0: + continue + + # 계란끼리 치기 + eggs[cur_idx][0] -= eggs[i][1] + eggs[i][0] -= eggs[cur_idx][1] + broken = True + + # 탐색 + dfs(cur_idx + 1) # 다음 계란으로 넘어간다. + + # 상태 복구 + eggs[cur_idx][0] += eggs[i][1] + eggs[i][0] += eggs[cur_idx][1] + + # 칠 수 있는 계란이 없었던 경우, 다음으로 넘어가야 한다. + if not broken: + dfs(cur_idx + 1) + + +dfs(0) +print(max_broken) \ No newline at end of file diff --git "a/minjeong/DFSBFS/2025-07-14-[PGS]-\355\203\200\352\262\237\353\204\230\353\262\204(java).java" "b/minjeong/DFSBFS/2025-07-14-[PGS]-\355\203\200\352\262\237\353\204\230\353\262\204(java).java" new file mode 100644 index 0000000..6c1fbc0 --- /dev/null +++ "b/minjeong/DFSBFS/2025-07-14-[PGS]-\355\203\200\352\262\237\353\204\230\353\262\204(java).java" @@ -0,0 +1,19 @@ +class Solution { + int count = 0; + + public int solution(int[] numbers, int target) { + backtracking(0, 0, numbers, target); + + return count; + } + + public void backtracking(int idx, int currSum, int[] numbers, int target) { + if (idx == numbers.length) { + if (currSum == target) count++; + return; + } + + backtracking(idx + 1, currSum + numbers[idx], numbers, target); + backtracking(idx + 1, currSum - numbers[idx], numbers, target); + } +} \ No newline at end of file diff --git "a/minjeong/DFSBFS/2025-07-18-[PGS]-\352\262\214\354\236\204\353\247\265\354\265\234\353\213\250\352\261\260\353\246\254(java).java" "b/minjeong/DFSBFS/2025-07-18-[PGS]-\352\262\214\354\236\204\353\247\265\354\265\234\353\213\250\352\261\260\353\246\254(java).java" new file mode 100644 index 0000000..74888b4 --- /dev/null +++ "b/minjeong/DFSBFS/2025-07-18-[PGS]-\352\262\214\354\236\204\353\247\265\354\265\234\353\213\250\352\261\260\353\246\254(java).java" @@ -0,0 +1,44 @@ +import java.util.*; + +class Solution { + public int solution(int[][] maps) { + int n = maps.length; + int m = maps[0].length; + + // 방문여부 리스트 + boolean[][] visited = new boolean[n][m]; + visited[0][0] = true; + + // 방향 벡터 만들기 + int[][] dirs ={{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; + + // 큐 선언 + Queue queue = new LinkedList<>(); + queue.offer(new int[]{0, 0}); + + // BFS 탐색 + while (!queue.isEmpty()) { + int[] current = queue.poll(); + int x = current[0]; + int y = current[1]; + + for (int[] dir : dirs) { + int nx = x + dir[0]; + int ny = y + dir[1]; + + if ((0 <= nx && nx < n && 0 <= ny && ny < m) + && !visited[nx][ny] + && maps[nx][ny] == 1) { + maps[nx][ny] = maps[x][y] + 1; + visited[nx][ny] = true; + queue.add(new int[]{nx, ny}); + } + } + } + + if (maps[n-1][m-1] != 1) { + return maps[n-1][m-1]; + } + return -1; + } +} \ No newline at end of file