백준 1652번 누울 자리를 찾아라 JAVA

YB·2025년 2월 27일

링크텍스트

설명

dot이 2이상인 경우 row와 column을 각각 conut 해주었다.
시간복잡도: O(N²), 공간복잡도: O(N²)

코드

import java.util.*;
import java.io.*;

class Main {	
		static int n;
		static char [][] arr;
		static int row = 0;
		static int column = 0;
	public static void main (String[] args) throws IOException {
	    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		n = Integer.parseInt(br.readLine());
		arr = new char[n][n];

		for(int i=0;i<n;i++){
			arr[i] = br.readLine().toCharArray();
		}

		rowCount();
		columnCount();

		System.out.println(row + " " + column);
		
	}

	public static void rowCount(){
		for(int i=0;i<n;i++){
			int dot = 0;
			for(int j=0;j<n;j++){
				if(arr[i][j]=='.'){
					dot++;
				}else if (arr[i][j]=='X'){
					if(dot>=2){
						row++;
					}
					dot=0;
				}
				if(j==n-1 && dot>=2) row++;
			}
		}
	}
	public static void columnCount(){
		for(int i=0;i<n;i++){
			int dot = 0;
			for(int j=0;j<n;j++){
				if(arr[j][i]=='.'){
					dot++;
				}else if (arr[j][i]=='X'){
					if(dot>=2){
						column++;
					}
					dot=0;
				}
				if(j==n-1 && dot>=2) column++;
			}
		}
	}
}

profile
안녕하세요

0개의 댓글