图像匹配(测试点过不去)

图像匹配问题。给出若干幅图像,图像的数据可以用矩阵表示。图像的数据范围是[0,255]。给出起始的图像x,在剩下任一图像中任选图像y,计算x的最后一列和y的第一列的欧氏距离。剩下图像中这种欧氏距离最小的图像是是最匹配的图像。假设最匹配图像为y 0 , 继续在余下的图像中寻找和y 0最匹配的图像。重复这个过程,直到没有图像剩下。
现在给出p幅图像的数据,依次编号为1,2,,p。 每幅图像的大小是m×n. 假设第一幅图像设置为编号x, 1≤x≤p. 按照前一段图像最匹配描述,计算最匹配的序列。

输入格式:
第一行输入四个整数m,n,p,x
第二行到到底m*p行,每一行有n个数据。
依次表示编号为1,2,.,p的图像数据。

输出格式:
输出最匹配序列,包括起始编号x

输入样例:
2, 2, 3, 2
33, 44
33, 44
55, 33
55, 33
44, 99
44, 99
输出样例:
2 1 3
样例说明, 2, 2, 3, 2 表示图像大小为2×2, 共3幅图像,
起始图像编号是2。

第一幅图像数据

33, 44
33, 44

第二幅图像数据
55, 33
55, 33
第三幅图像数据
44, 99
44, 99

输出结果
2 1 3 表明起始图像x编号为2,图像2最匹配的是1,图像1最匹配的是3。

import numpy as np

m, n, p, x = map(int, input().split(','))
array = [[] for i in range(p)]
for i in range(p):
    array[i] = np.full((m, n), 0)
for i in range(p):
    for j in range(m):
        array[i][j] = input().split(',')

distance = 0
begin = x - 1
order = [x]
list = []

for i in range(p - 2):
    for j in range(p):
        for k in range(m):
            distance = distance + (array[begin][k][m - 1] - array[j][k][0]) ** 2
        distance = np.sqrt(distance)
        list.append(distance)
        if distance == min(list) and j+1 != x and j+1 not in order:
            order.append(j + 1)
            begin = j
        distance = 0

for i in range(p):
    if i+1 not in order:
        order.append(i+1)
for i in range(p):
    print(order[i], end=' ')

img


第二个测试点一直过不去!

改成这样

img

import numpy as np
n,m,p,x = map(int, input().split(','))
a = [[] for i in range(p)]
for i in range(p):
    a[i] = np.full((m, n), 0)
for i in range(p):
    for j in range(m):
        a[i][j] = input().split(',')
res = [x]
used = [0] * p
used[x-1] = 1
for i in range(p-1):
    x = res[-1] - 1
    if x == -1: x = p-1
    y = -1
    dist = float("inf")
    for j in range(p):
        if used[j]: continue
        t = np.linalg.norm(a[x][:,n-1] - a[j][:,0])
        if t < dist:
            dist = t
            y = j
    res.append(y+1)
    used[y] = 1
print(" ".join(str(x) for x in res))

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632