곽로그
[백준 15683, Java] 감시 본문
반응형
package october2020;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;
class CCTV{
int x;
int y;
int type;
static int[] d = {4,2,4,4,1}; // type에 따라 회전 할 수 있는 경우의 수
CCTV(int x, int y, int type){
this.x =x;
this.y =y;
this.type = type;
}
public String toString() {
return "x:"+x+",y:"+y+",type:"+type;
}
}
public class DetectionDemo {
public static int[][] map;
public static int[][] backup;
public static int N;
public static int M;
public static int BLANK = 0;
public static int WALL =6;
public static ArrayList<CCTV> cctvList;
public static int[] dx = {-1,0,1,0};
public static int[] dy = {0,1,0,-1};
public static int minCount =987654321;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st;
st = new StringTokenizer(br.readLine(), " ");
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
map = new int[N][M];
backup = new int[N][M];
cctvList = new ArrayList<CCTV>();
for(int r = 0; r<N ; r++) {
st = new StringTokenizer(br.readLine()," ");
for(int c=0; c<M; c++) {
int value = Integer.parseInt(st.nextToken());
map[r][c] = value;
if(value!=WALL && value!=BLANK) {
cctvList.add(new CCTV(r,c,value-1));
}
}
}
System.out.println(cctvList);
findCCTV(0);
bw.write(String.valueOf(minCount));
bw.flush();
}
//cctvList에 있는 cctv에 대해서 모든 경우의수 탐색
public static void findCCTV(int index) {
if(index>=cctvList.size()) {
printMap();
System.out.println("===========================");
minCount=Math.min(countBlank(),minCount);
}
else {
copyMap(map, backup);
CCTV cctv = cctvList.get(index);
int cx = cctv.x;
int cy = cctv.y;
int type = cctv.type;
for(int direction=0;direction<CCTV.d[type];direction++) {
if(type==0) {
detect(direction,cx,cy);
}
else if(type==1) {
detect(direction,cx,cy);
detect(direction+2,cx,cy);
}
else if(type ==2) {
detect(direction,cx,cy);
detect(direction+1,cx,cy);
}
else if(type ==3) {
detect(direction,cx,cy);
detect(direction+1,cx,cy);
detect(direction+2,cx,cy);
}
else if(type ==4) {
detect(direction,cx,cy);
detect(direction+1,cx,cy);
detect(direction+2,cx,cy);
detect(direction+3,cx,cy);
}
System.out.println("----------------");
printMap();
System.out.println("----------------");
findCCTV(index+1);
copyMap(backup,map);
}
}
}
public static void detect(int direction, int x, int y) {
direction = direction%4;
if(direction ==0) {
int tempX = x+dx[direction];
while(tempX>=0 && map[tempX][y]!=WALL) {
map[tempX--][y] =7;
}
}
else if(direction ==1) {
int tempY = y+dy[direction];
while(tempY<M && map[x][tempY]!=WALL) {
map[x][tempY++] =7;
}
}
else if(direction ==2) {
int tempX = x+dx[direction];
while(tempX<N && map[tempX][y]!=WALL) {
map[tempX++][y] =7;
}
}
else if( direction ==3) {
int tempY = y+dy[direction];
while(tempY>=0 && map[x][tempY]!=WALL) {
map[x][tempY--] =7;
}
}
}
public static void copyMap(int[][] source, int[][] dest) {
for(int r =0 ;r<N ; r++) {
for(int c=0;c<M;c++) {
dest[r][c] = source[r][c];
}
}
}
public static int countBlank() {
int result = 0;
for(int r=0;r<N;r++) {
for(int c=0; c<M; c++) {
if(map[r][c]==BLANK) {
result++;
}
}
}
return result;
}
public static void printMap() {
for(int r=0;r<N;r++) {
for(int c=0;c<M; c++) {
System.out.print(map[r][c]+" ");
}
System.out.println();
}
}
}
반응형
Comments