<문제 링크>


https://www.acmicpc.net/problem/5427  



<내 소스>


iimport java.io.BufferedReader;

import java.io.InputStreamReader;

import java.util.LinkedList;

import java.util.Queue;


class Main {


static int t,n,m,ans, mx[] = {-1,0,1,0}, my[] = {0,1,0,-1};

static char[][] map;

static boolean escape;

static Queue<Position> start = new LinkedList<>();

static Queue<Position> fire = new LinkedList<>();

static Queue<Position> tmp = new LinkedList<>();


public static void main(String[] args) throws Exception {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    t = Integer.parseInt(br.readLine());

    for (int i=0; i<t; i++) {

    String[] b = br.readLine().split(" ");

    n = Integer.parseInt(b[0]);

    m = Integer.parseInt(b[1]);

    map = new char[m][n];

    for (int j=0; j<m; j++) {

    String a = br.readLine();

    for (int k=0; k<n; k++) {

    map[j][k] = a.charAt(k);

    if (map[j][k] == '@')

    start.add(new Position(j,k,1)); // 상근이 넣어준다

    if (map[j][k] == '*')

    fire.add(new Position(j,k)); // 불들을 넣어준다

    }

    } //입력

    a:while(!start.isEmpty()) { // 상근이가 이동할 수 없을 때까지

    while(!fire.isEmpty()) {

    Position f = fire.poll();

    for (int j=0; j<4; j++) {

    int x = f.x + mx[j];

    int y = f.y + my[j];

    if (0<=x && x<m && 0<=y && y<n && map[x][y] == '.') {

    map[x][y] = '*';

    tmp.add(new Position(x,y));

    }

    }

    } // 불을 먼저 퍼뜨린다.

    fire.addAll(tmp);

    tmp.clear();

   

    while(!start.isEmpty()) {

    Position s = start.poll();

    if ((s.x==0 && 0<=s.y && s.y<n) || (s.y==n-1 && 0<=s.x && s.x<m) || (s.x==m-1 && 0<=s.y && s.y<n)

    || (s.y==0 && 0<=s.x && s.x<m) ) {// 탈출 성공시

escape = true; // 탈출 성공

ans = s.edge; // 상근이 걸음수 대입

fire.clear();

start.clear();

tmp.clear(); // 다음 테스트를 위해 큐 비움

break a;

    }

    for (int j=0; j<4; j++) {

    int x = s.x + mx[j];

    int y = s.y + my[j];

    if (0<=x && x<m && 0<=y && y<n && map[x][y] == '.') {

    map[x][y] = 's';

    tmp.add(new Position(x,y,s.edge+1));

    }

    }

    } //상근이 이동

    start.addAll(tmp);

    tmp.clear();

    }

    if (escape)

    System.out.println(ans);

    else

    System.out.println("IMPOSSIBLE");

    escape = false;

    }

}

}


class Position {

int x,y,edge;

Position (int x, int y) {

this.x = x;

this.y = y;

}

Position (int x, int y, int edge) {

this.x = x;

this.y = y;

this.edge = edge;

}

}


1

1 1

@

같이 애초에 상근이가 탈출 위치에 있을 때도 1 뜨고 다른 케이스도 다 맞게 뜨는데

50%에서 자꾸 틀린다... 왜 그런 걸까?