[JAVA] CodeTree ์ฝ๋ํธ๋ฆฌ ๋นต
์ฝ๋ํธ๋ฆฌ | ์ฝ๋ฉํ ์คํธ ์ค๋น๋ฅผ ์ํ ์๊ณ ๋ฆฌ์ฆ ์ ์
๊ตญ๊ฐ๋ํ๊ฐ ๋ง๋ ์ฝ๋ฉ ๊ณต๋ถ์ ๊ฐ์ด๋๋ถ ์ฝ๋ฉ ์์ด๋ณด๋ถํฐ ๊ฟ์ ์ง์ฅ ์ฝํ ํฉ๊ฒฉ๊น์ง, ๊ตญ๊ฐ๋ํ๊ฐ ์์ ํ ์ปค๋ฆฌํ๋ผ์ผ๋ก ์ค๋นํด๋ณด์ธ์.
www.codetree.ai
๐ก ์ ํ์ ์ธ ์๋ฎฌ๋ ์ด์ , BFS ๋ฌธ์ ์ด๋ค. → DFS๋ ๋ถ๊ฐ๋ฅํ๋ค! ๊ฐ์ฅ ๋จผ์ ๋์ฐฉํ๋ ์์น๋ฅผ ๋ฝ์๋ด์ผ ๋๊ธฐ ๋๋ฌธ!
๋ฌธ์ ๋ฅผ ์ฝ์ด๋ณด๋ฉด ์ค๋ช ์ด ๊ต์ฅํ ์น์ ํ๊ฒ ๋์ด์๋ค.
์ ์ํด์ผํ ์ ์ 1, 2, 3์ ์์๋ฅผ ๋ฐ๋์ ์ง์ผ์ผ ํ๋ค๋ ๊ฒ! ํนํ 2๋ฒ์ ์กฐ์ฌํ์!
์๋ฅผ ๋ค์ด M๋ฒ์ ์ฌ๋๊น์ง ๋ชจ๋ ์ด๋ํ ๋ค์ ํธ์์ ์ ์ ๊ทผํ ์ ์๊ฒ ํ๋ ๊ฒ์ด ์๋๋ผ ์ด๋ ์ค 3๋ฒ์ ์ฌ๋์ด ํธ์์ ์ ๋์ฐฉํ๋ค๋ฉด ๊ทธ๋๋ถํฐ ํด๋น ํธ์์ ์ ์ ๊ทผํ ์ ์๊ฒ ๋๋ค.
๊ทธ ์ธ์๋ ๋ฌธ์ ์ ํน๋ณํ ๊ฒ์ ์๋ค. ์์ ๊ฐ 1๊ฐ์ด๊ณ 2๋ฒ์ ๋ํ ํน์ด์ ์ด ๋๋ฌ๋์ง ์์ผ๋ฏ๋ก ๋ฐ๋ก ์๊ฐํ๋ฉด์ ํ์ด์ผํ๋ค.
์ ์ฒด ์ฝ๋
import java.util.*;
import java.io.*;
public class Main {
static int N, M;
static int[][] map;
static int[][] store;
static boolean[][] v;
static ArrayDeque<int[]> people;
static int[] dx = {-1, 0, 0, 1}, dy = {0, -1, 1, 0};
public static void main(String args[]) throws Exception{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(bf.readLine(), " ");
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
map = new int[N][N];
for(int i=0; i<N; i++) {
st = new StringTokenizer(bf.readLine(), " ");
for(int j=0; j<N; j++)
map[i][j] = Integer.parseInt(st.nextToken());
}
store = new int[M][2];
v = new boolean[N][N];
for(int i=0; i<M; i++){
st = new StringTokenizer(bf.readLine(), " ");
store[i][0] = Integer.parseInt(st.nextToken())-1;
store[i][1] = Integer.parseInt(st.nextToken())-1;
}
people = new ArrayDeque<>();
int cnt = 0;
while(true) {
int size = people.size();
if(size == 0 && cnt!=0) break;
while(size-- > 0) {
int[] t = people.poll();
int[] add = bfs(t[0], t[1], t[2]);
if(add[1] == store[t[0]][0] && add[2] == store[t[0]][1]) // ํธ์์ ๋์ฐฉ
v[add[1]][add[2]] = true;
else people.offer(add);
}
if(cnt < M)
people.offer(find_basecamp(cnt, store[cnt][0], store[cnt][1]));
cnt++;
}
System.out.println(cnt);
}
static int[] bfs(int n, int x, int y) {
ArrayDeque<int[]> q = new ArrayDeque<>();
q.offer(new int[] {x, y, -1, -1});
boolean[][] visited = new boolean[N][N];
visited[x][y] = true;
while(!q.isEmpty()) {
int[] now = q.poll();
if(now[0] == store[n][0] && now[1] == store[n][1]) {
return new int[] {n, now[2], now[3]};
}
for(int d=0; d<4; d++) {
int nx = now[0] + dx[d];
int ny = now[1] + dy[d];
if(nx < 0 || ny < 0 || nx >= N || ny >= N || visited[nx][ny] || v[nx][ny]) continue;
visited[nx][ny] = true;
if(now[2] == -1) {
q.offer(new int[] {nx, ny, nx, ny});
}
else
q.offer(new int[] {nx, ny, now[2], now[3]});
}
}
return null; // number+์ด๋ํ ๊ณณ์ ์ขํ๋ฅผ ๋ฆฌํด!
}
static int[] find_basecamp(int n, int x, int y) { // @param : ํธ์์ ์ ์์น - ๊ฐ์ฅ ๊ฐ๊น์ด base camp ์ฐพ๊ธฐ
// System.out.println(x+", "+y);
ArrayDeque<int[]> q = new ArrayDeque<>();
q.offer(new int[] {x, y});
boolean[][] visited = new boolean[N][N];
visited[x][y] = true;
while(!q.isEmpty()) {
int[] now = q.poll();
if(map[now[0]][now[1]] == 1) {
v[now[0]][now[1]] = true;
return new int[] {n, now[0], now[1]};
}
for(int d=0; d<4; d++) {
int nx = now[0] + dx[d];
int ny = now[1] + dy[d];
if(nx < 0 || ny < 0 || nx >= N || ny >= N || visited[nx][ny] || v[nx][ny]) continue;
visited[nx][ny] = true;
q.offer(new int[] {nx, ny});
}
}
return null; // number+ํด๋น ๋ฒ ์ด์ค์บ ํ์ ์ขํ๋ฅผ ๋ฆฌํด!
}
}
ํ ์ค ์ฉ ์ดํด๋ณด์!
static int N, M;
static int[][] map;
static int[][] store;
static boolean[][] v;
static ArrayDeque<int[]> people;
static int[] dx = {-1, 0, 0, 1}, dy = {0, -1, 1, 0};
N, M → ์ฃผ์ด์ง ๊ฐ
map → ์ฃผ์ด์ง ์ง๋ ์ ๋ณด
store → ํธ์์ ์ ๋ณด (์ฆ, store[0][0] : 1๋ฒ ์ฌ๋์ด ๊ฐ ํธ์์ x์ขํ)
v → ํธ์์ , ๋ฒ ์ด์ค ์บ ํ์ ์ ๊ทผ ์ฌ๋ถ ๋ํ๋
people → ์ฌ๋๋ค์ ํ์ฌ ์์น๋ฅผ ์ ์ฅ
int cnt = 0;
while(true) {
int size = people.size();
if(size == 0 && cnt!=0) break;
while(size-- > 0) {
int[] t = people.poll();
int[] add = bfs(t[0], t[1], t[2]);
if(add[1] == store[t[0]][0] && add[2] == store[t[0]][1]) // ํธ์์ ๋์ฐฉ
v[add[1]][add[2]] = true;
else people.offer(add);
}
if(cnt < M)
people.offer(find_basecamp(cnt, store[cnt][0], store[cnt][1]));
cnt++;
}
๋ชจ๋ ๋์ฐฉํ์ง ๋ชปํ๋ ๊ฒฝ์ฐ๋ ์ฃผ์ด์ง์ง ์๋๋ค๊ณ ํ์ผ๋ฏ๋ก while์ true๋ก ๋๋ค.
์ด๋ ์กฐ์ฌํ ์ฌ๋์ด 0๋ช ์ด๋ผ๋ฉด while์ breakํ๋๋ฐ ์ฒ์ ์์ํ ๊ฒฝ์ฐ ๋น์ฐํ 0๋ช ์ด๋ฏ๋ก cnt๊ฐ 0์ผ๋๋ ์ ์ธํ๋ค.
while๋ฌธ์ ํตํด ํ ์ฌ๋์ ์ ๋ณด๋ฅผ ๋ฐ์์ bfsํจ์๋ฅผ ํตํด ๋ค์ ์์น๋ฅผ ์กฐ์ฌํ์ฌ add ๋ฐฐ์ด์ ๋ฃ์ด์ค๋ค. ์ด๋ ์ํ๋ ํธ์์ ์ ๋์ฐฉํ๋ค๋ฉด v๋ฅผ true๋ก ํ์ฌ ํด๋น ์์น์ ์ ๊ทผํ ์ ์๊ฒ ํ๋ค. ๊ทธ๋ฆฌ๊ณ deque์ ๋ค์ ๋ฃ์ง ์๋๋ค
ํธ์์ ์ ๋์ฐฉํ์ง ์์๋ค๋ฉด ๋ค์ deque์ ๋ฃ์ด์ค๋ค.
์๋ก์ด ์ฌ๋์ด ์ถ๋ฐํ๋ ์๊ฐ์ ๋ณด๊ณ find_basecampํจ์๋ฅผ ํตํด ๋ค์ด๊ฐ ๋ฒ ์ด์ค์บ ํ๋ฅผ ๊ณจ๋ผ์ค๋ค.
static int[] bfs(int n, int x, int y) {
ArrayDeque<int[]> q = new ArrayDeque<>();
q.offer(new int[] {x, y, -1, -1});
boolean[][] visited = new boolean[N][N];
visited[x][y] = true;
while(!q.isEmpty()) {
int[] now = q.poll();
if(now[0] == store[n][0] && now[1] == store[n][1]) {
return new int[] {n, now[2], now[3]};
}
for(int d=0; d<4; d++) {
int nx = now[0] + dx[d];
int ny = now[1] + dy[d];
if(nx < 0 || ny < 0 || nx >= N || ny >= N || visited[nx][ny] || v[nx][ny]) continue;
visited[nx][ny] = true;
if(now[2] == -1) {
q.offer(new int[] {nx, ny, nx, ny});
}
else
q.offer(new int[] {nx, ny, now[2], now[3]});
}
}
ํ์ฌ ์์น๋ฅผ ๋ฐ์์ ๋ค์ ์ด๋ํ ์์น๋ฅผ ๋ฐฐ์ด๋ก ๋ฐํํ๋ ํจ์์ด๋ค.
now[2], now[3]์ ์์ ์์น๋ฅผ ๋ฃ์ด์ฃผ์ด ๋ง์ง๋ง์๋ ํ์ธํ ์ ์๊ฒ ํ๋ค. → ์ด์ธ์๋ ํ์์ bfs์ ๋์ผํ๋ค.
4๋ฐฉ ํ์์ ํตํด์ ๋๋ฌํ ์ ์๋ ๊ณณ์ ์ ์ธํ๊ณ q์ ๊ฐฑ์ ๋ ๊ฐ์ ๋ฃ์ด ์ด๋ํ๋ค.
static int[] find_basecamp(int n, int x, int y) { // @param : ํธ์์ ์ ์์น - ๊ฐ์ฅ ๊ฐ๊น์ด base camp ์ฐพ๊ธฐ
ArrayDeque<int[]> q = new ArrayDeque<>();
q.offer(new int[] {x, y});
boolean[][] visited = new boolean[N][N];
visited[x][y] = true;
while(!q.isEmpty()) {
int[] now = q.poll();
if(map[now[0]][now[1]] == 1) {
v[now[0]][now[1]] = true;
return new int[] {n, now[0], now[1]};
}
for(int d=0; d<4; d++) {
int nx = now[0] + dx[d];
int ny = now[1] + dy[d];
if(nx < 0 || ny < 0 || nx >= N || ny >= N || visited[nx][ny] || v[nx][ny]) continue;
visited[nx][ny] = true;
q.offer(new int[] {nx, ny});
}
}
return null; // number+ํด๋น ๋ฒ ์ด์ค์บ ํ์ ์ขํ๋ฅผ ๋ฆฌํด!
}
ํธ์์ ์ ์์น๋ฅผ ๋ฐ์์ ๊ฐ์ฅ ๊ฐ๊น์ด ๋ฒ ์ด์ค์บ ํ์ ์ขํ๋ฅผ ๋ฐํํ๋ ํจ์์ด๋ค.
ํน๋ณํ ๊ฒ ์๋ bfs์ด๋ค. ์ด๋๋ ์ด๋ฏธ ๋ฐฉ๋ฌธํ ํธ์์ ๊ณผ ๋ฒ ์ด์ค์บ ํ๋ก ์ ๊ทผํ๋ ๊ฒ์ ๋ถ๊ฐ๋ฅ ํจ์ ์ ์ํ์