ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [ 백준 ][ 자바 ][ 7569 ]토마토
    코딩/알고리즘 2019. 3. 7. 23:46

    와....

    다 풀어놓고서 진짜 엄청난 삽질만 헀다

    ....

    코드 위치를 잘못해놓고 3차원 배열이라는 약간의 특수성떄문에

    생각 못하고 계속 디버깅하면서

    말도 안돼만 100번 하던 찰나에...발견했다...

    와...ㅋㅋㅋ내시간..ㅠㅠㅠㅠㅠㅠ

    굉장히 당혹스럽다

    ,...

    음 이문제는 3차원 배열이란 거 의외엔 다른 bfs랑 똑같이 풀면 된다

    상하좌우 그리고 위,아래라는 것만 생각하면 된다

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.LinkedList; import java.util.Queue; import java.util.StringTokenizer; //5 3 1 //0 -1 0 0 0 //-1 -1 0 1 1 //0 0 0 1 1 class tPoint{ int x; int y; int h; tPoint(int h ,int y, int x ){ this.h = h; this.y = y; this.x = x; } } public class Main_backjoon_7569_토마토 { static Queue

    q = new LinkedList(); static int[] dy = { 0, 0, 1, -1, 0, 0}; static int[] dx = { 1, -1, 0, 0, 0, 0}; static int[] dh = { 0, 0, 0, 0, 1, -1}; static int MAP[][][] = new int[101][101][101]; static boolean visited[][][] = new boolean[101][101][101]; static int M; //가로 static int N; //세로 static int H; //높이 static int CNT; //안익은 토마토 개수 static int DAY; //흐른 시간 public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine(), " "); M = Integer.parseInt(st.nextToken()); N = Integer.parseInt(st.nextToken()); H = Integer.parseInt(st.nextToken()); for(int k = 0; k < H; k++) { for(int i = 0 ; i < N; i++) { st = new StringTokenizer(br.readLine(), " "); int idx = 0; while(st.hasMoreTokens()) { MAP[k][i][idx]= Integer.parseInt(st.nextToken()); if( MAP[k][i][idx] == 1) { q.add(new tPoint(k, i,idx)); visited[k][i][idx] = true; } if( MAP[k][i][idx] == 0 ) { CNT+= 1; } idx +=1; } } } //printMap(); tomatoBfs(); } public static void printMap() { for(int k = 0 ; k < H; k++) { for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { System.out.print(MAP[k][i][j] + " "); } System.out.println(); } } } public static void printVisitedMap() { for(int k = 0 ; k < H; k++) { for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { System.out.print(visited[k][i][j] ? 1 + " " : 0 + " "); } System.out.println(); } } } public static void tomatoBfs() { //System.out.println(q.size()); int ripe = 0 ; boolean move = false; while(!q.isEmpty()) { //printVisitedMap(); //printMap(); //System.out.println("----------------------"); int qsize = q.size(); for(int s = 0 ; s < qsize; s++) { tPoint tp = q.poll(); for(int i = 0 ; i < 6; i ++) { int yy = tp.y + dy[i]; int xx = tp.x + dx[i]; int hh = tp.h + dh[i]; if( xx >= 0 && xx < M && yy >= 0 && yy < N && hh >= 0 && hh < H) { if ( MAP[hh][yy][xx] != -1 && !visited[hh][yy][xx]) { q.add(new tPoint(hh, yy, xx )); ripe += 1; visited[hh][yy][xx] = true; move = true; } } } } if( move ) DAY += 1; if( ripe == CNT ) break; } System.out.println( ripe == CNT ? DAY : -1); //System.out.println(ripe + " " + CNT); //익힌 숫자와 토마토 숫자가 같으면 } }


    댓글

Designed by Tistory.