본문 바로가기
PS 문제 풀이/HackerRank

[HackerRank] Cavity Map

by 까망 하르방 2021. 2. 18.
반응형

출처: https://www.hackerrank.com/challenges/cavity-map/problem

가장자리를 제외한 cell 영역에서 cavity인지를 판단하는 문제이다.

'Cavity 여부'는 해당 지점의 값이 상하좌우 값 보다 높을 때이다.

해당 지점을 [X]로 해서 표시해서 출력한다.

 

문제 Test Case 중 input data 입력 방식이 Sample Case와 달리 공백으로 주어져서

제출코드에서는 공백을 없애는 과정을 추가함.

 


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
 
public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
         
        int n = Integer.parseInt(sc.next());
        sc.nextLine();
         
        int[][] arr = new int[n][n];
        // 공백이 포함된 입력인지 체크
        boolean isBlank = false;
         
        for(int i=0; i<n; i++) {
            String str = sc.nextLine();
             
            int preLen = str.length();
            // 공백제거
            str = str.replaceAll(" ", "");
            int postLen = str.length();
             
            // 공백제거 후 실제 길이가 변화되었는지 확인
            if(preLen != postLen) isBlank = true;
             
            for(int j=0 ; j<n ; j++) {
                arr[i][j] = Integer.parseInt(str.substring(j, j+1));
            }
        }
         
        // 상하좌우
        int[] dx = {-1, 1, 0, 0};
        int[] dy = {0, 0, -1, 1};
        List<Cavity> cavities = new ArrayList<>();
         
        for(int x=0; x<n; x++) {
            for(int y=0; y<n; y++) {
                int isCavity = 0;
                for(int i=0; i<4; i++) {
                    int tx = x + dx[i];
                    int ty = y + dy[i];
                     
                    // 가장자리쪽은 대상이 아니다. (배열 범위 초과 주의)
                    if(tx < 0 || ty < 0 || tx >= n || ty >= n) {
                        break;
                    }
                     
                    if(arr[x][y] > arr[tx][ty]) {
                        isCavity++;
                    }
                }
                 
                // 상하좌우가 보다 높은 cell의 경우 cavity이다.
                if (isCavity == 4) {
                    // 다른 cell도 cavity인지 확인해야 하므로 우선 따로 보관
                    cavities.add(new Cavity(x, y));
                }
            }
        }
         
        // 위에서 모든 조사가 끝났다면 영역 변경
        while(cavities.size() > 0) {
            Cavity c = cavities.remove(0);
            arr[c.x][c.y] = -1;
        }
         
        for(int x=0; x<n; x++) {
            for(int y=0; y<n; y++) {
                if(arr[x][y] == -1) {
                    if(isBlank) {
                        System.out.print("X ");    
                    }else {
                        System.out.print("X");
                    }
                     
                }else {
                    if(isBlank) {
                        System.out.print(arr[x][y] +" ");    
                    }else {
                        System.out.print(arr[x][y]);
                    }
                }
            }
            System.out.println();
        }
    }
}
 
class Cavity{
    int x, y;
    public Cavity(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

 

반응형

'PS 문제 풀이 > HackerRank' 카테고리의 다른 글

[HackerRank] The Bomberman Game  (0) 2021.02.18
[HackerRank] Strange Counter  (0) 2021.02.18
[HackerRank] 3D Surface Area  (0) 2021.02.18
[HackerRank] Repeated String (Java)  (0) 2021.02.14
[HackerRank] Almost Sorted (Java)  (0) 2021.02.14

댓글