Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
Tags
more
Archives
Today
Total
관리 메뉴

코드와이

[BAEKJOON] 15683. 감시 본문

acmicpc

[BAEKJOON] 15683. 감시

코드와이 2021. 4. 21. 14:44

 

문제링크

www.acmicpc.net/problem/15683

 

15683번: 감시

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

www.acmicpc.net

 

package acmicpc.Gold5;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class 감시 {

	static int n, m, arr[][], ans;
	static List<int[]> list;
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		n = Integer.parseInt(st.nextToken());
		m = Integer.parseInt(st.nextToken());
		
		arr = new int[n][m];
		list = new ArrayList<>();
		for(int i = 0 ; i < n ; i++) {
			st = new StringTokenizer(br.readLine());
			for(int j = 0 ; j < m ; j++) {
				arr[i][j] = Integer.parseInt(st.nextToken());
				if(arr[i][j] < 6 && arr[i][j] > 0) list.add(new int[] {i,j});
			}
		}
		ans = 987654321;
		go(0, arr);
		System.out.println(ans);
	}
	
	static int[] dr = {-1,1,0,0};
	static int[] dc = {0,0,-1,1};
	public static void go(int idx, int[][] map) {
		
		if(idx == list.size()) {
			ans = Math.min(ans, count(map));
			return;
		}
		
		int[][] tmp = new int[n][m];
		int r = list.get(idx)[0];
		int c = list.get(idx)[1];
		
		if(arr[r][c] == 5) {
			
			for(int d = 0 ; d < 4 ; d++) {
				change(map,r,c,d);
			}
			
			go(idx+1, map);
			
		} else if(arr[r][c] == 4) {
			
			for(int i = 0 ; i < 4 ; i++) {
				tmp = copy(map);
				for(int d = 0 ; d < 4 ; d++) {
					
					if(d == i) continue;
					
					change(tmp, r, c, d);
				}
				go(idx+1, tmp);
			}
		} else if(arr[r][c] == 3) {
			// 상좌(02), 상우(03), 하좌(12), 하우(13)
			tmp = copy(map);
			change(tmp,r,c,0);
			change(tmp,r,c,2);
			go(idx + 1, tmp);
			
			tmp = copy(map);
			change(tmp,r,c,0);
			change(tmp,r,c,3);
			go(idx + 1, tmp);
			
			tmp = copy(map);
			change(tmp,r,c,1);
			change(tmp,r,c,2);
			go(idx + 1, tmp);
			
			tmp = copy(map);
			change(tmp,r,c,1);
			change(tmp,r,c,3);
			go(idx + 1, tmp);
			
		} else if(arr[r][c] == 2) {
			tmp = copy(map);
			change(tmp,r,c,0);
			change(tmp,r,c,1);
			go(idx + 1, tmp);
			
			tmp = copy(map);
			change(tmp,r,c,2);
			change(tmp,r,c,3);
			go(idx + 1, tmp);
			
		} else if(arr[r][c] == 1) {

			for(int d = 0 ; d < 4 ; d++) {
				tmp = copy(map);
				change(tmp,r,c,d);
				go(idx+1, tmp);
			}
		} 
		
	}
	
	public static void change(int[][] tmp, int r, int c, int d) {
		int t = 1;
		while(true) {
			int nr = r + dr[d] * t;
			int nc = c + dc[d] * t;
			if(nr < 0 || nc < 0 || nr >= n || nc >= m || tmp[nr][nc] == 6) break;
			if(tmp[nr][nc] == 0) {
				tmp[nr][nc] = 7;
			}
			t++;
		}
	}
	
	public static int[][] copy(int[][] map){
		int[][] tmp = new int[n][m];
		for(int i = 0 ; i < n ; i++) {
			for(int j = 0 ; j < m ; j++) {
				tmp[i][j] = map[i][j];
			}
		}
		return tmp;
	}
	
	public static int count(int[][] tmp) {
		int cnt = 0;
		for(int i = 0 ; i < n ; i++) {
			for(int j = 0 ; j < m ; j++) {
				if(tmp[i][j] == 0) cnt++;
			}
		}
		return cnt;
	}
}

'acmicpc' 카테고리의 다른 글

[BAEKJOON] 11403. 경로 찾기  (0) 2021.04.22
[BAEKJOON] 11404. 플로이드  (0) 2021.04.22
[BAEKJOON] 16985. Maaaaaaaaaze  (0) 2021.04.21
[BAEKJOON] 7579. 앱  (0) 2021.04.19
[BAEKJOON] 1516. 게임 개발  (0) 2021.04.19