문제
스타트링크의 사무실은 1×1크기의 정사각형으로 나누어져 있는 N×M 크기의 직사각형으로 나타낼 수 있다. 사무실에는 총 K개의 CCTV가 설치되어져 있는데, CCTV는 5가지 종류가 있다. 각 CCTV가 감시할 수 있는 방법은 다음과 같다.

1번 CCTV는 한 쪽 방향만 감시할 수 있다. 2번과 3번은 두 방향을 감시할 수 있는데, 2번은 감시하는 방향이 서로 반대방향이어야 하고, 3번은 직각 방향이어야 한다. 4번은 세 방향, 5번은 네 방향을 감시할 수 있다.
CCTV는 감시할 수 있는 방향에 있는 칸 전체를 감시할 수 있다. 사무실에는 벽이 있는데, CCTV는 벽을 통과할 수 없다. CCTV가 감시할 수 없는 영역은 사각지대라고 한다.
CCTV는 회전시킬 수 있는데, 회전은 항상 90도 방향으로 해야 하며, 감시하려고 하는 방향이 가로 또는 세로 방향이어야 한다.
0 0 0 0 0 0
0 0 0 0 0 0
0 0 1 0 6 0
0 0 0 0 0 0
지도에서 0은 빈 칸, 6은 벽, 1~5는 CCTV의 번호이다. 위의 예시에서 1번의 방향에 따라 감시할 수 있는 영역을 '#'로 나타내면 아래와 같다.

CCTV는 벽을 통과할 수 없기 때문에, 1번이 → 방향을 감시하고 있을 때는 6의 오른쪽에 있는 칸을 감시할 수 없다.
0 0 0 0 0 0
0 2 0 0 0 0
0 0 0 0 6 0
0 6 0 0 2 0
0 0 0 0 0 0
0 0 0 0 0 5
위의 예시에서 감시할 수 있는 방향을 알아보면 아래와 같다.

