[프로그래머스] 프렌즈 4블록

2020. 5. 3. 15:59프로그래머스/카카오

import java.util.*;

class Solution {
    List<String> deleteIndex = new ArrayList<>();

  public int solution(int m, int n, String[] board) {
      int answer = 0;
      
      String[] copyBoard = new String[board.length+2];
      
      initCopyBoard(copyBoard,board);
      
      
      while(true){
      boolean flag = false;
          
      for(int i=1; i<board.length; i++){ 
          for(int j=1; j<board[i].length(); j++){   
                if(targetBlock(board,i,j)){
                    answer += countAndChangeBlock(copyBoard,i+1,j+1);
                    flag = true;
                }
            }
        }
          
      if(flag == false){
          break;
      }
      
      board = updateBoard(copyBoard,board);
      initCopyBoard(copyBoard,board);
      }
    
    
      return answer;
  }
    
  private String[] initCopyBoard(String[] copyBoard,String[] board){
      String space = "0";
      
      for(int i=0; i<board[0].length()+1; i++){
          space +="0";
      }
      for(int i=1; i<copyBoard.length-1; i++){
          copyBoard[i] = "0"+ board[i-1] + "0";
      }
      copyBoard[0] = space;
      copyBoard[copyBoard.length-1] = space;
      return copyBoard;
  }
    
  private boolean targetBlock(String[] board, int i, int j){
      if(board[i].charAt(j) !='0'
         &&board[i].charAt(j) == board[i].charAt(j-1)
         && board[i].charAt(j) == board[i-1].charAt(j)
         && board[i].charAt(j) == board[i-1].charAt(j-1)){
          deleteIndex.add(Integer.toString(i)+" "+ Integer.toString(j));
      return true;    
      }
      return false;
    }
  
   private int countAndChangeBlock(String[] copyBoard, int i, int j) {
       StringBuilder sb1 = new StringBuilder(copyBoard[i-1]);
       StringBuilder sb2 = new StringBuilder(copyBoard[i]);
       
       int count = 0;
       
       if(copyBoard[i].charAt(j)!='0'){
           count++;
           sb2.setCharAt(j,'0');
           copyBoard[i] = sb2.toString();
       }
       if(copyBoard[i].charAt(j-1)!='0'){
           count++;
           sb2.setCharAt(j-1,'0');
           copyBoard[i] = sb2.toString();
       }
       if(copyBoard[i-1].charAt(j)!='0'){
           count++;
           sb1.setCharAt(j,'0');
           copyBoard[i-1] = sb1.toString();
       }
       if(copyBoard[i-1].charAt(j-1)!='0'){
           count++;
           sb1.setCharAt(j-1,'0');
           copyBoard[i-1] = sb1.toString();
       }
       
       return count;
   }
    
   private String[] updateBoard(String[] copyBoard, String[] board){
       for(int i=0; i<board.length; i++){
           board[i] = copyBoard[i+1].substring(1,copyBoard[i].length());
       }
       
       while(true){
          boolean flag = false; 
            for(int i=0; i<board.length-1; i++){
           StringBuilder sb1 = new StringBuilder(board[i]);
           StringBuilder sb2 = new StringBuilder(board[i+1]);
           for(int j=0; j<board[i].length(); j++){
            if(board[i+1].charAt(j)=='0' && board[i].charAt(j)!='0'){
                sb2.setCharAt(j,board[i].charAt(j));
                sb1.setCharAt(j,'0');
                board[i] = sb1.toString();
                board[i+1] = sb2.toString();
                flag=true;
            }
           }
       }
           if(flag == false){
               break;
           }
       }
      
       
       return board;
   }
     
}

무식하게 내용을 그대로 구현에 옮겼다.

1. 4개를 삭제할 수 있을때마다 복사한 보드를 통해 갯수를 카운트한다.

2. 업데이트된 카피보드를 보드로 옮긴다.

3. 바뀐 보드를 떨어뜨리는 작업을한다.

4. 전혀 변화가 없을때까지 반복한다.