로봇 청소기
Updated:
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
static class Node {
int r;
int c;
Node(int r, int c) {
this.r = r;
this.c = c;
}
}
static int[][] arr;
static int R;
static int C;
static int[] dr = { -1, 1, 0, 0 };
static int[] dc = { 0, 0, -1, 1 };
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
R = sc.nextInt();
C = sc.nextInt();
arr = new int[R][C];
ArrayList<Node> list = new ArrayList<>();
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
arr[i][j] = sc.nextInt();
if (arr[i][j] == 0) {
list.add(new Node(i, j));
}
}
}
c(list, 0, 0, new Node[3]);
System.out.println(max);
max = 0;
}
static int max = 0;
static void c(ArrayList<Node> list, int n, int c, Node[] re) {
if (c == re.length) {
int[][] tmp = new int[R][C];
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
tmp[i][j] = arr[i][j];
}
}
for (int i = 0; i < 3; i++) {
//System.out.print(re[i].r + " " + re[i].c + " ");
tmp[re[i].r][re[i].c] = 1;
}
//System.out.println();
Queue<Node> q = new LinkedList<>();
boolean[][] che = new boolean[R][C];
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
if (tmp[i][j] == 2) {
q.add(new Node(i, j));
che[i][j] = true;
while (!q.isEmpty()) {
Node node = q.poll();
for (int k = 0; k < 4; k++) {
int nr = node.r + dr[k];
int nc = node.c + dc[k];
if (nr >= 0 && nc >= 0 && nr < R && nc < C && tmp[nr][nc] == 0
&& che[nr][nc] == false) {
tmp[nr][nc] = 2;
q.add(new Node(nr, nc));
che[nr][nc] = true;
}
}
}
}
}
}
int cnt = 0;
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
//System.out.print(tmp[i][j] + " ");
if (tmp[i][j] == 0) {
cnt++;
}
}
//System.out.println();
}
if (max < cnt)
max = cnt;
//System.out.println();
return;
}
if (n == list.size()) {
return;
}
re[c] = list.get(n);
c(list, n + 1, c + 1, re);
c(list, n + 1, c, re);
}
}
Leave a comment