1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | import sys from collections import deque def find_node(y, x): array = [] dx = [0, 0, 1, 1, 1, –1, –1, –1] dy = [1, –1, 0, 1, –1, 0, –1, 1] flag = True for i in range(8): ny = y + dy[i] nx = x + dx[i] if 0 <= nx < m and 0 <= ny < n: if arr[ny][nx] == arr[y][x] and visited[ny][nx] == 0: array.append((ny, nx)) elif arr[ny][nx] > arr[y][x]: flag = False return array, flag def bfs(y, x): queue = deque([(y, x)]) visited[y][x] = 1 flag = True while queue: v = queue.popleft() array = find_node(v[0], v[1])[0] if flag: flag = find_node(v[0], v[1])[1] for i in array: queue.append(i) visited[i[0]][i[1]] = 1 return flag n, m = map(int, sys.stdin.readline().rstrip().split()) arr = [] visited = [[0] * m for _ in range(n)] result = 0 # 배열 입력 받음 for _ in range(n): arr.append(list(map(int, sys.stdin.readline().rstrip().split()))) for i in range(n): for j in range(m): if visited[i][j] == 0: if bfs(i, j): result += 1 print(result) | cs |