汉诺塔

高级

这是一个Engineering、Multimodal AI领域的自动化工作流,包含 21 个节点。主要使用 If、Set、Code、ManualTrigger、ExecuteWorkflow 等节点。 使用子工作流实现递归算法:汉诺塔演示

前置要求
  • 无特殊前置要求,导入即可使用
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "id": "VPnclcR3gHg85eOV",
  "meta": {
    "instanceId": "1ee7910f98ee64be6f03d5c49b072eda83992583a161149dc94314ed0b0a2fe0"
  },
  "name": "汉诺塔",
  "tags": [],
  "nodes": [
    {
      "id": "d0112e92-b3e0-4d97-b06d-382470864364",
      "name": "设置圆盘数量",
      "type": "n8n-nodes-base.set",
      "position": [
        912,
        1664
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "c775d359-9599-494b-918c-3e46aae9274c",
              "name": "numberOfDiscs",
              "type": "number",
              "value": 4
            }
          ]
        }
      },
      "notesInFlow": true,
      "typeVersion": 3.4
    },
    {
      "id": "b76775a3-9cf4-4cb5-9945-4cb7d2df7436",
      "name": "最多1个圆盘",
      "type": "n8n-nodes-base.if",
      "position": [
        752,
        1984
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "26c868df-4677-4ded-9ba5-7a69fc3cfd06",
              "operator": {
                "type": "number",
                "operation": "lte"
              },
              "leftValue": "={{ $json.numberOfDiscs }}",
              "rightValue": 1
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "a4ce4e6c-cc21-43f3-b1ae-04768a1abee3",
      "name": "A B C",
      "type": "n8n-nodes-base.executeWorkflow",
      "position": [
        1328,
        1664
      ],
      "parameters": {
        "options": {
          "waitForSubWorkflow": true
        },
        "workflowId": {
          "__rl": true,
          "mode": "list",
          "value": "VPnclcR3gHg85eOV",
          "cachedResultName": "Towers of Hanoi"
        },
        "workflowInputs": {
          "value": {
            "logs": "={{ $json.logs }}",
            "stackX": "={{ $json.stackA }}",
            "stackY": "={{ $json.stackB }}",
            "stackZ": "={{ $json.stackC }}",
            "numberOfDiscs": "={{ $json.numberOfDiscs }}"
          },
          "schema": [
            {
              "id": "numberOfDiscs",
              "type": "number",
              "display": true,
              "required": false,
              "displayName": "numberOfDiscs",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "stackX",
              "type": "object",
              "display": true,
              "required": false,
              "displayName": "stackX",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "stackY",
              "type": "object",
              "display": true,
              "required": false,
              "displayName": "stackY",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "stackZ",
              "type": "object",
              "display": true,
              "required": false,
              "displayName": "stackZ",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "logs",
              "type": "array",
              "display": true,
              "required": false,
              "displayName": "logs",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": true
        }
      },
      "notesInFlow": true,
      "typeVersion": 1.2
    },
    {
      "id": "5ef7690f-7554-47f5-8124-562edbba5a86",
      "name": "X Y Z",
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "position": [
        640,
        1984
      ],
      "parameters": {
        "workflowInputs": {
          "values": [
            {
              "name": "numberOfDiscs",
              "type": "number"
            },
            {
              "name": "stackX",
              "type": "object"
            },
            {
              "name": "stackY",
              "type": "object"
            },
            {
              "name": "stackZ",
              "type": "object"
            },
            {
              "name": "logs",
              "type": "array"
            }
          ]
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "be89d88c-9814-440b-8d49-99fe49e65a4b",
      "name": "X 到 Z",
      "type": "n8n-nodes-base.code",
      "position": [
        1232,
        1968
      ],
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "// Move disc\nconst disc = $json.stackX.items.pop();\n$json.stackZ.items.push(disc);\n\n// Log\n$json.logs.push($json.stackX.name + \" \" + $json.stackZ.name + \" \" + disc);\n\nreturn $input.item;"
      },
      "typeVersion": 2
    },
    {
      "id": "81ad6f90-0689-43e9-b67b-911c91dade6a",
      "name": "X 到 Z",
      "type": "n8n-nodes-base.code",
      "position": [
        1232,
        2112
      ],
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "// Move disc\nconst disc = $json.stackX.items.pop();\n$json.stackZ.items.push(disc);\n\n// Log\n$json.logs.push($json.stackX.name + \" \" + $json.stackZ.name + \" \" + disc);\n\nreturn $input.item;"
      },
      "typeVersion": 2
    },
    {
      "id": "e9430884-2810-475f-90d2-9cffcfbaf9c8",
      "name": "更新",
      "type": "n8n-nodes-base.code",
      "position": [
        1072,
        2112
      ],
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "// Reset number of discs\n$json.numberOfDiscs += 1;\n\n// Reset stacks (XZY to XYZ)\nconst stack = $json.stackY;\n$json.stackY = $json.stackZ;\n$json.stackZ = stack;\n\nreturn $input.item;"
      },
      "typeVersion": 2
    },
    {
      "id": "ef117924-272e-4eb6-b098-f1a909ae0334",
      "name": "更新",
      "type": "n8n-nodes-base.code",
      "position": [
        1520,
        2112
      ],
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "// Reset number of discs\n$json.numberOfDiscs += 1;\n\n// Reset stacks (YXZ to XYZ)\nconst stack = $json.stackY;\n$json.stackY = $json.stackX;\n$json.stackX = stack;\n\nreturn $input.item;"
      },
      "typeVersion": 2
    },
    {
      "id": "a3f48f91-44f9-41c2-963d-51c4d2f4e21d",
      "name": "创建堆栈",
      "type": "n8n-nodes-base.code",
      "position": [
        1120,
        1664
      ],
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "// Really check, if you want to waste computing energy. Recursion errors are hard enough. Best regards, Adrian\nconst min = 1;\nconst max = 5;\nif ($json.numberOfDiscs < min) {\n  $json.numberOfDiscs = min;\n}\nif ($json.numberOfDiscs > max) {\n  $json.numberOfDiscs = max;\n}\n\n// Create stack items\nlet items = [];\nfor (let i=$json.numberOfDiscs; i>0; i--) {\n  items.push(i);\n}\n\n// Set data\n$json.stackA = {};\n$json.stackA.name = \"A\";\n$json.stackA.items = items;\n$json.stackB = {};\n$json.stackB.name = \"B\";\n$json.stackB.items = [];\n$json.stackC = {};\n$json.stackC.name = \"C\";\n$json.stackC.items = [];\n$json.logs = [];\n\n// Return this n8n item\nreturn $input.item;"
      },
      "typeVersion": 2
    },
    {
      "id": "ade72738-15aa-4bcf-b98c-d4804eeb949e",
      "name": "X Z Y",
      "type": "n8n-nodes-base.executeWorkflow",
      "position": [
        944,
        2112
      ],
      "parameters": {
        "options": {},
        "workflowId": {
          "__rl": true,
          "mode": "list",
          "value": "VPnclcR3gHg85eOV",
          "cachedResultName": "Towers of Hanoi"
        },
        "workflowInputs": {
          "value": {
            "logs": "={{ $json.logs }}",
            "stackX": "={{ $json.stackX }}",
            "stackY": "={{ $json.stackZ }}",
            "stackZ": "={{ $json.stackY }}",
            "numberOfDiscs": "={{ $json.numberOfDiscs - 1 }}"
          },
          "schema": [
            {
              "id": "numberOfDiscs",
              "type": "number",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "numberOfDiscs",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "stackX",
              "type": "object",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "stackX",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "stackY",
              "type": "object",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "stackY",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "stackZ",
              "type": "object",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "stackZ",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "logs",
              "type": "array",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "logs",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": true
        }
      },
      "notesInFlow": true,
      "typeVersion": 1.2
    },
    {
      "id": "69fe66b5-2244-4ba8-a22f-507613f53f0b",
      "name": "Y X Z",
      "type": "n8n-nodes-base.executeWorkflow",
      "position": [
        1392,
        2112
      ],
      "parameters": {
        "options": {
          "waitForSubWorkflow": true
        },
        "workflowId": {
          "__rl": true,
          "mode": "list",
          "value": "VPnclcR3gHg85eOV",
          "cachedResultName": "Towers of Hanoi"
        },
        "workflowInputs": {
          "value": {
            "logs": "={{ $json.logs }}",
            "stackX": "={{ $json.stackY }}",
            "stackY": "={{ $json.stackX }}",
            "stackZ": "={{ $json.stackZ }}",
            "numberOfDiscs": "={{ $json.numberOfDiscs - 1 }}"
          },
          "schema": [
            {
              "id": "numberOfDiscs",
              "type": "number",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "numberOfDiscs",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "stackX",
              "type": "object",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "stackX",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "stackY",
              "type": "object",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "stackY",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "stackZ",
              "type": "object",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "stackZ",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "logs",
              "type": "array",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "logs",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": true
        }
      },
      "notesInFlow": true,
      "typeVersion": 1.2
    },
    {
      "id": "01a8c211-6e7f-4ef2-a651-84ed77de8390",
      "name": "解决方案",
      "type": "n8n-nodes-base.code",
      "position": [
        1536,
        1664
      ],
      "parameters": {
        "jsCode": "function logsToSentence(logs) {\n  return logs.map((entry, index) => {\n    const [from, to, disc] = entry.split(\" \");\n    let text;\n\n    if (index === 0) {\n      text = `Move disc ${disc} from ${from} to ${to}`;\n    } else if (index === 1) {\n      text = `then ${from} ${disc}→ ${to}`;\n    } else {\n      text = `${from} ${disc}→ ${to}`;\n    }\n\n    text += \",\";\n\n    if (index === 0 || index % 2 === 1) {\n      text += \"\\n\";\n    } else {\n      text += \" \";\n    }\n\n    return text;\n  }).join(\"\").slice(0, -2) + \".\";\n}\n\nreturn { \"solution\" : logsToSentence($input.first().json.logs) };"
      },
      "typeVersion": 2
    },
    {
      "id": "4f5aeb9a-37e0-4a02-9c0d-35dfe57cf70b",
      "name": "用户输入",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        864,
        1360
      ],
      "parameters": {
        "color": 7,
        "width": 192,
        "height": 448,
        "content": "### 输入"
      },
      "typeVersion": 1
    },
    {
      "id": "1633c56d-628c-4976-b00a-f2322bffc46f",
      "name": "设置",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1072,
        1360
      ],
      "parameters": {
        "color": 7,
        "width": 192,
        "height": 448,
        "content": "### 设置"
      },
      "typeVersion": 1
    },
    {
      "id": "187388ec-ec1c-4ee9-acf2-cad916f301b6",
      "name": "结果",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1280,
        1360
      ],
      "parameters": {
        "color": 7,
        "width": 192,
        "height": 448,
        "content": "### 结果"
      },
      "typeVersion": 1
    },
    {
      "id": "3a038abe-5ee2-4875-898a-d92910fc343a",
      "name": "解法",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1488,
        1360
      ],
      "parameters": {
        "color": 7,
        "width": 208,
        "height": 448,
        "content": "### 解法"
      },
      "typeVersion": 1
    },
    {
      "id": "2cdfe45e-3284-46b9-933c-c399ed0abfdc",
      "name": "交换",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        864,
        1840
      ],
      "parameters": {
        "color": 7,
        "width": 198,
        "height": 416,
        "content": "### 节点输入"
      },
      "typeVersion": 1
    },
    {
      "id": "f4cef2e4-e803-4b61-b197-56b8ef90685a",
      "name": "移动",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1184,
        1840
      ],
      "parameters": {
        "color": 7,
        "width": 192,
        "height": 416,
        "content": "### 节点步骤"
      },
      "typeVersion": 1
    },
    {
      "id": "b4ad8c17-ff19-4cd6-a848-207ec67918e1",
      "name": "更新",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1504,
        1840
      ],
      "parameters": {
        "color": 7,
        "width": 192,
        "height": 416,
        "content": "### 节点步骤"
      },
      "typeVersion": 1
    },
    {
      "id": "89a9c41f-42c4-48c1-a187-a90a795f7bdd",
      "name": "开始",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        640,
        1664
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "9baa2d1d-6d3b-4d8f-9d95-db479479a4e2",
      "name": "汉诺塔",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        0,
        1360
      ],
      "parameters": {
        "color": 5,
        "width": 576,
        "height": 1024,
        "content": "## 汉诺塔"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "50e00ac4-9c41-4e49-9390-4f196d97ad1a",
  "connections": {
    "A B C": {
      "main": [
        [
          {
            "node": "Solution",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Start": {
      "main": [
        [
          {
            "node": "Set number of discs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "X Y Z": {
      "main": [
        [
          {
            "node": "Max 1 disc",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "X Z Y": {
      "main": [
        [
          {
            "node": "Update",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Y X Z": {
      "main": [
        [
          {
            "node": " Update",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update": {
      "main": [
        [
          {
            "node": " X to Z",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    " X to Z": {
      "main": [
        [
          {
            "node": "Y X Z",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Max 1 disc": {
      "main": [
        [
          {
            "node": "X to Z",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "X Z Y",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create stacks": {
      "main": [
        [
          {
            "node": "A B C",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set number of discs": {
      "main": [
        [
          {
            "node": "Create stacks",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

这是一个高级难度的工作流,适用于Engineering、Multimodal AI等场景。适合高级用户,包含 16+ 个节点的复杂工作流

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
高级
节点数量21
分类2
节点类型7
难度说明

适合高级用户,包含 16+ 个节点的复杂工作流

作者
Adrian

Adrian

@adrian

AI Software & Platform Engineer. Senior Technical Consultant, Associate Researcher (Data Science & Computer Science Education), Project Engineer, Master of Computer Science (M.Sc.)

外部链接
在 n8n.io 上查看 →

分享此工作流