原题链接:PTA | 程序设计类实验辅助教学平台
- 测试点0: 独一无二
- 测试点1: Not Unique
- 测试点2: Not Exist
- 测试点3: 独一无二 点在边缘(相邻数值不足8个)
4 5 2
0 7 0 0
0 0 0 0
0 0 0 4
0 0 0 0
0 0 7 0
答案:(4, 3): 4
- 测试点5: 点在边缘(相邻数值不足8个)
4 5 2
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 7 0
(3, 5): 7
坑点分析:
坑点1:在一行中按照 (x, y): color
的格式输出所求像素点的位置以及颜色值,其中位置 x
和 y
分别是该像素在图像矩阵中的列、行编号(从 1 开始编号)。
解析:输出形式:先列,后行!编号从1开始
坑点2:万绿丛中的一点红,即有独一无二颜色的那个像素点。
解析:整个图像矩阵中只出现过一次,字典判断出现次数,count肯定会超时,没尝试~
坑点3:并且该点的颜色与其周围 8 个相邻像素的颜色差充分大。
解析:图像矩阵中每个点与相邻一圈差值都>TOL颜色差阈值,如:左上角只判断【右】【右下】【下】,第一列没有【左上】【左】【左下】,-1列亦是如此
坑点4:色差超过 TOL 的点才被考虑。
解析:计算时加上绝对值abs,因为判断像素点可能比周围一圈数字小,色差是绝对值差,不分正负哦~
有问题留言,看到会回复解答~
AC代码如下:
import sys
dic = {}
m, n, tol = map(int, sys.stdin.readline().split())
img = [[10**9 for j in range(m+2)] for i in range(n+2)]
for i in range(1, n+1):
nums = list(map(int, sys.stdin.readline().split()))
for j in range(1, m+1):
num = nums[j-1]
img[i][j] = num
dic[num] = dic.get(num, 0) + 1
row, col, max_num = 0, 0, 0
Not_Unique = False
Not_Exist = True
for i in range(1, n+1):
if Not_Unique: break
for j in range(1, m+1):
num = img[i][j]
if dic[num] == 1:
lft = img[i][j-1]
up_lft = img[i-1][j-1]
up = img[i-1][j]
up_rht = img[i-1][j+1]
lw_lft = img[i+1][j-1]
lw = img[i+1][j]
lw_rht = img[i+1][j+1]
rht = img[i][j+1]
if abs(num - lft) > tol and abs(num - up_lft) > tol and abs(num - up) > tol and abs(num - up_rht) > tol:
if abs(num - lw_lft) > tol and abs(num - lw) > tol and abs(num - lw_rht) > tol and abs(num - rht) > tol:
Not_Exist = False
if row !=0:
Not_Unique = True
break
col = j
row = i
max_num = num
if Not_Exist:
print('Not Exist')
elif Not_Unique:
print('Not Unique')
else:
print(f'({col}, {row}): {max_num}')
标签:1068,num,测试点,img,lw,abs,tol,PAT
From: https://blog.csdn.net/m0_56677113/article/details/142690348