九宫重排_康拓展开_bfs
我们把第一个图的局面记为:12345678.
把第二个图的局面记为:123.46758
显然是按从上到下,从左到右的顺序记录数字,空格记为句点。
本题目的任务是已知九宫的初态和终态,求最少经过多少步的移动可以到达。如果无论多少步都无法到达,则输出-1。
123.46758
46758123.
package study_code; import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner; public class 八数码 { static int[] vis = new int [900900]; //判断哈希值是否被访问
static int[] f = new int[9]; //保存阶乘
static int end; //最终状态的哈希值
static node start = new node();
static String resPath;
static int[][] dir = {{-1,0},{1,0},{0,-1},{0,1}};
static char[] dirc = {'U','D','L','R'};
static class node{
int loc; //记录当前空格位置 用一个数字记录 1-9
int s[]; //记录当前所有数字的位置
int hashVal; //当前状态的哈希值
String path = ""; //记录当前路径
public node() {
}
} public static void fac() {
f[0] = 1;
for (int i = 1; i < f.length; i++) {
f[i] = f[i-1] * i;
}
} //康拓展开
public static int getHash(int[] s) {
int res = 0;
for (int i = 0; i < s.length; i++) {
int index =0;
for (int j = i+1; j < s.length; j++) {
if (s[j]<s[i]) {
index++;
}
}
res += index*f[9-1-i];
}
return res;
} public static boolean bfs() {
Queue<node> q = new LinkedList<node>();
q.offer(start);
vis[start.hashVal] = 1;
while (!q.isEmpty()) {
node cur = q.poll();
vis[cur.hashVal] = 1;
if (cur.hashVal == end) {
resPath = cur.path;
return true;
}
int x =cur.loc/3;
int y =cur.loc%3;
for (int i = 0; i < 4; i++) {
int tx = x + dir[i][0];
int ty = y + dir[i][1];
if (tx<0||ty<0||tx>=3||ty>=3) {
continue;
}
node temp = new node();
temp.loc = tx*3+ty;
temp.path = cur.path+dirc[i];
temp.s = cur.s.clone();
//交换空格的位置
temp.s[cur.loc] = temp.s[temp.loc];
temp.s[temp.loc] = 0; temp.hashVal = getHash(temp.s);
if (vis[temp.hashVal] == 1) {
continue;
}
q.offer(temp);
vis[temp.hashVal] = 1;
}
}
return false; }
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
fac();
String t = sc.nextLine();
char[] c = t.toCharArray();
int[] n = new int[c.length];
for (int i = 0; i < c.length; i++) {
if (c[i] == '.') {
n[i] = 0;
start.loc = i;
}else {
n[i] = c[i] - '0';
}
}
start.s = n.clone();
start.hashVal = getHash(start.s);
t = sc.nextLine();
c = t.toCharArray();
n = new int[c.length];
for (int i = 0; i < c.length; i++) {
if (c[i] == '.') {
n[i] = 0;
}else {
n[i] = c[i] - '0';
}
}
end = getHash(n);
bfs();
System.out.println(resPath.length());
}
}
九宫重排_康拓展开_bfs的更多相关文章
- 九宫重拍(bfs + 康拓展开)
问题描述 如下面第一个图的九宫格中,放着 1~8 的数字卡片,还有一个格子空着.与空格子相邻的格子中的卡片可以移动到空格中.经过若干次移动,可以形成第二个图所示的局面. 我们把第一个图的局面记为:12 ...
- 【算法系列学习三】[kuangbin带你飞]专题二 搜索进阶 之 A-Eight 反向bfs打表和康拓展开
[kuangbin带你飞]专题二 搜索进阶 之 A-Eight 这是一道经典的八数码问题.首先,简单介绍一下八数码问题: 八数码问题也称为九宫问题.在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的 ...
- 【康拓展开】及其在求全排列第k个数中的应用
题目:给出n个互不相同的字符, 并给定它们的相对大小顺序,这样n个字符的所有排列也会有一个顺序. 现在任给一个排列,求出在它后面的第i个排列.这是一个典型的康拓展开应用,首先我们先阐述一下什么是康拓展 ...
- ACM/ICPC 之 BFS(离线)+康拓展开(TSH OJ-玩具(Toy))
祝大家新年快乐,相信在新的一年里一定有我们自己的梦! 这是一个简化的魔板问题,只需输出步骤即可. 玩具(Toy) 描述 ZC神最擅长逻辑推理,一日,他给大家讲述起自己儿时的数字玩具. 该玩具酷似魔方, ...
- ACM/ICPC 之 BFS(离线)+康拓展开 (HDU1430-魔板)
魔板问题,一道经典的康拓展开+BFS问题,为了实现方便,我用string类来表示字符串,此前很少用string类(因为不够高效,而且相对来说我对char数组的相关函数比较熟),所以在这里也发现了很多容 ...
- nyoj 139 我排第几个--康拓展开
我排第几个 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 现在有"abcdefghijkl”12个字符,将其所有的排列中按字典序排列,给出任意一种排列,说 ...
- bnuoj 1071 拼图++(BFS+康拓展开)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=1071 [题意]:经过四个点的顺逆时针旋转,得到最终拼图 [题解]:康拓展开+BFS,注意先预处理,得 ...
- hdu 1043 pku poj 1077 Eight (BFS + 康拓展开)
http://acm.hdu.edu.cn/showproblem.php?pid=1043 http://poj.org/problem?id=1077 Eight Time Limit: 1000 ...
- 8数码,欺我太甚!<bfs+康拓展开>
不多述,直接上代码,至于康拓展开,以前的文章里有 #include<iostream> #include<cstdio> #include<queue> using ...
随机推荐
- sql的where条件转换成mongdb筛选条件
解析字符串 filterModel1 and filterModel2 and (filterModel3 or filterModel4) 1.转换成mongo的筛选条件 /// <summa ...
- C#使用事件方式Winform窗体之间传值
[摘自:http://www.cnblogs.com/codeToUp/p/5371062.html] 工程的源代码地址:https://github.com/yes-or-no/WinFormTra ...
- Map详解
https://mp.weixin.qq.com/s/s4KLQyE5bY833kGzWl3vsg
- JavaScript提高容错的方式
项目环境为Java Web项目,前端多用jquery,记录碰到的JS提高容错的编写方式. 回调的数据为null,数据绑定过程报错,影响下面代码执行 这种情况一开始想到的是能不能改用前端框架来动态的对页 ...
- CRC32为例详细解析(菜鸟至老鸟进阶)
CRC-知识解析 cyclic redundancy check 写在前面的话: 之前在做学校项目的时候用到了CRC 原理,但在网上查找的过程中,发现讲解CRC知识的资源很多,但是对新手比较友好的.讲 ...
- leetcode第221题(最大正方形)的本地IDE实现及变形
问题描述: 在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积.PS:本文也对只包含0的最大正方形面积进行了运算 示例: 输入: 1 0 1 0 0 1 0 1 1 1 ...
- linux下 利用 rz 命令上传文件
1. 如何安装? 1)编译安装 root 账号登陆后,依次执行以下命令: # cd /tmp # wget http://www.ohse.de/uwe/releases/lrzsz-0.12.20 ...
- 6-2 python 操作数据库mysql
1.python操作MySQL的 查询(select操作) 步骤 import pymysql # 1.建立数据库连接 conn = pymysql.connect(host='118.24.3.40 ...
- JavaScript数组常用的方法
改变原数组: ※ push,pop,shif,unshift,sort,reverse ※ splice 不改变原数组: ※ concat,join→split,toString,slice push ...
- PHP 多字节处理函数 mb_strlen
一.前言 个人认为,PHP是世界上最好的语言. 二.介绍 查看yii2底层源码, 发现 mb_strlen($str, '8bit') , 此函数的不是PHP的核心函数, 所以需要开启对应的扩展. ...