测试点3 测试点5 容易错
注意一、这个点是独一无二,只出现了1次,如果这个点出现2次及以上,直接跳过。count()方法不能对嵌套列表使用
注意二、矩阵的边缘也要考虑,可以设置条件,如果是边缘就不对比。也可以给这个矩阵加个边框,值初始化为0;
注意三、题目说颜色差充分大,需加上绝对值abs
continue语句和break语句的区别:
continue语句是跳过本轮循环,执行下一轮循环
break语句是直接跳出循环体
正确,无超时。四号测试点1100ms(超过1400ms会被判为超时)
m, n, tol = map(int, input().split())
color = []
num = []
repeat = []
result = []
for i in range(n):
number = list(map(int,input().split()))
color.append(number)
num.extend(number)
for i in range(n):
for j in range(m):
if color[i][j] in repeat: # 检测到重复的数字,跳过本轮循环
continue
else:
count = num.count(color[i][j])
if count != 1: # 判断是否为重复数
repeat.append(color[i][j]) # 将重复的数字放入repeat列表中
continue
else: # 是独一无二的数
if i > 0 and j > 0: # 第一行、第一列不比较左上点
if abs(color[i][j] - color[i-1][j-1]) > tol:
pass
else:
continue
if i > 0: # 第一行不比较上点
if abs(color[i][j] - color[i-1][j]) > tol:
pass
else:
continue
if i > 0 and j < m - 1: # 第一行、最后一列不比较右上点
if abs(color[i][j] - color[i-1][j+1]) > tol:
pass
else:
continue
if j > 0: # 第一列不比较左点
if abs(color[i][j] - color[i][j-1]) > tol:
pass
else:
continue
if j < m - 1: # 最后一列不比较右点
if abs(color[i][j] - color[i][j+1]) > tol:
pass
else:
continue
if j > 0 and i < n - 1: # 第一列、最后一行不比较左下点
if abs(color[i][j] - color[i+1][j-1]) > tol:
pass
else:
continue
if i < n - 1: # 最后一行不比较下点
if abs(color[i][j] - color[i+1][j]) > tol:
pass
else:
continue
if i < n - 1 and j < m - 1: # 最后一行、最后一列不比较右下点
if abs(color[i][j] - color[i+1][j+1]) > tol:
pass
else:
continue
r = "("+str(j+1)+ ", " +str(i+1)+")"+": "+str(color[i][j])
result.append(r)
if len(result) == 1:
print(result[0])
elif len(result) > 1:
print("Not Unique")
else:
print("Not Exist")
有一个超时。思路是正确的,但多次使用count()方法计数,导致超时。可以将重复的数字放入列表,后轮检测到列表中相同的重复数,直接跳过本轮循环,减少count()方法的使用
m, n, tol = map(int, input().split())
color = []
num = []
result = []
for i in range(n):
number = list(map(int,input().split()))
color.append(number)
num.extend(number)
for i in range(n):
for j in range(m):
count = num.count(color[i][j])
if count != 1:
continue
if i > 0 and j > 0: # 第一行、第一列不比较左上点
if abs(color[i][j] - color[i-1][j-1]) > tol:
pass
else:
continue
if i > 0: # 第一行不比较上点
if abs(color[i][j] - color[i-1][j]) > tol:
pass
else:
continue
if i > 0 and j < m - 1: # 第一行、最后一列不比较右上点
if abs(color[i][j] - color[i-1][j+1]) > tol:
pass
else:
continue
if j > 0: # 第一列不比较左点
if abs(color[i][j] - color[i][j-1]) > tol:
pass
else:
continue
if j < m - 1: # 最后一列不比较右点
if abs(color[i][j] - color[i][j+1]) > tol:
pass
else:
continue
if j > 0 and i < n - 1: # 第一列、最后一行不比较左下点
if abs(color[i][j] - color[i+1][j-1]) > tol:
pass
else:
continue
if i < n - 1: # 最后一行不比较下点
if abs(color[i][j] - color[i+1][j]) > tol:
pass
else:
continue
if i < n - 1 and j < m - 1: # 最后一行、最后一列不比较右下点
if abs(color[i][j] - color[i+1][j+1]) > tol:
pass
else:
continue
r = "("+str(j+1)+ ", " +str(i+1)+")"+": "+str(color[i][j])
result.append(r)
if len(result) == 1:
print(result[0])
elif len(result) > 1:
print("Not Unique")
else:
print("Not Exist")
有两个答案错误。没有检测边缘点,只检测了中心点
m, n, tol = map(int, input().split())
color = []
result = []
for i in range(n):
color.append(list(map(int,input().split())))
for i in range(1,n-1):
for j in range(1,m-1):
if (color[i][j] - color[i-1][j-1] > tol and
color[i][j] - color[i-1][j] > tol and
color[i][j] - color[i-1][j+1] > tol and
color[i][j] - color[i][j-1] > tol and
color[i][j] - color[i][j+1] > tol and
color[i][j] - color[i+1][j-1] > tol and
color[i][j] - color[i+1][j] > tol and
color[i][j] - color[i+1][j+1] > tol):
r = "("+str(j+1)+ ", " +str(i+1)+")"+": "+str(color[i][j])
result.append(r)
if len(result) == 1:
print(result[0])
elif len(result) > 1:
print("Not Unique")
else:
print("Not Exist")
答案错误。使用列表记录了检测数据。没有删除重复颜色的像素点
m, n, tol = map(int, input().split())
color = []
result = []
for i in range(n):
color.append(list(map(int, input().split())))
for i in range(n):
for j in range(m):
judge = ["#" for x in range(8)]
if i > 0 and j > 0: # 第一行、第一列不比较左上点
if color[i][j] - color[i - 1][j - 1] > tol:
judge[0] = 1
else:
judge[0] = 0
if i > 0: # 第一行不比较上点
if color[i][j] - color[i - 1][j] > tol:
judge[1] = 1
else:
judge[1] = 0
if i > 0 and j < m - 1: # 第一行、最后一列不比较右上点
if color[i][j] - color[i - 1][j + 1] > tol:
judge[2] = 1
else:
judge[2] = 0
if j > 0: # 第一列不比较左点
if color[i][j] - color[i][j - 1] > tol:
judge[3] = 1
else:
judge[3] = 0
if j < m - 1: # 最后一列不比较右点
if color[i][j] - color[i][j + 1] > tol:
judge[4] = 1
else:
judge[4] = 0
if j > 0 and i < n - 1: # 第一列、最后一行不比较左下点
if color[i][j] - color[i + 1][j - 1] > tol:
judge[5] = 1
else:
judge[5] = 0
if i < n - 1: # 最后一行不比较下点
if color[i][j] - color[i + 1][j] > tol:
judge[6] = 1
else:
judge[6] = 0
if i < n - 1 and j < m - 1: # 最后一行、最后一列不比较右下点
if color[i][j] - color[i + 1][j + 1] > tol:
judge[7] = 1
else:
judge[7] = 0
if 0 not in judge:
r = "(" + str(j + 1) + ", " + str(i + 1) + ")" + ": " + str(color[i][j])
result.append(r)
if len(result) == 1:
print(result[0])
elif len(result) > 1:
print("Not Unique")
else:
print("Not Exist")
错误。可以不使用列表记录检测数据,符合条件就执行下一步,不符合就跳过本轮循环,开始下一轮。没有删除重复颜色的像素点
m, n, tol = map(int, input().split())
color = []
result = []
for i in range(n):
color.append(list(map(int,input().split())))
for i in range(n):
for j in range(m):
if i > 0 and j > 0: # 第一行、第一列不比较左上点
if color[i][j] - color[i-1][j-1] > tol:
pass
else:
continue
if i > 0: # 第一行不比较上点
if color[i][j] - color[i-1][j] > tol:
pass
else:
continue
if i > 0 and j < m - 1: # 第一行、最后一列不比较右上点
if color[i][j] - color[i-1][j+1] > tol:
pass
else:
continue
if j > 0: # 第一列不比较左点
if color[i][j] - color[i][j-1] > tol:
pass
else:
continue
if j < m - 1: # 最后一列不比较右点
if color[i][j] - color[i][j+1] > tol:
pass
else:
continue
if j > 0 and i < n - 1: # 第一列、最后一行不比较左下点
if color[i][j] - color[i+1][j-1] > tol:
pass
else:
continue
if i < n - 1: # 最后一行不比较下点
if color[i][j] - color[i+1][j] > tol:
pass
else:
continue
if i < n - 1 and j < m - 1: # 最后一行、最后一列不比较右下点
if color[i][j] - color[i+1][j+1] > tol:
pass
else:
continue
r = "("+str(j+1)+ ", " +str(i+1)+")"+": "+str(color[i][j])
result.append(r)
if len(result) == 1:
print(result[0])
elif len(result) > 1:
print("Not Unique")
else:
print("Not Exist")
print(result)