profile image

L o a d i n g . . .

1
import java.util.Scanner;

public class solution_d2_1979 {

public static void main(String[] args) {
// 1979 어디에 단어가 들갈수있을까?

//test case수 입력
Scanner sc = new Scanner(System.in);
int TestCase = sc.nextInt();

//test case수 만큼 과정반복
for (int i = 0; i < TestCase; i++) {

//각 크기 입력
int N = sc.nextInt();
int K = sc.nextInt();

//입력한 크기의 배열 선언 N x N
int[][] mainArray = new int[N][N];

//선언한 배열에 원소 직접 입력
for (int j = 0; j < N; j++) {
for (int k = 0; k < N; k++) {
mainArray[j][k] = sc.nextInt();
}
}

int total = 0; // K가 들어갈 공간이 몇개인지 나타내줄 원소
for (int x = 0; x < N; x++) {

int cnt = 0; //1개수 체크

//가로줄 체크 PART
for (int y = 0; y < N; y++) {
if (mainArray[x][y] == 1) //1이면 CNT=+1
cnt++;

//중간에 원소가 0이 확인 되거나, 마지막원소면
//cnt 체크해서 초기화 하거나 3개면 바로 total update
if (mainArray[x][y] == 0 || y == N - 1) {
if (cnt == K)
total += 1;
cnt = 0;
}
}

//세로줄 체크 PART (위 가로줄 과정과 동일)
for (int y = 0; y < N; y++) {
if (mainArray[y][x] == 1)
cnt++;
if (mainArray[y][x] == 0 || y == N - 1) {
if (cnt == K)
total += 1;
cnt = 0;
}

}

}

System.out.println("#" + (i + 1) + " " + total);

}

}

}
cs

 

 

복사했습니다!