有许多谜题是经典的“柯尼斯堡 7 桥”谜题的变体,您必须在不使用两次门的情况下找到穿过一组房间的路线。
...并且是一个稍微修改的难题,确实有解决方案,如您在此处看到的。
我对解决这类问题的编程方法很感兴趣,虽然有很多方法可以确定房间和门的特定配置没有解决方案,但我有兴趣计算要访问的门列表以解决谜。查看问题的一种方法是将其配置转换为图形并求解哈密顿量。然而,由于禁止“掉头”的约束,这类问题需要处理不雅的逻辑。
我在几分钟内破解了一个解决方案来显示问题。这是一种蛮力解决方案,将“房间”分组,并增加了不变量,即您不能在同一个房间中从一个“门”移动到另一个“门”(因为这需要进行 U 形转弯)。
我觉得必须有一个更好的抽象来表示这个问题,而不是诉诸以下“技巧”:
当路径刚刚来自那个房间时,有额外的逻辑来移除同一个房间中的门作为有效选择。
生成与输入房间配置不同的图形。
过滤所有不满足 U 形转弯约束的配置。(#1 的变体)
是否存在解决此类问题的现有文献,如果有,他们的结论是什么?房间问题是否与最著名的图算法所采用的方法根本不一致,以至于它需要这种特殊的逻辑?如果有更好的解决方案不是转换为图表,我也很想听听。
这是有效的现有代码,组代表第一个问题,注释掉的组代表后一个问题。:
// I renamed "groups" to rooms to make the code more clear.
var rooms = {
1: ['A','B','C','D'],
//1: ['A','B','C','D','P'],
2: ['E', 'D', 'F', 'G'],
3: ['F','I','J','H'],
//3: ['F','I','P','J', 'H'],
4: ['I', 'M', 'N', 'O'],
5: ['C','J','M','L','K'],
OUTER: ['A', 'B', 'E', 'G', 'H', 'O', 'N', 'L', 'K']
}
class Graph {
constructor(rooms) {
// This is a map of a door letter to the rooms (rooms) that it belongs to.
this.roomKey = {};
// The total number of doors
this.totalNodes = 0;
this.rooms = rooms;
// This is only used to produce the number of rooms, but remains in case
// I need to adapt the algorithm for the classical approach.
this.vertices = {};
for (var key in rooms) {
this.addRoom(key, rooms[key]);
}
}
addRoom(roomName, elements) {
for (var from of elements) {
if (!this.roomKey[from]) {
// initialize
this.roomKey[from] = [roomName]
} else {
this.roomKey[from].push(roomName)
}
for (var to of elements) {
// it doesn't make sense to add a vertex to yourself
if (from === to) continue
// otherwise add the vertex
this.addDoor(from, to)
}
}
}
addDoor(name, edge) {
// initialize if empty
if (!this.vertices[name]) {
this.vertices[name] = []
this.totalNodes++
}
if (this.vertices[name].indexOf(edge) != -1) {
console.log(`${name} already has this edge: ${edge}`)
} else {
this.vertices[name] = this.vertices[name].concat(edge)
}
}
hamiltonian(current, prevRoom, used) {
// Find the rooms that this connects to
var kpossible = this.roomKey[current]
// Find the rooms that connect to this door, but filter those that are
// in the room we just came from, this is the hacky part.
var possibleRoom = kpossible.find((room) => room !== prevRoom)
// Produce all possible rooms, but if we've already been to a room, remove it.
var possibleDoors = this.rooms[possibleRoom].filter((elt) => used.indexOf(elt) == -1)
if (used.length == this.totalNodes) {
console.log("success!", used)
return;
}
// No more possible rooms, this path is no good.
if (!possibleDoors || possibleDoors.length === 0)
return;
for(var door of possibleDoors) {
this.hamiltonian(door, possibleRoom, used.concat(door))
}
}
}