728x90
300x250
https://www.acmicpc.net/problem/1236
1236번: 성 지키기
첫째 줄에 성의 세로 크기 N과 가로 크기 M이 주어진다. N과 M은 50보다 작거나 같은 자연수이다. 둘째 줄부터 N개의 줄에는 성의 상태가 주어진다. 성의 상태는 .은 빈칸, X는 경비원이 있는 칸이다
www.acmicpc.net
경비원이 없는 행,열의 갯수를 구한 뒤 더 많은 쪽을 제출하면됨.
대각선으로 경비원을 배치하면 행/열모두를 잡을 수 있음
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
//System.setIn(new FileInputStream("Test.txt"));
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int M = sc.nextInt();
char map[][] = new char[N][M];
for (int i = 0; i < N; i++) {
map[i] = sc.next().toCharArray();
}
//경비원이 있는 행,열 체크
// boolean visitRow[] = new boolean[N];
int cntVisitRow = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if(map[i][j]=='X') {
// visitRow[i] = true;
cntVisitRow++;
break;
}
}
}
// boolean visitCol[] = new boolean[N];
int cntVisitCol = 0;
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
if(map[j][i]=='X') {
// visitCol[i] = true;
cntVisitCol++;
break;
}
}
}
//아무도 지나가지 않은 행,열 계산
int noRow = N-cntVisitRow;
int noCol = M-cntVisitCol;
System.out.println(Math.max(noRow, noCol));
}
}
728x90
'알고리즘 > 그 외' 카테고리의 다른 글
백준 10989번: 수 정렬하기3 [배열][정렬]- Java (0) | 2023.04.11 |
---|---|
백준 10431번: 줄세우기 [구현][배열][시뮬레이션] - Java (0) | 2023.04.11 |
[알고리즘] 시간복잡도 란? (0) | 2023.04.10 |
프로그래머스 - 행렬 테두리 회전하기 [2021 Dev-Matching: 웹 백엔드 개발자(상반기)] (0) | 2021.08.13 |
프로그래머스 - 키패드 누르기 [2020 카카오 인턴십] - Java (0) | 2021.08.12 |