알고리즘

[백준, 자바, 1388번] 바닥 장식

hminor 2024. 10. 11. 14:47
반응형

풀이

  • 따로 어려운 부분은 없었고, 단순히 문제 이해가 처음에는 안되었는데
  • 그냥 "단순히 선이 이어져 있으면, 그게 하나의 타일이다" 라고 설명했으면
  • 더 쉽게 이해했을듯 하다... ㅋㅋ
  • 무튼 그래서 mtx에 대해 기본 타입에선 "-" 만 체크하고
  • 이후엔 범위를 변경 후 "ㅣ" 만 체크해서 카운팅하여 해결.

 

import java.io.*;
import java.util.*;
public class _1388 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int result = 0;
        String now;
        int[] NM = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        String[][] mtx = new String[NM[0]][NM[1]];

        for (int i=0; i<NM[0]; i++) {
            String inp = br.readLine();
            for (int j=0; j<NM[1]; j++) mtx[i][j] = String.valueOf(inp.charAt(j));
        }

        // 가로 세로 각각 찾기(정사각형이 아니어서)
        for (int i=0; i<NM[0]; i++) {
            now = "";
            for (int j=0; j<NM[1]; j++) {
                if (mtx[i][j].equals("-") && !now.equals("-")) {
                    now = "-";
                    result++;
                }
                else if (!mtx[i][j].equals("-")) now = mtx[i][j];
            }
        }
        for (int i=0; i<NM[1]; i++) {
            now = "";
            for (int j=0; j<NM[0]; j++) {
                if (mtx[j][i].equals("|") && !now.equals("|")) {
                    now = "|";
                    result++;
                }
                else if (!mtx[j][i].equals("|")) now = mtx[j][i];
            }
        }
        System.out.println(result);

    }
}