滑动拼图 Sliding Puzzle
2018-09-09 22:01:02
问题描述:


问题求解:
问题很Interesting,其实本质就是解空间遍历,使用BFS就可以很快的予以解决~
public int slidingPuzzle(int[][] board) {
String goal = "123450";
String start = "";
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
start += (char)(board[i][j] + '0');
}
}
HashSet<String> set = new HashSet<>();
int[][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
set.add(start);
Queue<String> q = new LinkedList<>();
int step = 0;
if (start.equals(goal)) return step;
q.add(start);
while (!q.isEmpty()) {
step++;
int size = q.size();
for (int i = 0; i < size; i++) {
String cur = q.poll();
int idx = cur.indexOf('0');
int row = idx / 3;
int col = idx % 3;
for (int[] dir : dirs) {
int x = row + dir[0];
int y = col + dir[1];
if (x < 0 || x > 1 || y < 0 || y > 2) continue;
int idx2 = x * 3 + y;
StringBuffer sb = new StringBuffer(cur);
sb.setCharAt(idx, sb.charAt(idx2));
sb.setCharAt(idx2, '0');
String t = sb.toString();
if (t.equals(goal)) return step;
if (set.contains(t)) continue;
set.add(t);
q.add(t);
}
}
}
return -1;
}
滑动拼图 Sliding Puzzle的更多相关文章
- [Swift]LeetCode773. 滑动谜题 | Sliding Puzzle
On a 2x3 board, there are 5 tiles represented by the integers 1 through 5, and an empty square repre ...
- Leetcode之广度优先搜索(BFS)专题-773. 滑动谜题(Sliding Puzzle)
Leetcode之广度优先搜索(BFS)专题-773. 滑动谜题(Sliding Puzzle) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary ...
- python 游戏(滑动拼图Slide_Puzzle)
1. 游戏功能和流程图 实现16宫格滑动拼图,实现3个按钮(重置用户操作,重新开始游戏,解密游戏),后续难度,额外添加重置一次的按钮,解密算法的植入,数字改变为图片植入 游戏流程图 2. 游戏配置 配 ...
- 鸿蒙第三方组件——SwipeCaptcha滑动拼图验证组件
目录:1.组件效果展示2.Sample解析3.<鸿蒙第三方组件>系列文章合集 前言 基于安卓平台的滑动拼图验证组件SwipeCaptcha( https://github.com/mcxt ...
- 原生js+canvas实现滑动拼图验证码
上图为网易云盾的滑动拼图验证码,其应该有一个专门的图片库,裁剪的位置是固定的.我的想法是,随机生成图片,随机生成位置,再用canvas裁剪出滑块和背景图.下面介绍具体步骤. 首先随便找一张图片渲染到c ...
- 极验3.0滑动拼图验证的使用--java
[ 前言: 在登录其他网站的时候,看到有个滑动拼图的验证觉得挺好玩的,以前做一个图片验证的小demo,现在发现很多网站都开始流行滑动拼图的验证了,今天也想自己动手来弄一个. 废话不多说,开始撸起来! ...
- js实现滑动拼图验证码
js实现滑动拼图验证码,我这个样式是仿那些大网站做了, 学习用的,只用到前端. 小的个人网站感觉还可以用,大一点的别人用机器一下就破解了. 下面看图示: 样子大概是这样的. 源码在这 百度网盘: ...
- Flexbox + js实现滑动拼图游戏
滑动拼图就是把一张图片分成几等份,打乱顺序(下图),然后通过滑动拼凑成一张完整的图片. 要实现一个拼图游戏,需要考虑怎样随机的打乱顺序,怎样交换两张图片的位置,等等.但是,使用了Flexbox布局以后 ...
- leetcode 542. 01 Matrix 、663. Walls and Gates(lintcode) 、773. Sliding Puzzle 、803. Shortest Distance from All Buildings
542. 01 Matrix https://www.cnblogs.com/grandyang/p/6602288.html 将所有的1置为INT_MAX,然后用所有的0去更新原本位置为1的值. 最 ...
随机推荐
- Installing Moses on Ubuntu 16.04
Installing Moses on Ubuntu 16.04 The process of installation To install requirements sudo apt-get in ...
- Linux模拟僵尸进程并kill
模拟系统有僵尸进程后怎么解决 僵尸进程 #include <stdio.h> #include <sys/types.h> int main() { //fork a chil ...
- echarts自定义图例legend文字和样式
话不多说,先上效果图. 要完成这个图并不难,主要是下面那个图例比较难,需要定制. 让我们从官方文档找找思路,官方文档关于legend.formatter是这样的:链接在这 难点在于: 1.这里的图例文 ...
- Codeforces 839B Game of the Rows - 贪心
Daenerys Targaryen has an army consisting of k groups of soldiers, the i-th group contains ai soldie ...
- 0x17二叉堆之超市
题目链接:https://www.acwing.com/problem/content/147/ 容易想到一个贪心策略:在最优解中,对于每个时间(天数) t,应该在保证不卖出过期商品的前提下,尽量卖出 ...
- InstallShield安装包在Win7下权限问题的解决方案 (转载)
转载:http://blog.csdn.net/wuzhengqing1/article/details/6570149 转载:http://blog.csdn.net/brikoff/article ...
- 小白学习 Redis 数据库日记(2017-06-13)
redis 127.0.0.1:6379> LPUSH runoobkey redis(integer) 1redis 127.0.0.1:6379> LPUSH runoobkey mo ...
- android 6 中init.rc的生成过程【转】
本文转载自:https://blog.csdn.net/quhj/article/details/51819638 android 系统开机是会有一个初始化过程 init ,整个初始化过程是根据配置脚 ...
- eMMC之分区管理、总线协议和工作模式【转】
本文转载自:https://blog.csdn.net/u013686019/article/details/66472291 一.eMMC 简介 eMMC 是 embedded MultiMedia ...
- 3545: [ONTAK2010]Peaks 平衡树,最小生成树
链接 https://www.lydsy.com/JudgeOnline/problem.php?id=3545 离线询问,按照权值排个序 就是在克鲁斯卡尔时候维护个treap,到时候挨个查询一下就好 ...