개발자 톡
연습문제 톡
GINI야 도와줘
GINI야 부탁해 5번..ㅠㅠㅠ
- 등록일
- 2021-10-31 18:18:44
- 조회수
- 635
- 작성자
- zaza1994
안녕하세요.
매번 GINI 5번에서만 오답이 나옵니다.
이유를 모르겠습니다.
도움 요청드립니다.
import java.util.*;
import java.io.*;
public class Main {
static int R, C;
static boolean[][] rainCAN, isVisited;
static LinkedList rain;
static int[][] map;
static Pair src, des;
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
R = Integer.parseInt(st.nextToken());
C = Integer.parseInt(st.nextToken());
rainCAN = new boolean[R][C];
rain = new LinkedList();
map = new int[R][C];
isVisited = new boolean[R][C];
for(int r = 0; r < R; r++) {
String str = br.readLine();
for(int c = 0; c < C; c++) {
char ch = str.charAt(c);
if(ch == 'X') {
rainCAN[r][c] = false;
map[r][c] = 4;
} else if(ch == '.') {
rainCAN[r][c] = true;
} else if(ch == 'H') {
des = new Pair(r,c);
rainCAN[r][c] = false;
map[r][c] = 2;
} else if(ch == 'W'){
src = new Pair(r,c,0);
rainCAN[r][c] = false;
map[r][c] = 3;
} else {
rainCAN[r][c] = true;
map[r][c] = 1;
rain.add(new Pair(r,c));
}
}
}
solution();
}
static int[] dr = {-1, 0, 1, 0};
static int[] dc = {0, 1, 0, -1};
static void solution() {
int ans = recur();
if(ans == -1) System.out.println("FAIL");
else System.out.println(ans);
}
static int recur() {
Queue q = new LinkedList();
q.add(src);
isVisited[src.r][src.c] = true;
int update_cost = 0;
while(!q.isEmpty()) {
Pair p = q.poll();
if(p.cost != update_cost) {
rainUpdate();
update_cost = p.cost;
}
for(int i = 0; i < 4; i++) {
int nr = p.r + dr[i];
int nc = p.c + dc[i];
if(nr == des.r && nc == des.c) return p.cost+1;
if(nr > -1 && nr < R && nc > -1 && nc < C && (map[nr][nc] == 0 || map[nr][nc] == 2) && !isVisited[nr][nc]) {
q.add(new Pair(nr, nc, p.cost + 1));
isVisited[nr][nc] = true;
}
}
}
return -1;
}
static void rainUpdate() {
if(rain.isEmpty()) return;
LinkedList new_rain = new LinkedList();
for(Pair p : rain) {
new_rain.add(p);
for(int i = 0; i < 4; i++) {
int nr = p.r + dr[i];
int nc = p.c + dc[i];
if(nr > -1 && nr < R && nc > -1 && nc < C && rainCAN[nr][nc] && map[nr][nc] != 1) {
new_rain.add(new Pair(nr,nc));
map[nr][nc] = 1;
}
}
}
rain = new_rain;
}
static class Pair{
int r, c, cost;
Pair(int r, int c) { this.r = r; this.c = c; this.cost = 0;}
Pair(int r, int c, int cost) { this.r = r; this.c = c; this.cost = cost;}
}
}
#gini야_도와줘
#gini