这题写了好久,看网上也没有太多用Python实现的,所以就坚持自己写了一下
思路:
全是基本方法,没有使用numpy等高级操作。因为本人不会[笑哭]。
用list构造一张二维表,并在原数据的周围的边缘插入一圈数据(必定和题目中所给的数据相差大于TOL,这里取了0x3f3f3f3f),这样做,主要是为了方便,以免下标越界,不然边界情况处理起来很麻烦
然后就直接写了一个方法,把当前的下标左右九宫格的右下位置,来判断左上方数据是否符合条件,表中的所有数据进行一次遍历即可。
M, N, TOL = list(map(int, input().split()))
li = []
answer = ""
flag_unique = True
assembly = set()
repetition = set()
li.append([0x3f3f3f3f] * (M+2))
#构造二维表,并去重
for i in range(N):
temp = list(map(int, input().split()))
for j in temp:
if j in assembly:
repetition.add(j)
else:
assembly.add(j)
temp.insert(0, 0x3f3f3f3f)
temp.append(0x3f3f3f3f)
li.append(temp)
li.append([0x3f3f3f3f] * (M+2))
#利用以下方法进行遍历,注意去掉边界
for i in range(N+2):
if not flag_unique: break
if i not in (0, 1):
for j in range(M+2):
if j not in (0, 1):
mid = li[i-1][j-1]
if abs(mid - li[i-2][j-2]) > TOL and abs(mid - li[i-2][j-1]) > TOL:
if abs(mid - li[i-2][j]) > TOL and abs(mid - li[i-1][j-2]) > TOL:
if abs(mid - li[i-1][j]) > TOL and abs(mid - li[i][j-2]) > TOL:
if abs(mid - li[i][j-1]) > TOL and abs(mid - li[i][j]) > TOL:
if mid not in repetition:
if answer == "":
answer = "({}, {}): {}".format(j-1, i-1, mid)
else:
flag_unique = False
break
if flag_unique == False:
print("Not Unique")
elif answer == "":
print("Not Exist")
else:
print(answer)