선택은 나의 것

[SWEA] 1979 어디에 단어가 들어갈 수 있을까 본문

☽ Algorithm/SWEA

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

Algoribi 2021. 8. 12. 13:34

문제

SWEA 1979 : 어디에 단어가 들어갈 수 있을까

 

SW Expert Academy

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

swexpertacademy.com

접근

 N x N의 퍼즐 판이 주어졌을 때 연속된 가로, 세로의 칸에 K의 길이를 갖는 단어가 들어갈 수 있는 자리의 개수를 찾는 간단한 문제이다. 2중 for문을 두 번(가로, 세로) 돌아서 찾을 수 있다.

코드

#include <iostream>
#define endl "\n"

using namespace std;

int main() {
    int test_case;
    cin >> test_case;
    for (int t = 1; t <= test_case; t++) {
        int n, k, map[20][20], ans = 0;
        cin >> n >> k;
        for (int i = 1; i <= n; i++) {
            int counter = 0;
            for (int j = 1; j <= n; j++) {
                cin >> map[i][j];
                if (map[i][j] == 1)
                    counter++;
                else if (map[i][j] == 0 && counter == k) {
                    ans++;
                    counter = 0;
                } else 
                    counter = 0;
            }
            if (counter == k)
                ans++;
        }
        for (int i = 1; i <= n; i++) {
            int counter = 0;
            for (int j = 1; j <= n; j++) {
                if (map[j][i] == 1)
                    counter++;
                else if (map[j][i] == 0 && counter == k) {
                    ans++;
                    counter = 0;
                } else 
                    counter = 0;
            }
            if (counter == k)
                ans++;
        }
        cout << "#" << t << " " << ans << endl;
    }
}

 

깃 허브 주소 : https://github.com/algoribi/algorithm-study

'☽ Algorithm > SWEA' 카테고리의 다른 글

[SWEA] 1285 아름이의 돌 던지기  (0) 2021.08.14
[SWEA] 1970 쉬운 거스름돈  (0) 2021.08.13
[SWEA] 11688 Calkin-Wilf tree 1  (0) 2021.08.11
[SWEA] 1983 조교의 성적 매기기  (0) 2021.08.04
[SWEA] 1859 백만 장자 프로젝트  (0) 2021.08.03
Comments