Python 3中,457个 316 306字节
E=enumerate
V={'+'}
Q=[[(-j,i,k)for i,u in E(open(0))for j,v in E(u)for k in[{v}&V,'join'][u[j:j+2]=='|-']]]
while Q:
a,b,c,d,*e=A=tuple(x//2for y,x in sorted((y,x)for x,y in E(Q.pop())));e or exit('NOT')
if{A}-V:V|={A};Q+=[[c,d,a,b]+e,A,A[2:]+A[:2]][a<c<b<d:][c<a<d<b:]
if b==d:Q=[[a,c]+e]
exit('KNOT')
??
该程序首先将结转换为矩形图,该矩形图具有以下限制:
- 没有两个垂直或水平线段位于同一条线上。
- 没有垂直线段跨越水平线段。
例如,第一个测试用例将转换为以下矩形图:
+-----------+
| |
| | +-------+
| | | |
| +-------+ | | |
| | | | | |
| | +---+ | |
| | | | | |
| | | +---+ |
| | | |
| | | +-------+
| | | | | |
+-----+ | | | |
| | | | | |
| +---+ | | |
| | | | | |
| | +-------------+ | |
| | | | | |
| | | +---+ |
| | | | | |
| | | | +---+
| | | |
+-+ | |
| |
+-+
我们用垂直线段的y坐标从右到左的顺序唯一表示:
(5,10, 1,9, 8,10, 9,12, 5,12, 1,4, 0,3, 2,4, 3,7, 6,8, 7,11, 2,11, 0,6)
然后按照Ivan Dynnikov,“链接的弧形表示”中的描述,搜索矩形图的简化形式。单调简化”,2004年。Dynnikov证明,从小结的任何矩形图中,都有一系列简化的动作,这些动作以平凡的图结束。简要地说,允许的移动包括:
- 循环排列垂直(或水平)线段;
- 在某些配置约束下交换连续的垂直(或水平)线段。
- 用一个顶点替换位于图的一角的三个相邻顶点。
请参阅纸张以获取图片。这不是一个明显的定理。例如,如果使用不增加交叉次数的Reidemeister动作,则不成立。但是对于上面的特定简化,事实证明这是正确的。
(我们通过仅排列垂直线段来简化实现,还允许将整个结转置为将水平线与垂直线互换。)
演示版
$ python3 knot.py <<EOF
+-------+ +-------+
| | | |
| +---|----+ +-------+
| | | | | |
+-------|------------|---+
| | | |
+---+ +---+
EOF
KNOT
$ python3 knot.py <<EOF
+----------+
| |
| +--------------+
| | | |
| | +-|----+ |
| | | | | |
| +-----+ | |
| | | |
| +------|---+
| |
+---------------+
EOF
NOT
$ python3 knot.py <<EOF # the Culprit
+-----+
| |
+-----------+ |
| | | |
| +-+ | +---|-+
| | | | | | | |
| +-|-------+ | |
| | | | | | | |
+-|-+ | | +---+ |
| | | |
+---|---------+
| |
+-+
EOF
NOT
$ python3 knot.py <<EOF # Ochiai unknot
+-----+
| |
+-|---------+
| | | |
| | +-+ | |
| | | | | |
+-|-|---|-|-+ |
| | | | | | | |
| | | +---|---+
| | | | | |
+-------+ | |
| | | |
| +-------+
| |
+-------+
EOF
NOT
$ python3 knot.py <<EOF # Ochiai unknot plus trefoil
+-----+ +-----+
| | | |
+-|---------+ |
| | | | | |
| | +-+ | +---+ |
| | | | | | | |
+-|-|---|-|-+ +---+
| | | | | | | |
| | | +---|-----+
| | | | | |
+-------+ | |
| | | |
| +-------+
| |
+-------+
EOF
KNOT
$ python3 knot.py <<EOF # Thistlethwaite unknot
+---------+
| |
+---+ +---------+
| | | | | |
| +-------+ | |
| | | | | |
| | | +---+ |
| | | | | |
| | +-------+ |
| | | | | |
| +-------+ | |
| | | | | |
+-----------+ | | | |
| | | | | |
| +-----------+ | | |
| | | | | |
| | +-------------+ |
| | | |
| | +-----+
| | | |
| | +---+
| | | |
+---------------------+ |
| |
+---------------------+
EOF
NOT
$ python3 knot.py <<EOF # (−3,5,7)-pretzel knot
+-------------+
| |
+-|-----+ |
| | | |
+-|-+ +-------+ |
| | | | | |
+-|-+ +---+ +---+
| | | | | |
| | +---+ +---+
| | | | | |
| | +---+ +---+
| | | | | |
| +---+ +---+
| | | |
| | +---+
| | | |
| | +---+
| | | |
| +---+
| |
+-----+
EOF
KNOT
$ python3 knot.py <<EOF # Gordian unknot
+-------------+ +-------------+
| | | |
| +---------+ | | +---------+ |
| | | | | | | |
| | +-------------+ +-------------+ | |
| | | | | | | | | | | |
| | | +---------+ | | +---------+ | | |
| | | | | | | | | | | | | | | |
| +-------+ | +-------+ +-------+ | +-------+ |
| | | | | | | | | | | | | | | |
+-------+ | +-------+ | | +-------+ | +-------+
| | | | | | | | | | | | | | | |
| +-------+ | | | | | | | | +-------+ |
| | | | | | | | | | | | | | | |
+-------+ | | | | | | | | | | +-------+
| | | | | | | | | | | | | | | |
| +-----+ | | | | | | +-----+ |
| | | | | | | | | | | |
+---------+ | | | | +---------+
| | | | | | | |
+---------+ | | +---------+
| | | | | | | |
| | +-----------------+ | |
| | | | | |
| +---------------------+ |
| | | |
+-----------+ +-----------+
EOF
NOT