Skip to content

Commit 5a1decc

Browse files
authored
Merge pull request #237 from AlgorithmStudy-Allumbus/minjeong3
Minjeong / 7월 4주차 / 4문제
2 parents c7e6887 + fc26fb1 commit 5a1decc

File tree

4 files changed

+188
-0
lines changed

4 files changed

+188
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import sys
2+
import copy
3+
4+
input = sys.stdin.readline
5+
6+
N, M = map(int, input().split())
7+
office = [list(map(int, input().split())) for _ in range(N)]
8+
9+
# CCTV 위치 저장
10+
cctvs = []
11+
for i in range(N):
12+
for j in range(M):
13+
if 1 <= office[i][j] <= 5:
14+
cctvs.append((i, j, office[i][j]))
15+
16+
total_cctv = len(cctvs) # 전체 CCTV 개수
17+
min_blind_spot = 1e9 # 정답으로 반환할 사각지대 개수
18+
19+
# CCTV별 방향 설정
20+
directions = [(-1, 0), (0, 1), (1, 0), (0, -1)] # 상 -> 우 -> 하 -> 좌
21+
cctv_dirs = {
22+
1: [[0], [1], [2], [3]],
23+
2: [[0, 2], [1, 3]],
24+
3: [[0, 1], [1, 2], [2, 3], [3, 0]],
25+
4: [[0, 1, 2], [1, 2, 3], [2, 3, 0], [3, 0, 1]],
26+
5: [[0, 1, 2, 3]]
27+
}
28+
29+
30+
# 사각지대 개수 구하는 함수
31+
def count_zero(temp):
32+
cnt = 0
33+
for i in range(N):
34+
for j in range(M):
35+
if temp[i][j] == 0:
36+
cnt += 1
37+
return cnt
38+
39+
40+
# DFS 함수
41+
def dfs(idx, office):
42+
global min_blind_spot
43+
# 종료 조건
44+
if idx == total_cctv:
45+
# 종료할 때는 사각지대(= 0) 개수 세고, 최소값 갱신해야 한다.
46+
blind_spot = count_zero(office)
47+
min_blind_spot = min(min_blind_spot, blind_spot)
48+
return
49+
50+
x, y, number = cctvs[idx]
51+
for cctv_dir in cctv_dirs[number]:
52+
# temp에 현재 office 리스트 깊은 복사
53+
temp = copy.deepcopy(office)
54+
55+
for dir in cctv_dir:
56+
dx, dy = directions[dir]
57+
nx, ny = x + dx, y + dy
58+
59+
while 0 <= nx < N and 0 <= ny < M and temp[nx][ny] != 6:
60+
if temp[nx][ny] == 0:
61+
temp[nx][ny] = '#' # CCTV 감시OK
62+
nx += dx
63+
ny += dy
64+
65+
dfs(idx + 1, temp)
66+
67+
68+
dfs(0, office)
69+
print(min_blind_spot)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import sys
2+
3+
input = sys.stdin.readline
4+
5+
N = int(input()) # 계란의 수
6+
eggs = [list(map(int, input().split())) for _ in range(N)] # 계란의 내구도와 무게를 담은 리스트
7+
8+
max_broken = 0 # 최대 깨진 계란 수를 저장하는 변수
9+
10+
11+
def count_broken_eggs(eggs):
12+
# 깨진 계란의 수 구하기
13+
count = 0
14+
for S, W in eggs:
15+
if S <= 0:
16+
count += 1
17+
return count
18+
19+
20+
def dfs(cur_idx):
21+
global max_broken
22+
# 모든 계란을 한 번씩 든 경우
23+
if cur_idx == N:
24+
# 깨진 계란의 수 세고 max_broken 갱신
25+
max_broken = max(max_broken, count_broken_eggs(eggs))
26+
return
27+
28+
# 현재 계란이 깨졌다면 다음 계란으로 넘어간다.
29+
if eggs[cur_idx][0] <= 0:
30+
dfs(cur_idx + 1)
31+
return
32+
33+
broken = False # 현재 계란으로 다른 계란을 깼는지 여부
34+
for i in range(N):
35+
if i == cur_idx or eggs[i][0] <= 0:
36+
continue
37+
38+
# 계란끼리 치기
39+
eggs[cur_idx][0] -= eggs[i][1]
40+
eggs[i][0] -= eggs[cur_idx][1]
41+
broken = True
42+
43+
# 탐색
44+
dfs(cur_idx + 1) # 다음 계란으로 넘어간다.
45+
46+
# 상태 복구
47+
eggs[cur_idx][0] += eggs[i][1]
48+
eggs[i][0] += eggs[cur_idx][1]
49+
50+
# 칠 수 있는 계란이 없었던 경우, 다음으로 넘어가야 한다.
51+
if not broken:
52+
dfs(cur_idx + 1)
53+
54+
55+
dfs(0)
56+
print(max_broken)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
int count = 0;
3+
4+
public int solution(int[] numbers, int target) {
5+
backtracking(0, 0, numbers, target);
6+
7+
return count;
8+
}
9+
10+
public void backtracking(int idx, int currSum, int[] numbers, int target) {
11+
if (idx == numbers.length) {
12+
if (currSum == target) count++;
13+
return;
14+
}
15+
16+
backtracking(idx + 1, currSum + numbers[idx], numbers, target);
17+
backtracking(idx + 1, currSum - numbers[idx], numbers, target);
18+
}
19+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public int solution(int[][] maps) {
5+
int n = maps.length;
6+
int m = maps[0].length;
7+
8+
// 방문여부 리스트
9+
boolean[][] visited = new boolean[n][m];
10+
visited[0][0] = true;
11+
12+
// 방향 벡터 만들기
13+
int[][] dirs ={{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
14+
15+
// 큐 선언
16+
Queue<int[]> queue = new LinkedList<>();
17+
queue.offer(new int[]{0, 0});
18+
19+
// BFS 탐색
20+
while (!queue.isEmpty()) {
21+
int[] current = queue.poll();
22+
int x = current[0];
23+
int y = current[1];
24+
25+
for (int[] dir : dirs) {
26+
int nx = x + dir[0];
27+
int ny = y + dir[1];
28+
29+
if ((0 <= nx && nx < n && 0 <= ny && ny < m)
30+
&& !visited[nx][ny]
31+
&& maps[nx][ny] == 1) {
32+
maps[nx][ny] = maps[x][y] + 1;
33+
visited[nx][ny] = true;
34+
queue.add(new int[]{nx, ny});
35+
}
36+
}
37+
}
38+
39+
if (maps[n-1][m-1] != 1) {
40+
return maps[n-1][m-1];
41+
}
42+
return -1;
43+
}
44+
}

0 commit comments

Comments
 (0)