4gats 2023. 7. 11. 14:57

https://www.acmicpc.net/problem/14890

 

14890번: 경사로

첫째 줄에 N (2 ≤ N ≤ 100)과 L (1 ≤ L ≤ N)이 주어진다. 둘째 줄부터 N개의 줄에 지도가 주어진다. 각 칸의 높이는 10보다 작거나 같은 자연수이다.

www.acmicpc.net

조건에 맞게 철저하게 구현을 하면 된다.

처음엔 재귀로 풀려고 하다가

for문 한 번에 하는 게 훨씬 낫다는 걸 깨닫게 되었다...

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <iostream>
 
using namespace std;
 
int n, len;
int map[104][104];
 
bool row_cnt(int row_num)
{
    int sameHeight = 1;
    // 다음 값과 비교를 하니까 n - 2까지
    for (int j = 0; j < n - 1; j++)
    {
        // 높이 같은 길이 연속
        if (map[row_num][j] == map[row_num][j + 1])
        {
            sameHeight++;
            continue;
        }
        // 한 칸 낮은 곳을 만났을 때
        else if (map[row_num][j] == map[row_num][j + 1] + 1)
        {
            int cnt = 1;
            for (int k = 1; k < len; k++)
            {
                if (map[row_num][j + k] == map[row_num][j + k + 1])
                    cnt++;
                else
                    return false;
            }
            
            if (cnt == len)
            {
                j += cnt - 1;
                sameHeight = 0;
            }
            
        }
        // 더 높은 길 만났을 때
        else if (map[row_num][j] + 1 == map[row_num][j + 1])
        { 
            // 기존에 평평한 길이가 len 보다 같거나 길어야 놓지
            if (sameHeight >= len)
            {
                sameHeight = 1;
                continue;
            }
            else
            {
                return false;
            }
        }
        // 높이 차 1 넘을 때
        else if (abs(map[row_num][j] - map[row_num][j + 1]) >= 2)
        {
            return false;
        }
    }
 
    return true;
}
 
 
 
bool column_cnt(int column_num)
{
    int sameHeight = 1;
    for (int i = 0; i < n - 1; i++)
    {
        if (map[i][column_num] == map[i + 1][column_num])
        {
            sameHeight++;
            continue;
        }
        else if (map[i][column_num] == map[i+1][column_num] + 1)
        {
            int cnt = 1;
            bool chk = true;
            for (int k = 1; k < len; k++)
            {
                if (map[i + k][column_num] == map[i+k+1][column_num])
                    cnt++;
                else
                {
                    return false;
                }
            }
            if (cnt == len)
            {
                i += cnt - 1;
                sameHeight = 0;
            }
            
        }
        else if (map[i][column_num] + 1 == map[i+1][column_num])
        {
            if (sameHeight >= len)
            {
                sameHeight = 1;
                continue;
            }
            else
            {
                return false;
            }
        }
        else if (abs(map[i][column_num] - map[i + 1][column_num]) >= 2)
        {
            return false;
        }
    }
 
    return true;
}
 
int main()
{
    cin >> n >> len;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            cin >> map[i][j];
        }
    }
    int ans = 0;
 
 
    for (int i = 0; i < n; i++)
    {
        if (row_cnt(i))
            ans++;
    }
 
    for (int j = 0; j < n; j++)
    {
        if (column_cnt(j))
            ans++;
    }
 
    cout << ans << '\n';
 
    return 0;
}
cs