cgy12306
[백준 BoJ] 14890 - 경사로 본문
// https://www.acmicpc.net/problem/14890
// 경사로
// reference by wizely
#include <iostream>
#include <algorithm>
using namespace std;
int N, L;
int map[101][101] = { 0, };
int res = 0;
void checkX(int x) {
int cnt = 1;
for (int j = 0; j < N - 1; j++) {
int v = map[x][j + 1] - map[x][j];
// 평지 카운팅
if (v == 0) cnt++;
// 경사가 오르막길일 경우 카운트 초기화, 만약 카운트가 L보다 작으면 종료
else if (v == 1 && cnt >= L) cnt = 1;
// 경사가 내려막길일 경우 cnt를 -L + 1 로 변환
else if (v == -1 && cnt >= 0) cnt = -L + 1;
// 경사로 차이가 2일경우 종료
else return;
}
if (cnt >= 0) res++;
}
void checkY(int y) {
int cnt = 1;
for (int j = 0; j < N - 1; j++) {
int v = map[j + 1][y] - map[j][y];
if (v == 0) cnt++;
else if (v == 1 && cnt >= L) cnt = 1;
else if (v == -1 && cnt >= 0) cnt = -L + 1;
else return;
}
if (cnt >= 0)res++;
}
void simulation() {
for (int i = 0; i < N; i++) {
checkX(i);
checkY(i);
}
}
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];
}
}
simulation();
cout << res << "\n";
return 0;
}
- simulation
아무리 노가다로 구현해봐도 예외 케이스를 찾을 수 없어서 wizely님의 코드를 참고 했습니다.
경사가 올라가거나 내려갈 때 이전의 평지들의 개수를 카운팅합니다.
경사가 내려갔다가 바로 올라가는 것은 안되기 때문에 카운팅 해준 변수로 제어합니다.
'Algorithm > C++' 카테고리의 다른 글
[백준 BoJ] 14502 - 연구소 (0) | 2021.10.23 |
---|---|
[백준 BoJ] 15686 - 치킨 배달 (0) | 2021.10.23 |
[백준 BoJ] 14499 - 주사위 굴리기 (0) | 2021.10.22 |
[백준 BoJ] 13458 - 시험 감독 (0) | 2021.10.22 |
[백준 BoJ] 14503 - 로봇 청소기 (0) | 2021.10.22 |
Comments