当前位置:网站首页>941 · Sliding Puzzles
941 · Sliding Puzzles
2022-08-10 06:50:00 【yinhua405】
In a block size is 2x3
的板上,有 5
block tile,分别用整数 1
到 5
表示,There is also an open space 0
表示.
One move representation 0
Swap places with a number in one of the four directions adjacent to it.
当且仅当 这块板
The tile placement status on [[1,2,3],[4,5,0]]
时,It can be said that the problem of this board has been solved.
Given a puzzle,Returns the minimum number of moves required,to resolve the state of the board.If unable to resolve the state of the board,则返回-1.
board
will be mentioned above2 x 3
The size is entered as an array .board[i][j]
会是[0, 1, 2, 3, 4, 5]
中的其中一个值.
样例
样例 1:
给出 board = `[[1,2,3],[4,0,5]]`, 返回 `1`.
解释:
交换0和5,只需要一步.
样例 2:
给出 board = `[[1,2,3],[5,4,0]]`, 返回 `-1`.
解释:
No matter how many steps you move, it won't solve the problem.
样例 3:
给出 board = `[[4,1,2],[5,0,3]]`, 返回 `5`.
解释:
至少需要移动5步来解决这个问题.
比如这么做:
移动 0 步之后: [[4,1,2],[5,0,3]]
移动 1 步之后: [[4,1,2],[0,5,3]]
移动 2 步之后: [[0,1,2],[4,5,3]]
移动 3 步之后: [[1,0,2],[4,5,3]]
移动 4 步之后: [[1,2,0],[4,5,3]]
移动 5 步之后: [[1,2,3],[4,5,0]]
样例 4:
给出 board = `[[3,2,4],[1,5,0]]`, 返回 `14`.
vector<vector<int>> canGoVec = { {1,3},{0,2,4},{1,5},{0,4},{1,3,5},{2,4} };
string NumberToString(int x) {
stringstream ss;
ss << x;
return ss.str();
}
string toString(vector<vector<int>> &board)
{
string ret = "";
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
ret.append(NumberToString(board[i][j]));
}
}
return ret;
}
int findStart(string &str)
{
int i = 0;
for (; i < 6; i++)
{
if (str[i] == '0')
{
return i;
}
}
}
void getAll(string &src, queue<string>&ret, set<string>&visSet)
{
int start = findStart(src);
string str = src;
vector<int> vec = canGoVec[start];
for (int i = 0; i < vec.size(); i++)
{
char c = str[start];
str[start] = str[vec[i]];
str[vec[i]] = c;
if (visSet.count(str))
{
str = src;
continue;
}
ret.push(str);
visSet.insert(str);
str = src;
}
}
int slidingPuzzle(vector<vector<int>> &board)
{
int count = 0;
string target = "123450";
set<string>visSet;
queue<string>goVec;
string start = toString(board);
getAll(start,goVec, visSet);
while (goVec.size()>0)
{
int size = goVec.size();
count++;
for (int i = 0; i < size; i++)
{
string tmp = goVec.front();
if (tmp == target)
{
return count;
}
getAll(tmp, goVec, visSet);
goVec.pop();
}
}
return -1;
}
边栏推荐
猜你喜欢
SCS【2】单细胞转录组 之 cellranger
高质量WordPress下载站模板5play主题
【8月9日活动预告】Prometheus峰会
Grammar Basics (Judgment Statements)
Fiddler(八) - 抓取手机APP的流量-插件Fiddler Orchestra Beta安装&配置
DGIOT支持工业设备租赁以及远程管控
Qt绘制椭圆曲线的角度问题(离心角和旋转角)
Qt借助隐藏控件和QSS绘制重复元素
High quality WordPress download station 5 play theme template
COLMAP+OpenMVS realizes 3D reconstruction mesh model of objects
随机推荐
浅谈C语言整型数据的存储
Parallax Mapping: More Realistic Texture Detail Representation (Part 1): Why Use Parallax Mapping
761. Special Binary Sequences
Qt列表下方增加弹出加载数据提示效果
pytest之parametrize参数化
深入理解数组
个人博客系统
Everyone, the default configuration of oracle cdc occasionally takes 30 seconds to capture data. How to optimize this?
Lunix(阿里云服务器)安装Anaconda并开启jupyter服务本地访问
Two-dimensional cartoon rendering - coloring
Qt中输入框在Win10上“Win+/“快捷键的一个Bug
MySQL事务隔离级别
ESP32 485风速
各位大佬,oracle11g,cdc2.2,flink1.13.6,单表增量同步。在没新增数据的情
WooCommerce 安装和 rest api 使用
强化学习_07_DataWhale深度Q网络进阶技巧
关于研究鼠标绘制平滑曲线的阶段总结
941 · 滑动拼图
[网络安全]实操AWVS靶场复现CSRF漏洞
Qt借助隐藏控件和QSS绘制重复元素