오답
#include <iostream>
using namespace std;
int n, l;
int map[100][100];
int dp[100][100] = { 1 };
int dx[4] = { -1,1,0,0 };
int dy[4] = { 0,0,-1,1 };
int result = 0;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> l;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> map[i][j];
}
}
for (int i = 0; i < n; i++) {
bool flag = true;
for (int j = 1; j < n; j++) {
if (map[i][j - 1] == map[i][j]) {
dp[i][j] = dp[i][j-1] + 1;
continue;
}
if ((map[i][j] == map[i][j - 1]+1 && dp[i][j - 1] < l) || map[i][j] > map[i][j - 1] + 1) {
flag = false;
break;
}
else if (map[i][j] == map[i][j - 1]+1 && dp[i][j - 1] >= l) {
dp[i][j] = 0;
continue;
}
else if (map[i][j] == map[i][j - 1] - 1) {
if (j + 1 >= n)
{
flag = false;
break;
}
else {
if (map[i][j + 1] != map[i][j])
{
flag = false;
break;
}
else {
continue;
}
}
}
else {
flag == false;
break;
}
}
if (flag == true) {
result++;
}
}
memset(dp, 1, sizeof(dp));
for (int i = 0; i < n; i++) {
bool flag = true;
for (int j = 1; j < n; j++) {
if (map[j-1][i] == map[j][i]) {
dp[j][i] = dp[j-1][i] + 1;
continue;
}
if ((map[j][i] == map[j-1][i]+1 && dp[j-1][i] < l) || map[i][j] > map[j-1][i] + 1) {
flag = false;
break;
}
else if (map[j][i] == map[j-1][i]+1 && dp[j-1][i] >= l) {
dp[j][i] = 0;
continue;
}
else if (map[j - 1][i] == map[j][i] - 1) {
if (j + 1 >= n)
{
flag = false;
break;
}
else {
if (map[j + 1][i] != map[j][i])
{
flag = false;
break;
}
else {
continue;
}
}
}
else {
flag == false;
break;
}
}
if (flag == true) {
result++;
}
}
cout << result;
return 0;
}
정답
#include <iostream>
#include <vector>
using namespace std;
int n, l;
int map[100][100];
int result = 0;
void input() {
cin >> n >> l;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> map[i][j];
}
}
return;
}
void answer() {
cout << result;
}
bool can_ramp(vector<int> &v) {
vector<bool> check(n,false);
for (int i = 1; i < n; i++) {
if (v[i - 1] == v[i]) continue;
else if (v[i] == v[i - 1] + 1) {
for (int j = 1; j <= l; j++)
if (i - j < 0 || v[i - j] != v[i - 1] || check[i - j]) return false;
for (int j = 1; j <= l; j++)
check[i - j] = true;
}
else if (v[i] + 1 == v[i - 1]) {
for (int j = 0; j < l; j++)
if (i + j >= n || v[i + j] != v[i] || check[i + j]) return false;
for (int j = 0; j < l; j++)
check[i + j] = true;
i += l - 1;
}
else return false;
}
return true;
}
void solve() {
for (int i = 0; i < n; i++) {
vector<int> v;
for (int j = 0; j < n; j++) {
v.push_back(map[i][j]);
}
if (can_ramp(v)) {
result++;
}
}
for (int i = 0; i < n; i++) {
vector<int> v;
for (int j = 0; j < n; j++) {
v.push_back(map[j][i]);
}
if (can_ramp(v)) {
result++;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
input();
solve();
answer();
return 0;
}