STUDY/Algorithm

[백준] 14890 경사로 python

sinawi95 2022. 3. 15. 10:48
728x90

https://www.acmicpc.net/problem/14890

 

14890번: 경사로

첫째 줄에 N (2 ≤ N ≤ 100)과 L (1 ≤ L ≤ N)이 주어진다. 둘째 줄부터 N개의 줄에 지도가 주어진다. 각 칸의 높이는 10보다 작거나 같은 자연수이다.

www.acmicpc.net

구현 문제여서 그냥 풀기만 하면된다.(사람마다 풀이가 다 다를듯...)

# import sys; input = sys.stdin.readline

def check():
    global N, L, _map
    ret = 0

    for r in range(N):
        h = _map[r][0]
        cnt = 1
        for c in range(1, N):
            if h == _map[r][c]:
                cnt += 1
                h = _map[r][c]
                continue
            if h == _map[r][c] - 1 and cnt >= L:
                h = _map[r][c]
                cnt = 1
                continue
            elif h == _map[r][c] + 1:
                for i in range(1, L):
                    if c + i < N and _map[r][c + i] == _map[r][c]:
                        continue
                    break
                else:
                    h = _map[r][c]
                    cnt = -L + 1
                    continue
            break
        else:
            ret += 1

    for c in range(N):
        h = _map[0][c]
        cnt = 1
        for r in range(1, N):
            if h == _map[r][c]:
                cnt += 1
                h = _map[r][c]
                continue
            if h == _map[r][c] - 1 and cnt >= L:
                h = _map[r][c]
                cnt = 1
                continue
            elif h == _map[r][c] + 1:
                for i in range(1, L):
                    if r + i < N and _map[r + i][c] == _map[r][c]:
                        continue
                    break
                else:
                    h = _map[r][c]
                    cnt = -L + 1
                    continue
            break
        else:
            ret += 1

    return ret

def main():
    global N, L, _map
    N, L = map(int, input().split())
    _map = [list(map(int, input().split())) for _ in range(N)]
    answer = check()
    print(answer)


if __name__ == "__main__":
    main()

이게왜 골드 3문제인진 모르겠다.