View
๋ฐ์ํ
https://www.acmicpc.net/problem/14502
package ๋ฐฑ์ค;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main_14502 {
static class Point {
int y, x;
public Point(int y, int x) {
super();
this.y = y;
this.x = x;
}
}
static int originMap[][];
static int copyMap[][];
static int N, M, resultMax = Integer.MIN_VALUE;
static int dx[] = { -1, 1, 0, 0 };
static int dy[] = { 0, 0, -1, 1 };
static ArrayList<Point> virus = new ArrayList<>();
public static void main(String[] args) throws 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());
originMap = new int[N][M];
// ์
๋ ฅ๊ฐ ๋ฃ๊ธฐ
for (int y = 0; y < N; y++) {
st = new StringTokenizer(br.readLine());
for (int x = 0; x < M; x++) {
int tmp = Integer.parseInt(st.nextToken());
originMap[y][x] = tmp;
if (tmp == 2) { // ๋ฐ์ด๋ฌ์ค ์ขํ๋ฅผ ๋ฆฌ์คํธ์ ๋ด์๋
virus.add(new Point(y, x));
}
}
}
// ๋ฒฝ์ 3๊ฐ ์ธ์ฐ๋ ๊ฒฝ์ฐ์ ์(์กฐํฉ)
combination(0, 0, 0);
System.out.println(resultMax);
}
private static void combination(int y, int x, int cnt) {
// ๊ธฐ๋ฅ: ๋ฒฝ 3๊ฐ๋ฅผ ๋ฝ์
if (cnt == 3) {
copy();// ๋งต ๋ณต์ฌํด์ ์ฌ์ฉ ๊ธฐ์กด์ ๋งต์ ๋ณํX
for (int i = 0; i < virus.size(); i++) {
spread(virus.get(i).y, virus.get(i).x);// ๋ฐ์ด๋ฌ์ค๋ฅผ ํผ๋จ๋ฆผ
}
servive(); // ์ด์ ๋จ์ ์ง์ญ cnt
return;
}
for (int row = 0; row < N; row++) {
for (int col = 0; col < M; col++) {
if (originMap[row][col] == 0) { // ๋ฒฝ์ด ์๋ ์ง์ญ ๋ฐ๊ฒฌ ์
originMap[row][col] = 1; // ๋ฒฝ์ ์ธ์๋ด
combination(row, col, cnt + 1);
originMap[row][col] = 0; // ๋ค์ ์๋๋๋ก ๋ณต๊ท
}
}
}
} // combi-end
private static void copy() {
// ๊ธฐ๋ฅ: ๋ฐ์ด๋ฌ์ค๋ฅผ ํผ๋จ๋ฆฌ๊ธฐ ์ํด ๋งต์ ์ ์ ์นดํผํด๋์
copyMap = new int[N][M];
for (int y = 0; y < N; y++) {
for (int x = 0; x < M; x++) {
copyMap[y][x] = originMap[y][x];
}
}
}
private static void servive() {
// ๊ธฐ๋ฅ: ๋ฐ์ด๋ฌ์ค๋ฅผ ํผํธ๋ฆฐ ๋ค ์ด์ ๋จ์ ์ฐ๊ตฌ์์ ๊ฐ์ ์นด์ดํธ
// ์นด์ดํธ ์ดํ ์ต๋๊ฐ ๊ฐฑ์
int cnt = 0;
for (int y = 0; y < N; y++) {
for (int x = 0; x < M; x++) {
if (copyMap[y][x] == 0) {
cnt++;
}
}
} // ์ฐ๊ตฌ์ ๊ฐ์ ์นด์ดํธ ๋
if (resultMax < cnt) {
resultMax = cnt;
}
}
private static void spread(int y, int x) {
// ๊ธฐ๋ฅ : 2(๋ฐ์ด๋ฌ์ค)๋ฅผ ์ํ์ข์ฐ๋ก ํผ๋จ๋ฆผ
Queue<Point> que = new ArrayDeque<>();
que.add(new Point(y, x));
while (!que.isEmpty()) {
Point node = que.poll();
for (int i = 0; i < 4; i++) {
int nx = node.x + dx[i];
int ny = node.y + dy[i];
if (nx >= 0 && ny >= 0 && nx < M && ny < N) {
if (copyMap[ny][nx] == 0) {
copyMap[ny][nx] = 2;
que.add(new Point(ny, nx));
}
}
}
}
}
}
๋ฐ์ํ
'์๊ณ ๋ฆฌ์ฆ > ์ฝ๋ฉํ ์คํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] 2๏ธโฃ์ต์๊ฐ ๋ง๋ค๊ธฐ (JAVA) (0) | 2023.02.24 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] 2๏ธโฃ ๋ฌด์ธ๋ ์ฌํ(JAVA) (0) | 2023.02.24 |
[๋ฐฑ์ค] 2023: ์ ๊ธฐํ ์์ (Java) (0) | 2022.10.06 |
[SWEA_3307] ์ต์ฅ ์ฆ๊ฐ ์์ด(LIS, Longest Increasing Subsequence) (0) | 2022.10.06 |
[ํ๋ก๊ทธ๋๋จธ์ค] ๋ณดํธ์์์ ์ค์ฑํํ ๋๋ฌผ (0) | 2022.10.06 |
reply