알고리즘/삼성 SW expert Academy

[SWEA] 1979. 어디에 단어가 들어갈 수 있을까_JAVA

뇌장하드 2022. 5. 7. 17:57

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=2&contestProbId=AV5PuPq6AaQDFAUq&categoryId=AV5PuPq6AaQDFAUq&categoryType=CODE&problemTitle=&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=2&pageSize=10&pageIndex=1 

 

SW Expert Academy

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

swexpertacademy.com

 

가로 세로 방향으로 따로 구해서 완전 탐색을 해준다. 

 

 

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.util.Scanner;
 
 
public class Solution {
    public static void main(String args[]) throws Exception
    {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for(int test_case = 1; test_case <= T; test_case++)
        {
            int n = sc.nextInt();
            int k = sc.nextInt();
            int [][] arr=new int[n][n];
            int ans=0;
            for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
                    arr[i][j]=sc.nextInt();
                }
            }
            //가로 확인
            for(int i=0;i<n;i++){
                int colcount=0;
                for(int j=0;j<n;j++){
                    if(arr[i][j]==0){
                        if(colcount==k){
                            ans++;
                        }
                        colcount=0;
                    }
                    else//arr[i][j] 가 1일떄
                        colcount+=1;
                    }
                }
                if(colcount==k) ans++;
            }
 
            //세로
            for(int i=0;i<n;i++){
                int rowcount=0;
                for(int j=0;j<n;j++){
                    if(arr[j][i]==0){
                        if(rowcount==k){
                            ans++;
                        }
                        rowcount=0;
                    }
                    else//arr[j][i] 가 1일떄
                        rowcount+=1;
                    }
                }
                if(rowcount==k) ans++;
            }
            System.out.println("#" + test_case + " " + ans);
        }
        sc.close();
    }
}
cs