백준 1652

hong030·2023년 2월 17일
0

*실버 5단계 문제

풀이)
input[1][1]이 "." 일 때 input[2][1]도 "."이면 세로로 누울 수 있는 자리가 하나 있는 것이고, input[1][2]가 "."이면 가로로 누울 수 있는 자리가 하나 있는 것이다.
중복으로 세는 경우를 없애기 위해
1. 연속되게 "."이 있으면서 다음 칸이 "X"인 경우
2. 연속되게 "."이 있으면서 다음칸이 벽인 경우(input[i][j]에서 i, j가 N을 넘은 경우) 이 두 가지에서 횟수를 센다.

내 코드)

import java.io.*;

public class Backjoon1652 {
	public static void main(String[]args) throws IOException {
		
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		int N=Integer.parseInt(bf.readLine());
		char input[][] = new char[N][N];
		for(int i=0;i<N;i++) {
			String temp[] = bf.readLine().split("");
            for(int j = 0; j < N; j++)
                input[i][j] = temp[j].charAt(0);
		}
		
		int R=0, C=0;
		
        for(int i = 0; i < N; i++) {
            for (int j = 0 ; j < N; j++) {
                if(i + 1 < N) {
                    if (input[i][j] == '.' && input[i+1][j] == '.' && (i + 2 >= N || input[i+2][j] == 'X')) {
                        C += 1;
                    }
                }
                if(j + 1 < N) {
                    if (input[i][j] == '.' && input[i][j + 1] == '.' && (j + 2 >= N || input[i][j + 2] == 'X')) {
                        R += 1;
                    }
                }
            }
        }
        
        System.out.println(R + " " + C);
		
	}
}

profile
자바 주력, 프론트 공부 중인 초보 개발자. / https://github.com/hongjaewonP

0개의 댓글