CCTV는 CCTV를 통과할 수 있다. 아래 예시를 보자.
0 0 2 0 3
0 6 0 0 0
0 0 6 6 0
0 0 0 0 0
위와 같은 경우에 2의 방향이 ↕ 3의 방향이 ←와 ↓인 경우 감시받는 영역은 다음과 같다.
# # 2 # 3
0 6 # 0 #
0 0 6 6 #
0 0 0 0 #
사무실의 크기와 상태, 그리고 CCTV의 정보가 주어졌을 때, CCTV의 방향을 적절히 정해서, 사각 지대의 최소 크기를 구하는 프로그램을 작성하시오.
입력
첫째 줄에 사무실의 세로 크기 N과 가로 크기 M이 주어진다. (1 ≤ N, M ≤ 8)
둘째 줄부터 N개의 줄에는 사무실 각 칸의 정보가 주어진다. 0은 빈 칸, 6은 벽, 1~5는 CCTV를 나타내고, 문제에서 설명한 CCTV의 종류이다.
CCTV의 최대 개수는 8개를 넘지 않는다.
출력
첫째 줄에 사각 지대의 최소 크기를 출력한다.
예제 입력 1
4 6
0 0 0 0 0 0
0 0 0 0 0 0
0 0 1 0 6 0
0 0 0 0 0 0
예제 출력 1
20
예제 입력 2
6 6
0 0 0 0 0 0
0 2 0 0 0 0
0 0 0 0 6 0
0 6 0 0 2 0
0 0 0 0 0 0
0 0 0 0 0 5
예제 출력 2
15
예제 입력 3
6 6
1 0 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0
0 0 0 1 0 0
0 0 0 0 1 0
0 0 0 0 0 1
예제 출력 3
6
예제 입력 4
6 6
1 0 0 0 0 0
0 1 0 0 0 0
0 0 1 5 0 0
0 0 5 1 0 0
0 0 0 0 1 0
0 0 0 0 0 1
예제 출력 4
2
예제 입력 5
1 7
0 1 2 3 4 5 6
예제 출력 5
0
예제 입력 6
3 7
4 0 0 0 0 0 0
0 0 0 2 0 0 0
0 0 0 0 0 0 4
예제 출력 6
0
우선 cctv가 어디에 위치해 있는지 알아야 하기 때문에 cctv의 위치를 저장한다.
완전탐색을 이용하여 cctv들이 볼 수 있는 방향을 {0(하),1(우),2(상),3(좌)}로 생각하며 바라볼 수 있는 모든 방향에 대해 찾는다.
볼 수 있는 방향을 모두 찾았으면 각 상황별 동작을 만들어주고, 기본으로 만들어 둔 tvDirection을 수정하면 안되기 때문에 새로운 배열을 복사하여 만들어 준다.
상황을 만들 때 주의해야 할 점은 cctv를 통과할 수 있기 때문에 1~5로 저장한 cctv를 만나면 건너 띈 후 처리를 해주어야 하며,
배열 내에서만 움직여야 하고, 6을 만나면 동작을 멈추게해야 한다.
풀이 코드
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
public class 감시_15683 {
static int tvCnt = 0;
static int minBlind = Integer.MAX_VALUE;
static int[] answer;
static int[] dx = {1,0,-1,0};
static int[] dy = {0,1,0,-1};
static int[][] tvDirection;
static List<Integer> cctv;
static List<Integer> cctvX;
static List<Integer> cctvY;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
StringTokenizer st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
tvDirection = new int[x][y];
cctv = new ArrayList<>();
cctvX = new ArrayList<>();
cctvY = new ArrayList<>();
int[] watch = {0,1,2,3};
for(int i = 0; i<x; i++){
st = new StringTokenizer(br.readLine());
for(int j = 0; j<y; j++){
tvDirection[i][j] = Integer.parseInt(st.nextToken());
if(tvDirection[i][j] >0 && tvDirection[i][j] < 6) {
cctv.add(tvDirection[i][j]);
cctvX.add(i);
cctvY.add(j);
tvCnt++;
}
}
}
answer = new int[tvCnt];
see(0, watch);
pw.println(minBlind);
br.close();
pw.close();
}
public static void see(int count, int[] watch) {
if(count >= tvCnt) {
int[][] tempDirection = new int[tvDirection.length][tvDirection[0].length];
for(int i = 0; i<tvDirection.length; i++) {
System.arraycopy(tvDirection[i],0,tempDirection[i],0,tvDirection[0].length);
}
for(int i = 0; i< answer.length; i++){
if(cctv.get(i) == 1) {
active1(answer[i],cctvX.get(i),cctvY.get(i),tempDirection);
} else if(cctv.get(i) == 2) {
active2(answer[i],cctvX.get(i),cctvY.get(i),tempDirection);
} else if(cctv.get(i) == 3) {
active3(answer[i],cctvX.get(i),cctvY.get(i),tempDirection);
} else if(cctv.get(i) == 4) {
active4(answer[i],cctvX.get(i),cctvY.get(i),tempDirection);
} else if(cctv.get(i) == 5) {
active5(answer[i],cctvX.get(i),cctvY.get(i),tempDirection);
}
}
int blind = 0;
for(int i = 0; i< tempDirection.length; i++){
for(int j = 0; j< tempDirection[0].length; j++) {
if(tempDirection[i][j] == 0) {
blind++;
}
}
}
minBlind = Math.min(blind,minBlind);
return;
}
for(int i = 0; i<watch.length; i++) {
answer[count] = watch[i];
see(count+1,watch);
}
}
public static boolean checkRange(int cnt,int answer, int xL, int yL, int[][] tempDirection) {
if(xL+(dx[answer]*cnt) >= 0 && xL+(dx[answer]*cnt) < tempDirection.length
&& yL+(dy[answer]*cnt) >= 0 && yL+(dy[answer]*cnt) < tempDirection[0].length) {
return true;
} else {
return false;
}
}
public static void move(int answer, int xL, int yL, int[][] tempDirection) {
int cnt = 0;
while(checkRange(cnt, answer, xL,yL,tempDirection)){
if(tempDirection[xL+(dx[answer]*cnt)][yL+(dy[answer]*cnt)] == 0) {
tempDirection[xL+(dx[answer]*cnt)][yL+(dy[answer]*cnt)] = -1;
} else if(tempDirection[xL+(dx[answer]*cnt)][yL+(dy[answer]*cnt)] == 6) {
break;
}
cnt++;
}
}
public static void active1(int answer,int xL, int yL,int[][] tempDirection){
move(answer,xL,yL,tempDirection);
}
public static void active2(int answer,int xL, int yL,int[][] tempDirection){
if(answer % 2 == 0) {
move(0,xL,yL,tempDirection);
move(2,xL,yL,tempDirection);
}
else {
move(1,xL,yL,tempDirection);
move(3,xL,yL,tempDirection);
}
}
private static void active3(int answer, int xL, int yL, int[][] tempDirection) {
move(answer,xL,yL,tempDirection);
move((answer+1)%4 ,xL,yL,tempDirection);
}
private static void active4(int answer, int xL, int yL, int[][] tempDirection) {
move(answer,xL,yL,tempDirection);
if(answer % 2 == 1) {
move(0,xL,yL,tempDirection);
move(2,xL,yL,tempDirection);
}
else {
move(1,xL,yL,tempDirection);
move(3,xL,yL,tempDirection);
}
}
private static void active5(int answer, int xL, int yL, int[][] tempDirection) {
move(0,xL,yL,tempDirection);
move(2,xL,yL,tempDirection);
move(1,xL,yL,tempDirection);
move(3,xL,yL,tempDirection);
}
}
'알고리즘 > 백준' 카테고리의 다른 글
| [백준]좋다 1253 (0) | 2023.12.13 |
|---|---|
| [백준] 배열돌리기4 17406 (1) | 2023.12.07 |
| [백준]보석 상자 2792 (1) | 2023.12.04 |
| [백준]먹을것인가 먹힐것인가_7795 (2) | 2023.12.03 |
| [백준] 양팔저울_17610 (1) | 2023.11.27 |