티스토리 뷰

출처 : https://www.swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWIeW7FakkUDFAVH


#include <iostream>

#include <algorithm>
using namespace std;
 
#define MAX_N 20
 
int i, j, N, X, K, cnt, result;
int ga[MAX_N][MAX_N], se[MAX_N][MAX_N];
bool visited[MAX_N][MAX_N], check;
 
void input()
{
    int tmp; result = 0;
    cin >> N >> X;
    for (i = 0;i < N;i++) for (j = 0;j < N;j++) { cin >> tmp; ga[i][j] = tmp; se[j][i] = tmp; }
}
 
void cal(int(*garo)[MAX_N])
{
    for (i = 0;i < N;i++)
    {
        for (j = 0;j < N-1;j++)
        {
            check = true;
            if (garo[i][j] - garo[i][j + 1] == 1) // 낮아지는 경우
            {
                for (cnt = 1;cnt < X;cnt++)
                {
                    if (j+1+cnt > N-1 || garo[i][j + 1] != garo[i][j + 1 + cnt]) check = false;
                    if (cnt == X - 1 && check==true) visited[i][j + 1 + cnt] = true;
                }
            }
            else if (garo[i][j] - garo[i][j + 1] == -1) // 높아지는 경우
            {
                for (cnt = 1;cnt < X;cnt++)
                {
                    if (visited[i][j]==true || visited[i][j-cnt]==true || j-cnt < 0 || garo[i][j] != garo[i][j-cnt]) check = false;
                }
            }
            else if (abs(garo[i][j] - garo[i][j + 1]) > 1) break;
 
            if (check == false) break;
            if (j == N - 2) result += 1;
        }
    }
}
 
int main()
{
    int T, t; t = 1;
    cin >> T;
    while (T--)
    {
        input();
        for (i = 0;i < N;i++) for (j = 0;j < N;j++) visited[i][j] = false;
        cal(ga);
        for (i = 0;i < N;i++) for (j = 0;j < N;j++) visited[i][j] = false;
        cal(se);
        cout << "#" << t << " " << result << endl;
        t++;
    }
}


'알고리즘 > SWEA' 카테고리의 다른 글

[SWEA 2105] 디저트 카페  (0) 2018.07.23
[SWEA 1949] 등산로 조정  (0) 2018.07.23
[SWEA 4013] 특이한 자석  (0) 2018.07.23
[SWEA 3752] 가능한 시험 점수  (0) 2018.07.23
[SWEA 1952] 수영장  (0) 2018.07.23
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/10   »
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
글 보관함