
입력받을때 공백을 넣어서 받는 걸로 착각해서 오래걸린 문제.. 아직 자바가 익숙하지 않아서 힘들다.
문제 자체는 어렵지 않다! 1. 가로세로에 병사가 배치되어있는지 확인하고 2. 병사가 하나도 배치되지 않은 행(또는 열)의 수를 세어서 3. 가로 세로 중 더 큰 값을 출력하면 된다.
예시를 보면 ..


(4, 4) 에 병사가 하나도 없을때는 모든 행에 병사가 없으므로
열 배열: [0]0 [1]0 [2]0 [3]0 -> 0의 개수(병사가 없는 열의 개수): 4
행 배열: [0]0 [1]0 [2]0 [3]0 -> 0의 개수(병사가 없는 행의 개수): 4
둘 중 더 큰 값을 출력, 여기서는 같으므로 4가 정답이다.
(3,5)인 경우에는..
열 배열: [0]1 [1]1 [2]1 -> 0의 개수: 0
행 배열: [0]1 [1]1 [2]1 [3]1 [4]1 -> 0의 개수: 0
0 출력
(5, 8)
열 배열: [0]1 [1]0 [2]1 [3]0 [4]0 -> 0의 개수: 3
행 배열: [0]1 [1]1 [2]0 [3]1 [4]1 [5]1 [6]1 [7]1 -> 0의 개수: 1
3 출력
조금만 생각하면 답이 쉽게 나온다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
public class BOJ1236 {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int row, col, i, j;
String [][] arr;
String str;
str = bf.readLine(); // 한 줄 읽기
StringTokenizer st = new StringTokenizer(str); // 공백으로 분리됨
row = Integer.parseInt(st.nextToken());
col = Integer.parseInt(st.nextToken());
arr = new String[row][col];
// 입력 받기
for(i=0;i<row;i++){
str = bf.readLine(); // 한 줄 읽기
for (j=0;j<col;j++){
arr[i][j] = str.charAt(j)+""; // char -> String 변환
}
}
// 행&열 배열 만들고 병사 유무 적기
int [] countRow = new int[row];
int [] countCol = new int[col];
// 행 병사 유무 확인
for(i=0;i<row;i++){
for(j=0;j<col;j++){
if(arr[i][j].equals("X")) {
countRow[i] = 1;
break;
}
countRow[i] = 0;
}
}
// 열 병사 유무 확인
for(j=0;j<col;j++){
for(i=0;i<row;i++){
if(arr[i][j].equals("X")) {
countCol[j] = 1;
break;
}
countCol[j] = 0;
}
}
// 0 개수 세기
int countZeroRow = 0;
int countZeroCol = 0;
for(i=0;i<row;i++)
if(countRow[i] == 0)
countZeroRow++;
for(i=0;i<col;i++)
if(countCol[i] == 0)
countZeroCol++;
// 두어야 할 병사의 수
System.out.println(Math.max(countZeroRow, countZeroCol));
// 출력
// for(i=0;i<row;i++){
// for (j = 0; j < col; j++)
// System.out.print(arr[i][j]);
// System.out.println();
// }
//
// for(i=0;i<row;i++)
// System.out.print(countRow[i]);
// System.out.println();
// for(i=0;i<col;i++)
// System.out.print(countCol[i]);
//
// System.out.println();
// System.out.println("행 0의 개수: " + countZeroRow);
// System.out.println("열 0의 개수: " + countZeroCol);
}
}
'코딩가딩가' 카테고리의 다른 글
| [JAVA]BOJ 10989, 수 정렬하기 3 (0) | 2024.05.22 |
|---|---|
| [JAVA]BOJ 10431, 줄세우기 (0) | 2024.05.22 |
| [JAVA]배열 (1) | 2024.03.23 |
| [JAVA]BOJ 10158 (1) | 2024.01.16 |
| [JAVA]BOJ 13223 (0) | 2024.01.10 |