[Algorithm/Java] 백준 1652번 - 누울 자리를 찾아라
https://www.acmicpc.net/problem/1652
🔍 문제 풀이
문제 도식화
💻 코드
전체 코드
import java.io.*;
import java.util.*;
public class Main {
static int n;
static char[][] arr;
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++){
String line = br.readLine();
for(int j=0; j<n; j++){
arr[i][j] = line.charAt(j);
}
}
int[] ans = solve();
for(int val:ans){
System.out.print(val + " ");
}
}
static int[] solve(){
int cntRow = 0, cntCol = 0;
// 가로 확인
for(int i=0; i<n; i++) {
int cnt = 0;
for (int j = 0; j < n; j++) {
if (arr[i][j] == '.') {
cnt ++;
}
if(arr[i][j] == 'X' || j == n-1) {
if (cnt >= 2) {
cntRow++;
}
cnt = 0;
}
}
}
// 세로 확인
for(int i=0; i<n; i++) {
int cnt = 0;
for (int j = 0; j < n; j++) {
if (arr[j][i] == '.') {
cnt ++;
}
if(arr[j][i] == 'X' || j == n-1) {
if (cnt >= 2) {
cntCol++;
}
cnt = 0;
}
}
}
return new int[]{cntRow, cntCol};
}
}
댓글남기기