Skip to content

Instantly share code, notes, and snippets.

@modos
Created April 26, 2023 10:57
Show Gist options
  • Save modos/7e68793c65c4221d34c1577e87a33feb to your computer and use it in GitHub Desktop.
Save modos/7e68793c65c4221d34c1577e87a33feb to your computer and use it in GitHub Desktop.
جدول
#In the name of God
import sys
sys.setrecursionlimit(1000 * 1000 + 10)
maxn = 3000 * 1000 + 10
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0 , 1 , -1, 1 , -1, 0 , 1]
n = int(input())
mark = [[False for i in range(n)] for i in range(n)]
col = [ [] for i in range(n)]
for i in range(n):
col[i] = list(map(int, input().split()))
def valid(r, c):
return r in range(n) and c in range(n)
def dfs(r, c, return_flag_smaller, return_flag_bigger):
mark[r][c] = True
for i in range(8):
if valid(r + dx[i], c + dy[i]):
if not mark[r + dx[i]][c + dy[i]] and col[r + dx[i]][c + dy[i]] == col[r][c]:
return_flag_smaller, return_flag_bigger = dfs(r + dx[i], c + dy[i], return_flag_smaller, return_flag_bigger)
else:
if col[r][c] > col[r + dx[i]][c + dy[i]]:
return_flag_smaller = False
if col[r][c] < col[r + dx[i]][c + dy[i]]:
return_flag_bigger = False
return [return_flag_smaller, return_flag_bigger]
peak = 0
low = 0
for i in range(n):
for j in range(n):
if not mark[i][j]:
x = dfs(i, j, True, True)
low += int(x[0])
peak += int(x[1])
print(peak, low)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment