본문 바로가기

알고리즘

백준 17144번 - 미세먼지 안녕!(런타임 에러)

시뮬레이션 문제입니다.

런타임 에러가 나와서 아직 방법을 못찾았습니다.

혹시 스캐너로 입력을 받는 것이 문제일까 싶어서 버퍼리더로 받았는데도 동일한 문제가 나오는 걸 보니

공기청정기에서 배열 접근하는 방법에서 문제가 있는게 아닐까 의심되네요 ㅠㅠ

시간 날 때 다시 한번 풀어봐야겠습니다.

package com.company;

import java.util.Scanner;

public class boj17144 {

    static int N, M, T;
    static int[][] map, temp, cnt, airMap;
    static int[] dx = {-1, 1, 0, 0};
    static int[] dy = {0, 0, -1, 1};
    static int airX1=0;
    static int airX2=0;
    static int result;

    static void initMap(){


        for(int i=0; i<N; i++){
            for(int j=0; j<M; j++){
                temp[i][j] = 0;
                cnt[i][j] = 0;
                if(map[i][j] == -1){
                    temp[i][j] = -1;
                    cnt[i][j] = -1;
                    if(airX1 == 0){
                        airX1 = i;
                    } else {
                        airX2 = i;
                    }
                }
            }
        }

    }

    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);
        N = sc.nextInt();
        M = sc.nextInt();
        T = sc.nextInt();

        map = new int[N][M];
        temp = new int[N][M];
        cnt = new int[N][M];
        airMap = new int[N][M];


        for(int i=0; i<N; i++){
            for(int j=0; j<M; j++){
                map[i][j] = sc.nextInt();
                temp[i][j] = 0;
                cnt[i][j] = 0;
                if(map[i][j] == -1){
                    temp[i][j] = -1;
                    cnt[i][j] = -1;
                    if(airX1 == 0){
                        airX1 = i;
                    } else {
                        airX2 = i;
                    }
                }
            }
        }


        for(int i=0; i<T; i++){
            initMap();
            for(int j=0; j<N*M; j++){

                int x = j/M;
                int y= j%M;
                int value;
                int count=0;
                // -1도 건너 뛰어야함 나중에 추가 해주기

                //상하좌우 계산식
                value = map[x][y]/5;

                for(int k=0; k<4; k++){
                    int nextX = x+dx[k];
                    int nextY = y+dy[k];

                    // -1인것도 제외 식 추가해 줘야함
                    if(nextX >= 0 && nextY >= 0 && nextX < N && nextY < M){
                        if(temp[nextX][nextY] == -1) continue;
                        temp[nextX][nextY] += value;
                        count++;
                    }

                }
                if(cnt[x][y] != -1){
                    cnt[x][y] += count;
                }


            }

            for(int a=0; a<N*M; a++){
                int x = a/M;
                int y = a%M;
                if(map[x][y] == -1) continue;
                map[x][y] = map[x][y] - (map[x][y]/5)*cnt[x][y] + temp[x][y];
            }

            // map 복사
            for(int d=0; d<N; d++){
                for(int f=0; f<M; f++){
                    airMap[d][f] = map[d][f];
                }
            }

//            // map 복사
//            for(int d=0; d<N; d++){
//                for(int f=0; f<M; f++){
//                    System.out.print(" "+map[d][f]);
//                }
//                System.out.println();
//            }
//            System.out.println();
//            System.out.println();

            for(int a=0; a<N; a++){
                for(int b=0; b<M; b++){
                    if(a <= airX1){
                        if(b == 0){ // x축이 0일 떄
                            if(a==0 || a == airX1) continue;
                            map[a][b] = airMap[a-1][b];
                        } else if(a == 0){ // y축이 0일 때
                            if(b == M-1){
                                map[a][b] = airMap[a+1][b];
                            }else{
                                map[a][b] = airMap[a][b+1];
                            }
                        } else if(b == M-1) { // x축이 M일 때
                            if(a== airX1){
                                map[a][b] = airMap[a][b-1];
                            } else{
                                map[a][b] = airMap[a+1][b];
                            }

                        } else if(a == airX1){ // y축이 에어컨의 y축과 같을 떄
                            if(b == 1) // 바로 에어컨 옆
                                map[a][b] = 0;
                            else {
                                map[a][b] = airMap[a][b-1];
                            }
                        } else {
                            continue;
                        }
                    } else {
                        if(airX2 == a){ // y축이랑 에어컨이랑 같을 떄
                            if(b == 0) continue;
                            if(b==1){
                                map[a][b] = 0;
                            } else {
                                map[a][b] = airMap[a][b-1];
                            }
                        } else if(b == M-1){ // x축이 끝일떄
                            if(a == airX2) continue;
                            map[a][b] = airMap[a-1][b];
                        } else if(a == N-1){ // y축이 끝일 때
                            if(b==M-1) continue;
                            map[a][b] = airMap[a][b+1];
                        } else if(b == 0){ // x축이 0일 때
                            map[a][b] = airMap[a+1][b];
                        }


                    }


                }
            }

//            // map 복사
//            for(int d=0; d<N; d++){
//                for(int f=0; f<M; f++){
//                    System.out.print(" "+map[d][f]);
//                }
//                System.out.println();
//            }
//
//            System.out.println();
//            System.out.println();
//            System.out.println();

            result = 0;

            for(int a=0; a<N; a++){
                for(int b=0; b<M; b++){
                    result += map[a][b];
                }
            }



        }



        System.out.println(result+2);


    }

}

'알고리즘' 카테고리의 다른 글

백준 - 14502 연구소  (0) 2020.04.26
백준 14889 - 스마트와 링크  (0) 2020.04.26
프로그래머스 - 구명보트  (0) 2020.04.17
프로그래머스 - 큰 수 만들기  (0) 2020.04.17
프로그래머스 - 라면공장  (0) 2020.04.12