Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions minjeong/Backtracking/2025-07-21-[백준]-#15683-감시.py
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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)
19 changes: 19 additions & 0 deletions minjeong/DFSBFS/2025-07-14-[PGS]-타겟넘버(java).java
Original file line number Diff line number Diff line change
@@ -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);
}
}
44 changes: 44 additions & 0 deletions minjeong/DFSBFS/2025-07-18-[PGS]-게임맵최단거리(java).java
Original file line number Diff line number Diff line change
@@ -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<int[]> 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;
}
}
Loading