코딩테스트/시뮬레이션
[시뮬레이션] 백준 14890 경사로 C++
유(YOO)
2023. 9. 4. 17:59
https://www.acmicpc.net/problem/14890
#include <iostream>
#include <vector>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n, l;
cin >> n >> l;
vector<vector<int>> board(n, vector<int>(n, 0));
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
cin >> board[i][j];
}
}
int answer = 0;
vector<vector<bool>> row(n, vector<bool>(n, false)); // 가로줄
for(int i=0; i<n; i++) {
bool flag = true;
for(int j=0; j<n-1; j++) {
if (board[i][j] > board[i][j+1]) { // 감소
if (board[i][j] - board[i][j+1] > 1) flag = false; // 2칸 이상 차이
for(int k=0; k<l; k++) {
if (j+1+k >= n) flag = false;
else if (board[i][j+1+k] != board[i][j+1]) flag = false;
}
if (flag) {
for(int k=0; k<l; k++) {
if (row[i][j+1+k]) flag = false;
row[i][j+1+k] = true;
}
}
else break;
}
if (board[i][j] < board[i][j+1]) { // 증가
if (board[i][j+1] - board[i][j] > 1) flag = false;
for(int k=0; k<l; k++) {
if (j-k < 0) flag = false;
else if (board[i][j-k] != board[i][j]) flag = false;
}
if (flag) {
for(int k=0; k<l; k++) {
if (row[i][j-k]) flag = false;
row[i][j-k] = true;
}
}
else break;
}
}
if (flag) answer++;
}
vector<vector<bool>> col(n, vector<bool>(n, false)); // 세로줄
for(int i=0; i<n; i++) {
bool flag = true;
for(int j=0; j<n-1; j++) {
if (board[j][i] > board[j+1][i]) { // 감소
if (board[j][i] - board[j+1][i] > 1) flag = false;
for(int k=0; k<l; k++) {
if (j+1+k >= n) flag = false;
else if (board[j+1+k][i] != board[j+1][i]) flag = false;
}
if (flag) {
for(int k=0; k<l; k++) {
if (col[j+1+k][i]) flag = false;
col[j+1+k][i] = true;
}
}
else break;
}
if (board[j][i] < board[j+1][i]) { // 증가
if (board[j+1][i] - board[j][i] > 1) flag = false;
for(int k=0; k<l; k++) {
if (j-k < 0) flag = false;
else if (board[j-k][i] != board[j][i]) flag = false;
}
if (flag) {
for(int k=0; k<l; k++) {
if (col[j-k][i]) flag = false;
col[j-k][i] = true;
}
}
else break;
}
}
if (flag) answer++;
}
cout << answer;
return 0;
}