알고리즘/SWEA

[SWEA 4014] 활주로 건설

히더 2018. 7. 23. 14:44

출처 : 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++;
    }
}