코딩해요/JAVA

[프로그래머스] PCCE 기출문제 9번/이웃한 칸

yenas0 2024. 7. 30. 22:02
반응형

 

 

class Solution {
    public int solution(String[][] board, int h, int w) {
        int n = board.length;
        int count = 0;
        int dh[] = {0, 1, -1, 0};
        int dw[] = {1, 0, 0, -1};
        
        for(int i=0; i<4; i++){
            int h_check = h + dh[i];
            int w_check = w + dw[i];
            
            if(h_check >= 0 && h_check < n && w_check >= 0 && w_check < n){
                if(board[h][w].equals(board[h_check][w_check]))
                    count++;
            }
        }
        return count;
    }
}
반응형