-
[ 백준 ][ java ][ 불! ][ 4179 ]코딩/알고리즘 2019. 3. 2. 21:40
음...불이 두개가 있길래 하나는 뭔가했는데 똑같은 문제인거같다.
이런 동시 다발? 성 문제를 풀자니 약간 헷갈리는데
다른 분 말을 빌리자면
상식적으로 생각하면됩니다.라는데...딱 맞는 말같다.
일단 불이랑 같은 시간대에 움직인다면 번진곳으론 이동 못 한다고 생각하면된다
즉 불을 먼저 이동 시키면 된다.
또 이 문제에서 중요한것은
외곽에 있으면 바로 탈출하는게 아니라
다 빠져 나가는 시간까지 포함하는것이다
나는 시간을 맨 처음에 1로 세팅하는 것으로 해놓고 외곽도착시 끝나는 것으로 했다
또 간과한 예외가 있었는데
만약 맨 처음에 바로 외곽에 있을 경우를 생각 못했는데
함수 첫번째에 분기점을 넣는 방식으로 해결했다
좀 더티한 해결방식이지만.....
혹시 이걸 보시는 분들은 이 코드를 너무 맹신하지마시길..
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.LinkedList; import java.util.Queue; import java.util.StringTokenizer; /* * 4 4 #### #JF# #..# #..# */ class point { int x; int y; point(int y, int x) { this.y = y; this.x = x; } } public class Main_backjoon_4179_불 { static int[] dx = { 1, -1, 0, 0 }; static int[] dy = { 0, 0, 1, -1 }; static char[][] MAP = new char[1001][1001]; static boolean[][] jVisited = new boolean[1001][1001]; static boolean[][] fireVisited = new boolean[1001][1001]; static Queue
fq = new LinkedList (); static Queue jq = new LinkedList (); static int N; static int M; static int TIME= 1; static boolean sucsess = false; static boolean move; static point fp; static point jp; 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()); for (int i = 0; i < N; i++) { st = new StringTokenizer(br.readLine(), ""); char[] chs = st.nextToken().toCharArray(); for (int j = 0; j < M; j++) { MAP[i][j] = chs[j]; if (chs[j] == 'F') { fq.add(new point(i, j)); fireVisited[i][j] = true; } if (chs[j] == 'J') { jq.add(new point(i, j)); jVisited[i][j] = true; } } } // printMap(); while (true) { if (!fq.isEmpty()) { int size = fq.size(); for (int i = 0; i < size; i++) { point fp = fq.poll(); fire(fp.y, fp.x); } } if (!jq.isEmpty()) { int size = jq.size(); for (int i = 0; i < size; i++) { point jp = jq.poll(); run(jp.y, jp.x); } } TIME += 1; //printMap(); //System.out.println(); if (sucsess) { System.out.println(TIME); break; } if (jq.isEmpty() && !move && !sucsess) { System.out.println("IMPOSSIBLE"); break; } } } public static void fire(int y, int x) { for (int d = 0; d < 4; d++) { int xx = x + dx[d]; int yy = y + dy[d]; if (xx >= 0 && xx < M && yy >= 0 && yy < N) { if (MAP[yy][xx] != '#' && !fireVisited[yy][xx]) { fq.add(new point(yy, xx)); MAP[yy][xx] = 'F'; fireVisited[yy][xx] = true; } } } } public static void run(int y, int x) { if ( y == 0 || x == 0 || y == N - 1 || x == M - 1) { // 탈출조건 sucsess = true; TIME -= 1; return; } move = false; for (int d = 0; d < 4; d++) { int xx = x + dx[d]; int yy = y + dy[d]; if (xx >= 0 && xx < M && yy >= 0 && yy < N) { if (MAP[yy][xx] != '#' && MAP[yy][xx] != 'F' && !jVisited[yy][xx]) { if ( yy == 0 || xx == 0 || yy == N - 1 || xx == M - 1) { // 탈출조건 sucsess = true; } move = true; jq.add(new point(yy, xx)); MAP[yy][xx] = 'J'; jVisited[yy][xx] = true; } } } } public static void printMap() { for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { System.out.print(MAP[i][j] + " "); } System.out.println(); } } } '코딩 > 알고리즘' 카테고리의 다른 글
[ 백준 ][ 자바 ][ 7569 ]토마토 (0) 2019.03.07 [ 백준 ][ 자바 ][ 2206 ] 벽 부수고 이동하기 (0) 2019.03.07 [2018 윈터코딩][ python ] 스킬트리 (0) 2019.03.02 [ 백준 ][ 자바 ][ 1260 ] DFS와 BFS (0) 2019.03.01 [ 백준 ][ 자바 ][ 10026 ][ 적록색약 ] (0) 2019.02.09