14890번 경사로

동도리동·2021년 10월 13일
0

코딩테스트

목록 보기
56/76

처음 생각이 어렵다.

#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;
}

참고자료 https://yabmoons.tistory.com/49

profile
긍정코딩세상

0개의 댓글

관련 채용 정보