처음 생각이 어렵다.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int map[100][100];
int map2[100][100];
int n, L;
bool check(int map[][100], int x, int y) {
int s = map[x][y + 1];
for (int j = y+1; j <= y + L; j++) {
if (map[x][j] != s) return false;
}
return true;
}
int go(int map[][100]) {
int ans = 0;
for (int i = 0; i < n; i++) {
bool ok = true;
int front = 1;
for (int j = 0; j < n-1; j++) {
if (map[i][j]==map[i][j+1]) { //앞과 뒤가 같을 때
front++;
}
else if (map[i][j]==map[i][j+1]+1) { //앞이 1클때
if (check(map, i, j) == true) {
j = j + L - 1;
front = 0;
}
else {
ok = false;
break;
}
}
else if (map[i][j]+1==map[i][j+1]) { //뒤가 1클때
if (front >= L) {
front = 1;
}
else {
ok = false;
break;
}
}
else { //차이가 2이상
ok = false;
break;
}
}
if (ok) {
ans++;
//cout << "i: " << i << " " << '\n';
}
}
return ans;
}
int main() {
//freopen("in1.txt", "rt", stdin);
cin >> n >> L;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> map[i][j];
map2[j][i] = map[i][j];
}
}
int A = go(map);
int B = go(map2);
cout << A+B << '\n';
return 0;
}