๋ฐฑ์ค 2933๋ฒ : ๋ฏธ๋ค๋ | JAVA
๐ ์๊ณ ๋ฆฌ์ฆ ์ ํ
์ ๋ง ์ด๋ ค์ด ๋ฌธ์ ์๋ค ... ์ฌ์ค ์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ๋ 30๋ถ ์์ ํด๊ฒฐํ์ง ๋ชปํ์ ๊ฒฝ์ฐ, ํ์ด๋ฅผ ๋ณด๊ณ ๋ง๋ฌด๋ฆฌ ํ๋ ๊ฒ์ด ๊ฐ์ฅ ์ด์์ ์ด๋ผ๊ณ ์๋ ค์ ธ ์์ง๋ง ์ด ๋ฌธ์ ๋ ๋ฉฐ์น ์ ๋ถ์ก๊ณ ๋ณธ ๊ฒ ๊ฐ๋ค. ํํ
์ฐ์ , ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํ ๋จ๊ณ๋ฅผ ์ ๋ฆฌํด๋ณด์.
- ์ฃผ์ด์ง ๋์ด์ ๋ฐฉํฅ์ ๋ฏธ๋ค๋ ํ๊ดด
- ํ๊ดด๋ ์์ ์์ ํด๋ฌ์คํฐ๊ฐ ๊ณต์ค์ ๋ ์๋์ง ํ์ธ
- ํด๋ฌ์คํฐ๊ฐ ๊ณต์ค์ ๋ ์๋ค๋ฉด ๋ฐ์ผ๋ก ๋ด๋ฆด ์ ์์ ๋๊น์ง ๋ด๋ฆฌ๊ธฐ
์ ์์ ์ N๋ฒ(๋ง๋๋ฅผ ๋์ง ํ์)๋งํผ ๋ฐ๋ณตํด์ฃผ๋ฉด ๋๋ค.
ํด๋ฌ์คํฐ๋ฅผ ์์๋ด๊ธฐ ์ํด BFS๋ฅผ ์ฌ์ฉํ ์ ์์ผ๋ฉฐ, ๋๋จธ์ง๋ ๋ชจ๋ ๊ตฌํ์ด๋ผ๊ณ ๋ณผ ์ ์์ ๊ฒ ๊ฐ๋ค.
๐ ํ์ด
์์์ ์ธ๊ธํ ๋ฐ์ ๊ฐ์ด, ์ด ๋ฌธ์ ๋ 3๋จ๊ณ๋ฅผ N๋ฒ(๋ง๋๋ฅผ ๋์ง ํ์)๋งํผ ๋ฐ๋ณตํด์ฃผ๋ฉด ๋๋ค.
- ์ฃผ์ด์ง ๋์ด์ ๋ฐฉํฅ์ ๋ฏธ๋ค๋ ํ๊ดด
- ํ๊ดด๋ ์์ ์์ ํด๋ฌ์คํฐ๊ฐ ๊ณต์ค์ ๋ ์๋์ง ํ์ธ
- ํด๋ฌ์คํฐ๊ฐ ๊ณต์ค์ ๋ ์๋ค๋ฉด ๋ฐ์ผ๋ก ๋ด๋ฆด ์ ์์ ๋๊น์ง ๋ด๋ฆฌ๊ธฐ
1๋ฒ ๋จ๊ณ์ ๊ฒฝ์ฐ, 'x'๋ฅผ ๋ง๋ฌ์ ๋ '.'๋ก ๋ฐ๊ฟ์ฃผ๋ฉด ๋๊ธฐ์ ๊ฝค ๊ฐ๋จํ ์์ ์ด๋ค.
2๋ฒ ๋จ๊ณ๊ฐ ๊น๋ค๋ก์ด๋ฐ, ์ฐ์ ํด๋ฌ์คํฐ๋ฅผ ์ฐพ๊ธฐ ์ํด BFS๋ฅผ ์ฌ์ฉํ์๋ค. ๊ทธ๋ฆฌ๊ณ ํด๋ฌ์คํฐ๋ฅผ ์ด๋ฃจ๋ ๋ฏธ๋ค๋์ row ์์น๋ฅผ ํ์ธํ์ฌ, ์ต๋๊ฐ(๊ฐ์ฅ ๋ฎ์ ์์น)๊ฐ R-1์ด ์๋ ๊ฒฝ์ฐ ๊ณต์ค์ ๋ ์๋ค๊ณ ํ๋จํ๊ณ 3๋ฒ ๋จ๊ณ๋ก ๋์ด๊ฐ๋๋ก ํ์๋ค.
๋ฌธ์ ์์ ๋ ๊ฐ ๋๋ ๊ทธ ์ด์์ ํด๋ฌ์คํฐ๊ฐ ๋์์ ๋จ์ด์ง๋ ๊ฒฝ์ฐ๋ ์๋ค๊ณ ๋ช ์ํ์๊ธฐ ๋๋ฌธ์ ํ๋์ ๋ ์๋ ํด๋ฌ์คํฐ๋ฅผ ๋ฐ๊ฒฌํ๋ฉด ๋์ด์ ์ฐพ์๋ณด์ง ์๊ณ return ํ์.
3๋ฒ ๋จ๊ณ์ ๊ฒฝ์ฐ, ๋ช ์นธ์ ๋ด๋ฆด ์ ์๋์ง ๋จผ์ ํ๋จํด์ผ ํ๋ค. ์ฐ์ ์๋ ์๋ฆฌ์ ์๋ ํด๋ฌ์คํฐ๋ฅผ ๋ชจ๋ '.'์ผ๋ก ๋ฐ๊ฟ ์ง์ด๋ค. ๊ทธ๋ฆฌ๊ณ ํ ์นธ์ฉ ๋ด๋ ค๋ณด๋ฉด์ ๋ฐ๋ฅ์ด๋ ๋ค๋ฅธ ๋ฏธ๋ค๋๊ณผ ๋ฟ์ ๊ฒฝ์ฐ ๋์ด์ ๋ด๋ ค๊ฐ ์ ์๋ค๊ณ ํ๋จํ๊ณ , ๋ด๋ ค๊ฐ ์ ์๋ ์นธ ์๋ฅผ ๊ฒฐ์ ํ๋ค. ํด๋น ํด๋ฌ์คํฐ๋ฅผ ๋ชจ๋ ์ง์ด ์ํ์ด๊ธฐ ๋๋ฌธ์, ๋ด๋ฆด ์ ์๋ ์นธ ์ ๋งํผ ๋ด๋ ค์ ๋ค์ 'x'๊ฐ์ ๋ฃ์ด์ฃผ๋ฉด ๋๋ค.
๐ ์ฝ๋
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
static class Loc {
int i;
int j;
public Loc(int i, int j) {
this.i = i;
this.j = j;
}
}
static int height;
static int width;
static char[][] graph;
static int[][] clusters;
static int stick;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
height = Integer.parseInt(st.nextToken());
width = Integer.parseInt(st.nextToken());
graph = new char[height][width];
for(int i = 0; i < height; i++) {
graph[i] = br.readLine().toCharArray();
}
int chance = Integer.parseInt(br.readLine());
st = new StringTokenizer(br.readLine(), " ");
for(int i = 0; i < chance; i++) {
stick = Integer.parseInt(st.nextToken());
destroyMineral(stick, i%2==0?1:2);
setCluster();
}
// ๊ฒฐ๊ณผ ์ถ๋ ฅ
for(int i = 0; i < height; i++) {
System.out.println(graph[i]);
}
}
public static void destroyMineral(int stick, int dir) {
// dir์ด 1์ด๋ฉด ์ผ์ชฝ
if(dir == 1) {
for(int i = 0; i < width; i++) {
if(graph[height-stick][i] == 'x') {
graph[height-stick][i] = '.';
return;
}
}
}
// dir์ด 2์ด๋ฉด ์ค๋ฅธ์ชฝ
else {
for(int i = width-1; i >=0; i--) {
if(graph[height-stick][i] == 'x') {
graph[height-stick][i] = '.';
return;
}
}
}
}
public static void setCluster() {
clusters = new int[height][width]; // 0์ผ๋ก ์ด๊ธฐํ ๋จ
int clusterNum = 1;
for(int i = 0; i < height; i++) {
for(int j = 0; j < width; j++) {
if(graph[i][j] == 'x' && clusters[i][j] == 0) {
// ๋ ์๋ ํด๋ฌ์คํฐ๋ฅผ ๋ฐ๊ฒฌํ ๊ฒฝ์ฐ
if(findCluster(i, j, clusterNum))
return;
clusterNum++;
}
}
}
}
public static boolean findCluster(int i, int j, int clusterNum) {
int[] di = {0, 0, -1, 1};
int[] dj = {1, -1, 0, 0};
int lowest = -1;
Queue<Loc> q = new LinkedList<>();
ArrayList<Loc> arr = new ArrayList<>();
q.add(new Loc(i, j));
clusters[i][j] = clusterNum;
// bfs๋ก ํด๋ฌ์คํฐ ์ฐพ๊ธฐ
while(!q.isEmpty()) {
Loc now = q.poll();
lowest = Math.max(lowest, now.i);
for(int d = 0; d < 4; d++) {
int ni = now.i + di[d];
int nj = now.j + dj[d];
if(ni < 0 || nj < 0 || ni >= height || nj >= width)
continue;
if(clusters[ni][nj] == 0 && graph[ni][nj] == 'x') {
clusters[ni][nj] = clusterNum;
q.add(new Loc(ni, nj));
}
}
// ํ๋์ ํด๋ฌ์คํฐ๊ฐ arr
arr.add(now);
}
// ํด๋ฌ์คํฐ์ ๊ฐ์ฅ ์๋๊ฐ ๋ฐ๋ฅ๊ณผ ๋ง๋ฟ์์์ง ์์ ๊ฒฝ์ฐ, ํด๋ฌ์คํฐ ์ด๋
if(lowest != height-1) {
moveCluster(arr);
return true;
}
return false;
}
public static void moveCluster(ArrayList<Loc> arr) {
int move = 1;
// ์๋ ์๋ฆฌ์ ์๋ ํด๋ฌ์คํฐ ์ง์ฐ๊ธฐ
for(Loc loc : arr) {
graph[loc.i][loc.j] = '.';
}
outerLoop:
while(true) {
for(Loc loc : arr) {
// ๋ฐ์ผ๋ก ํ์นธ ๋ด๋ ธ์ ๋ ๋ฐ๋ฅ ๋์ด๊ฐ๊ฑฐ๋ ๋ค๋ฅธ ํด๋ฌ์คํฐ๋ ๊ฒน์น๊ธฐ ์ ๊น์ง๋ง ๋ด๋ฆฌ๊ธฐ
if(loc.i + move == height || graph[loc.i + move][loc.j] == 'x') {
move--;
break outerLoop;
}
}
move++;
}
// ์
๋ฐ์ดํธ
for(Loc loc : arr) {
graph[loc.i + move][loc.j] = 'x';
}
}
}