티스토리 뷰

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class Solution {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();

        int T = Integer.parseInt(br.readLine());
        for (int t = 0; t < T; t++) {
            String[] inputs = br.readLine().split(" ");
            int n = Integer.parseInt(inputs[0]);
            int k = Integer.parseInt(inputs[1]);

            int[][] arr = new int[n][n];
            for (int i = 0; i < n; i++) {
                inputs = br.readLine().split(" ");
                for (int j = 0; j < n; j++) {
                    arr[i][j] = Integer.parseInt(inputs[j]);
                }
            }

            int result = 0;
            for (int y = 0; y < n; y++) {
                result += inputWordCntFromX(arr[y], n, k);
            }
            for (int x = 0; x < n; x++) {
                result += inputWordCntFromY(arr, x, n, k);
            }

            sb.append(String.format("#%d %d\n", t + 1, result));
        }

        System.out.println(sb);
    }

    private static int inputWordCntFromX(int[] arr, int n, int k) {
        int count = 0;
        int result = 0;
        for (int x = 0; x < n; x++) {
            if (arr[x] == 1) {
                count++;
            } else {
                if (count == k) {
                    result++;
                }
                count = 0;
            }
        }
        if (count == k) {
            result++;
        }
        return result;
    }

    private static int inputWordCntFromY(int[][] arr, int x, int n, int k) {
        int count = 0;
        int result = 0;
        for (int y = 0; y < n; y++) {
            if (arr[y][x] == 1) {
                count++;
            } else {
                if (count == k) {
                    result++;
                }
                count = 0;
            }
        }
        if (count == k) {
            result++;
        }
        return result;
    }
}
Total
Today
Yesterday
최근에 올라온 글
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30