我在一个编程论坛Ohjelmointiputka发现了这个问题:
有人说电脑可以找到解决方案,但我找不到证据。
证明存在一个包含117个元素的矩阵,该矩阵包含的数字使得人们可以读取数字1,2,…,100的平方。
这里的read意味着你确定了开始的位置和方向(8种可能性),然后往那个方向走,把数字连接起来。例如,如果您可以连续找到数字1,0,0,0,4,那么您就找到了整数100004,它包含1,2,10,100和20的平方数,因为您可以从该序列中读出1,4,100,10000和400 (反转)。
但是有太多的数字要找(准确地说是100个平方数,或者81个,如果你去掉那些包含在另一个总共312位的平方数中的数字)和如此少的整数,你必须将所有这些平方数放得如此密集,以至于找到这样一个矩阵是困难的,至少对我来说是这样。
我发现,如果存在这样的矩阵mxn,我们可以不损失一般性地假设m<=n。因此,该矩阵的类型一定是1x117
、3x39
或9x13
。但是什么样的算法可以找到矩阵呢?
我已经设法做了检查数字是否可以添加到黑板上的程序。但是如何实现搜索算法呢?
# -*- coding: utf-8 -*-
# Returns -1 if can not put and value how good a solution is if can be put. Bigger value of x is better.
def can_put_on_grid(grid, number, start_x, start_y, direction):
# Check that the new number lies inside the grid.
x = 0
if start_x < 0 or start_x > len(grid[0]) - 1 or start_y < 0 or start_y > len(grid) - 1:
return -1
end = end_coordinates(number, start_x, start_y, direction)
if end[0] < 0 or end[0] > len(grid[0]) - 1 or end[1] < 0 or end[1] > len(grid) - 1:
return -1
# Test if new number does not intersect any previous number.
A = [-1,-1,-1,0,0,1,1,1]
B = [-1,0,1,-1,1,-1,0,1]
for i in range(0,len(number)):
if grid[start_x + A[direction] * i][start_y + B[direction] * i] not in ("X", number[i]):
return -1
else:
if grid[start_x + A[direction] * i][start_y + B[direction] * i] == number[i]:
x += 1
return x
def end_coordinates(number, start_x, start_y, direction):
end_x = None
end_y = None
l = len(number)
if direction in (1, 4, 7):
end_x = start_x - l + 1
if direction in (3, 6, 5):
end_x = start_x + l - 1
if direction in (2, 0):
end_x = start_x
if direction in (1, 2, 3):
end_y = start_y - l + 1
if direction in (7, 0, 5):
end_y = start_y + l - 1
if direction in (4, 6):
end_y = start_y
return (end_x, end_y)
if __name__ == "__main__":
A = [['X' for x in range(13)] for y in range(9)]
numbers = [str(i*i) for i in range(1, 101)]
directions = [0, 1, 2, 3, 4, 5, 6, 7]
for i in directions:
C = can_put_on_grid(A, "10000", 3, 5, i)
if C > -1:
print("One can put the number to the grid!")
exit(0)
我还发现我认为暴力搜索或最好的第一次搜索太慢了。我认为可能有一个解决方案,使用模拟退火,遗传算法或装箱算法。我还想知道是否可以以某种方式应用马尔可夫链来找到网格。不幸的是,在目前的技能下,这些似乎对我来说太难实现了。
发布于 2019-04-23 03:06:58
在https://github.com/minkkilaukku/square-packing/blob/master/sqPackMB.py中有一个这样的程序。只需从第20行和第21行更改M=9,N=13。
https://stackoverflow.com/questions/44983929
复制相似问题