https://www.acmicpc.net/problem/14890
이 문제는 조건이 까다롭지 하나하나 따지며 생각하면서 if문으로 뚫으면 괜찮다.
실수한게 재귀함수에 return을 넣지 않아서 계속 result가 다르게 나와서 cout을 곳곳에 넣었는데 오히려 이 글을 보시는 분들이 출력을 잘 볼 수 있는 좋은 상황이 된거 같다.
#include <iostream>
#include <cmath>
using namespace std;
int n, l;
int arr[101][101];
int temp[101][101];
int landCount = 0;
int result = 0;
void go(int y, int x, int past)
{
if (x >= n) { //끝까지 옴.
//cout << "Come to End Y : " << y << "\n";
result++;
return;
}
if (past == 0) { //최초이다.
past = arr[y][x];
landCount++; //첫칸 한칸이 있음을 알림.
go(y, x + 1, past);
return;
}
int sub = arr[y][x] - past;
if (sub > 1 || sub < -1) {
//cout << "sub Out Y : " << y << "\n";
return; //차이가 1이상이면 나가라}
}
past = arr[y][x];
if (sub == 0) { //이전과 현재의 높이가 같다면
landCount++;
go(y, x + 1, past);
}
else if (sub > 0) { //현재가 더 높다.
if (landCount < l) {
//cout << "Now big Out Y : " << y << " now x : " << x << " landCount : " << landCount << "\n";
return; //현재까지의 평지가 충분하지 않다.
}
landCount = 1;
go(y, x + 1, past);
}
else if (sub < 0) { //지금이 더 낮다.
int i;
for (i = 1; i < l; i++) {
if (arr[y][x + i] != arr[y][x]) {
//cout << "Now low Out Y : " << y << " now x : " << x << "\n";
return;
}//높이가 같지 않으면 나가라
}
//4443 3 3 3334322222
landCount = 0;
go(y, x + l, past);
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> l;
fill(&arr[0][0], &arr[0][0], -1); //높이 최소가 1이라서 높이 차를 2로 만들기 위해 -1로 만들었음.
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int t; cin >> t;
arr[i][j] = t;
}
}
for (int i = 0; i < n; i++) {
landCount = 0;
go(i, 0, 0);
}
//배열 뒤집기
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
temp[i][j] = arr[j][i];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = temp[i][j];
}
}
for (int i = 0; i < n; i++) {
landCount = 0;
go(i, 0, 0);
}
cout << result;
}
'백준 문제 풀이 & C++ 공부' 카테고리의 다른 글
백준 14391 C++ (0) | 2024.05.30 |
---|---|
백준 C++ 2234번 (0) | 2024.05.13 |
백준 2910 C++ (0) | 2024.04.23 |
백준 17471 C++ (0) | 2024.04.17 |
백준 14620 C++ (0) | 2024.03.25 |