参考:https://blog.nowcoder.net/n/0a5f5968e0c94844917ada856a748cc5
思路:思维题,题目说了边界补零且无法被更改,所以说要让所有元素相同,那么最终结果应该都是0。因此对于中间是0来说,直接统计即可;对于中间是正数来说,他四周应该都<=0,才可能-1操作到0;同理对于中间是负数来说,他四周应该都>=0,才可能+1操作到0。最终统计出来后,判断一下没补0的n * n矩阵中,非零元素是否等于可操作元素即可,如果是的话输出"YES",否则输出"NO"。
代码:
import sys
input = lambda: sys.stdin.readline().strip()
import math
inf = 10 ** 18
def I():
return input()
def II():
return int(input())
def MII():
return map(int, input().split())
def GMI():
return map(lambda x: int(x) - 1, input().split())
def LI():
return input().split()
def LII():
return list(map(int, input().split()))
def LFI():
return list(map(float, input().split()))
fmax = lambda x, y: x if x > y else y
fmin = lambda x, y: x if x < y else y
isqrt = lambda x: int(math.sqrt(x))
'''
'''
def solve():
n = II()
a = [[0] * (n + 2)] + [[0] + LII() + [0] for _ in range(n)] + [[0] * (n + 2)]
cnt = 0
zeros = 0
for i in range(1, n + 1):
for j in range(1, n + 1):
if a[i][j] == 0:
zeros += 1
continue
if a[i][j] < 0 and a[i - 1][j] >= 0 and a[i + 1][j] >= 0 and a[i][j - 1] >= 0 and a[i][j + 1] >= 0:
cnt += 1
if a[i][j] > 0 and a[i - 1][j] <= 0 and a[i + 1][j] <= 0 and a[i][j - 1] <= 0 and a[i][j + 1] <= 0:
cnt += 1
print("YES" if cnt == n * n - zeros else "NO")
t = 1
# t = II()
for _ in range(t):
solve()

京公网安备 11010502036488号