자바로 백준 1236 풀기

hong030·2023년 4월 4일
0
post-thumbnail
  • 브론즈 1단계 문제

풀이)

모든 행과 열에 한 명 이상의 경비원이 있게끔 배치하려 할 때, 필요한 최소의 경비원 수는?

입력받은 배열에 대해, 경비원이 없는 세로줄 수와 경비원이 없는 가로줄 수 중 큰 값을 구하면 된다.

내 코드)

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

public class Backjoon1236 {
	public static void main(String[]args) throws IOException{
		
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));		
		StringTokenizer st = new StringTokenizer(bf.readLine());
		
		int N = Integer.parseInt(st.nextToken());
		int M = Integer.parseInt(st.nextToken());
		
		int nCount = M; //경비원이 없는 세로줄
		int mCount = N; //경비원이 없는 가로줄
		
		String input[] = new String[N];
		for(int i=0;i<N;i++) {
			String str = bf.readLine();
			input[i] = str;
			if(str.contains("X")) {
				mCount--;
			}
		}
		
		char[][] arr = new char[N][M];
		for(int i=0;i<N;i++) {
			arr[i] = input[i].toCharArray();
		}
		
		for(int i=0;i<M;i++) {
			for(int j=0;j<N;j++) {
				if(arr[j][i] == 'X') {
					nCount--;
					break;
				}
			}
		}

		int ans = nCount>mCount?nCount:mCount;
		System.out.println(ans);
	}
}

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

0개의 댓글