Coding Test/SW Expert Academy

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

owls 2022. 11. 16. 11:26
728x90
  • 문제

※ SW Expert 아카데미의 문제를 무단 복제하는 것을 금지합니다.

N X N 크기의 단어 퍼즐을 만들려고 한다. 입력으로 단어 퍼즐의 모양이 주어진다.

주어진 퍼즐 모양에서 특정 길이 K를 갖는 단어가 들어갈 수 있는 자리의 수를 출력하는 프로그램을 작성하라.

 

input output
2
5 3
0 0 1 1 1
1 1 1 1 0
0 0 1 0 0
0 1 1 1 1
1 1 1 0 1
5 3
1 0 0 1 0
1 1 0 1 1
1 0 1 1 1
0 1 1 0 1
0 1 1 1 0
#1 2
#2 6

 

  • 문제 해결

행우선 2중for문, 열우선 2중for문으로 

연속된 1의 개수와 k가 같은지 검사하면 된다.

#include<iostream>
#include <vector>
#include <numeric>
using namespace std;

int main(int argc, char** argv)
{
	int test_case;
	int T;
	cin>>T;

	for(test_case = 1; test_case <= T; ++test_case)
	{
		int n = 0, k = 0;
        cin >> n >> k;
        vector<vector<int>> vec(n, vector<int>(n, 0));
        
        for(int i = 0; i < n; i++){
			int cnt = 0;
        	for(int j = 0; j < n; j++){
            	cin >> vec[i][j];
               
            }            
        }
        
		int result = 0;
        //행우선
        for(int i = 0; i < n; i++){
            int sum = 0;
        	for(int j = 0; j < n; j++){
            	if( vec[i][j] == 0){
                	if( sum == k){
                    	result++;
                    }
                    sum = 0;
                }
                else{
                	sum++;
                }
            }
            if( sum == k)
                result++;
        }
        
        //열우선
        for(int i = 0; i < n; i++){
            int sum = 0;
        	for(int j = 0; j < n; j++){
            	if( vec[j][i] == 0){
                	if(sum == k){
                    	result++;
                    }
                    sum = 0;
                }
                else{
                	sum++;
                }
            }
            if(sum == k)
                result++;
        }
       
        cout << "#" << test_case << " " << result << endl;
    }
	return 0;
}
728x90