当前位置:网站首页>Solve the Final Fantasy 13-2 Clock Puzzle with DFS
Solve the Final Fantasy 13-2 Clock Puzzle with DFS
2022-08-09 02:37:00 【Lordaeron_ESZ】
最近在补 XGP in Final Fantasy13-2时,Encounter a clock puzzle,感觉挺有意思,Like trying to solve it with a search algorithm.
问题描述
如下图所示,有一个时钟,contains a node,Each node has a numerical ID,Players can initially choose a node arbitrarily,选择后,The node is eliminated and the pointer points to the node's location,According to the numeric value of this node n Split into two pointers that rotate clockwise and counterclockwise respectively n unit length.After that each time the player can only select the node pointed to by the pointer,Nodes are eliminated after selecting them,The two pointers are merged to point to the location of the selected node and split and rotated as described above,Players need to eliminate all nodes to win.
注:Players cannot select nodes that have already been eliminated,If the two pointers after splitting and rotating are both located at the position of the node that has been eliminated,则判定游戏失败.
算法思路
This problem is easy to think of to use深度优先搜索来解决,Choose a node to start with,as the first choice 12 Node at the o'clock position,(以下为了方便,Arrange positions in the clock by nodes n node n)该结点值为 5,After selecting, rotate clockwise and counterclockwise respectively to reach the node 5 和 结点 7,This creates two branches(Equivalent to the left and right subtrees of a binary tree),Select these two nodes respectively to continue the search,If the node reaches a node that has already been visited(That is, the node has been eliminated),Then the search in that direction is terminated,并进行回溯,Delete this node on the path,and restore the access flag.
If the number of nodes on the path has been reached 12,That is, all nodes are successfully eliminated,Then the path is a solution path,Save that result and backtrack to continue searching,until all possibilities have been tried,算法结束.
完整代码
#include<iostream>
#include<vector>
using namespace std;
class Solution {
private:
int n;
vector<int> path; // 搜索路径
vector<vector<int>> res; // 成功路径
void dfs(vector<int>& sequences, vector<bool>& visited, int ind) {
if (visited[ind]) {
// 已访问过
return;
}
path.emplace_back(ind);
visited[ind] = true;
if (path.size() == n) {
// All elements are successfully accessed
res.emplace_back(path);
}
int p1 = (ind - sequences[ind] + n) % n; // Subscript after rotation counterclockwise
int p2 = (ind + sequences[ind]) % n; // Subscript after clockwise rotation
if (p1 == p2) {
dfs(sequences, visited, p1);
}
else {
dfs(sequences, visited, p1);
dfs(sequences, visited, p2);
}
// 回溯
path.pop_back();
visited[ind] = false;
}
public:
vector<vector<int>> clockPuzzle(vector<int>& sequences) {
n = sequences.size();
vector<bool> visited;
for (int i = 0; i < n; ++i) {
// Pick a node as the starting node
visited.assign(n, false); // 重置visited数组
dfs(sequences, visited, i);
}
return res;
}
};
int main() {
Solution S;
vector<int> sequences = {
5,5,3,6,2,1,4,3,4,5,3,4 };
auto res = S.clockPuzzle(sequences);
for (const auto& re : res) {
for (const auto& r : re) {
cout << r << " ";
}
cout << endl << endl;
}
}
结果分析
Below are all the solution paths to the problem in the example graph,经验证,符合条件.
边栏推荐
- 最近看到很多人想自学或者报班但是不清楚如何选择,我今天就和大家说说
- 数字 06 verilog_关于异步FIFO
- Jenkins的环境部署,(打包、发布、部署、自动化测试)
- 中国SSD产业突围有多难?除了技术“瓶颈”还有哪里挑战?
- MT4 / MQL4 entry to the master of EA course lesson two - commonly used functions
- xml引配置文件
- The first lesson of HNUMSC-C language
- 2022/8/8 Competition thinking + state pressure dp
- What is the difference between a project manager and a product manager?
- MySQL/Oracle字符串分割
猜你喜欢
《独行月球》:独孤月的两次选择,让一个“中间人”成为大英雄
Flume (四) --------- Flume 企业开发案例
The most fierce "employee" in history, madly complaining about the billionaire boss Xiao Zha: So rich, he always wears the same clothes!
Jenkins配置钉钉通知
如何最大限度地减少企业受到供应链攻击的风险
接口自动化测试-接口封装思想
MT4/MQL4 Getting Started to Mastering EA Tutorial Lesson 1 - MQL Language Common Functions (1) OrderSend() Function
使用TensorRT对AlphaPose模型进行加速
数字 06 verilog_关于异步FIFO
用DFS解决最终幻想13-2时钟谜题
随机推荐
1261. 在受污染的二叉树中查找元素
【AspNetCore】实现JWT(使用Microsoft.AspNetCore.Authentication.JwtBearer)
按钮点击动画
快速乘写法
jmeter的websocket插件安装和使用方法
gpio子系统和pinctrl子系统(下)
高性能 MySQL(十二):分区表
Redis - 时间序列数据类型的保存方案和消息队列实现
2022年自然语言处理校招社招实习必备知识点盘点分享
2020.12.4 log
炫酷-轮播图-走马灯
10.1-----19. Delete the Nth node from the bottom of the linked list
2020.10.13 Development log
MT4/MQL4 entry to proficient foreign exchange EA tutorial Lesson 1 Getting to know MetaEditor
Cyclictest 简介 安装 测试
攀爬倒影发光方块
Likou Brush Question Record 1.5-----367. Valid perfect squares
Jenkins configuration nail notification
[TensorRT] 对UNet进行推理加速
物联网未来:未来五年的预期