九宫重拍(bfs + 康拓展开)
我们把第一个图的局面记为:12345678.
把第二个图的局面记为:123.46758
显然是按从上到下,从左到右的顺序记录数字,空格记为句点。
本题目的任务是已知九宫的初态和终态,求最少经过多少步的移动可以到达。如果无论多少步都无法到达,则输出-1。
123.46758
46758123.
#include<iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
typedef long long LL;
struct Node{
int cur[];
LL step;
};
Node s, e;
const int N = 1e6;
const int Next[][] = {{, }, {, }, {-, }, {, -}};//搜索的四个方向
bool vis[N * ];//标记数组
int fac[] = {, , , , , , , , , };//前几个数的阶乘
LL cantor(int s[])//康拓展开
{
LL ans = ;
int n = ;
for (int i = ; i < n - ; i++)
{
int tmp = ;
for (int j = i + ; j < n; j++)
if (s[j] < s[i])
tmp++;
ans += fac[n - i - ] * tmp;
}
return ans;
}
void cantor_reverse(int index, int a[])//康拓展开逆, 在本道题中未使用
{
index--;
int n = ;
bool visit[];
memset(visit, false, sizeof(visit));
for (int i = ; i < n; i++)
{
int tmp = index / fac[n - i - ];
for (int j = ; j <= tmp; j++)
if (visit[j])
tmp++;
a[i] = tmp + ;
visit[tmp] = true;
index %= fac[n - i - ];
}
}
bool ischecked(int row, int col)//检查是否满足移动的条件
{
return (row > && col > && row < && col < );
}
bool matched(Node node)//看是否达到给定的状态
{
for (int i = ; i < ; i++)
if (node.cur[i] != e.cur[i])
return false;
return true;
}
LL bfs()
{
memset(vis, false, sizeof(vis));
queue<Node> Q;
s.step = ;
Q.push(s);
Node p, q;
int start_num = cantor(s.cur);
vis[start_num] = true;//标记第一个元素
while (!Q.empty())
{
p = Q.front();
Q.pop();
int pos;
for (pos = ; pos < ; pos++)
if (p.cur[pos] == )//将"."当成9来计算
break;
int row, col, new_row, new_col;
row = pos / + ;
col = pos % + ;
for (int i = ; i < ; i++)
{
new_row = row + Next[i][];
new_col = col + Next[i][];
if (ischecked(new_row, new_col))//判断是否满足可移动的条件
{
q = p;
q.step = p.step + ;
//下面三步是交换这两个数(也就是移动到空位去)
int t = q.cur[(row - ) * + col - ];
q.cur[(row - ) * + col - ] = q.cur[(new_row - ) * + new_col - ];
q.cur[(new_row - ) * + new_col - ] = t;
if (matched(q))//如果找到之后直接返回
{
return q.step;
}
int num = cantor(q.cur);
if (!vis[num])
{
vis[num] = true;
Q.push(q);
}
}
}
}
return -;//找不到就返回-1
}
int main()
{
char sta[], en[];
scanf("%s %s", sta, en);
for (int i = ; i < ; i++)
if (sta[i] != '.')
s.cur[i] = sta[i] - '';
else
s.cur[i] = ;//将'.'看成9
for (int i = ; i < ; i++)
if (en[i] != '.')
e.cur[i] = en[i] - '';
else
e.cur[i] = ;
LL tmp = bfs();
printf("%lld\n", tmp); return ;
}
九宫重拍(bfs + 康拓展开)的更多相关文章
- Eight (HDU - 1043|POJ - 1077)(A* | 双向bfs+康拓展开)
The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've see ...
- 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 ...
- hdu-1043 bfs+康拓展开hash
因为是计算还原成一种局面的最短步骤,应该想到从最终局面开始做bfs,把所有能到达的情况遍历一遍,把值存下来. bfs过程中,访问过的局面的记录是此题的关键,9*9的方格在计算过程中直接存储非常占内存. ...
- HDU 4531 bfs/康拓展开
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4531 吉哥系列故事——乾坤大挪移 Time Limit: 2000/1000 MS (Java/Othe ...
- 蓝桥杯 历届试题 九宫重排 (bfs+康托展开去重优化)
Description 如下面第一个图的九宫格中,放着 1~8 的数字卡片,还有一个格子空着.与空格子相邻的格子中的卡片可以移动到空格中.经过若干次移动,可以形成第二个图所示的局面. 我们把第一个图的 ...
- cdoj 414 八数码 (双向bfs+康拓展开,A*)
一道关乎人生完整的问题. DBFS的优越:避免了结点膨胀太多. 假设一个状态结点可以扩展m个子结点,为了简单起见,假设每个结点的扩展都是相互独立的. 分析:起始状态结点数为1,每加深一层,结点数An ...
- 【算法系列学习三】[kuangbin带你飞]专题二 搜索进阶 之 A-Eight 反向bfs打表和康拓展开
[kuangbin带你飞]专题二 搜索进阶 之 A-Eight 这是一道经典的八数码问题.首先,简单介绍一下八数码问题: 八数码问题也称为九宫问题.在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的 ...
随机推荐
- Linux硬盘命名和安装分区
硬盘命名: 硬盘命名基于文件,一般有如下文件方式: /dev/hda1 /dev/sdb3 具体含义如下: /dev:是所有设备文件存放的目录. hd和sd:他们是区别的前两个字母,代表该分区所在的设 ...
- Linux下python3与python3的多版本共存
python3已经出来有些许时候了,python3相比python2进行了大量的改进,包括语法,新的功能,还有优化.虽然很多库已经同时支持 python2和python3了,但是有些库仍然没有很好的支 ...
- oracle创建表空间、用户、用户授权、删除表空间、删除用户
--创建临时表空间 create temporary tablespace test_temp --test_temp表空间名称 tempfile 'E:\oracle\product\10.2.0\ ...
- Css 描点
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- django自带User管理中添加自己的字段方法
#coding=utf-8 from django.db import models from django.contrib.auth.models import User, make_passwor ...
- Xcode7调试-b
Xcode7中苹果为我们增加了两个重要的debug相关功能.了解之后觉得非常实用,介绍给大家. 1.Address Sanitizer: 妈妈再也不用担心 EXC_BAD_ACCESS? EXC_BA ...
- Python三元表达式
我们知道Python没有三元表达式,但是我们通过技巧达到三元表达式的效果. 摘自<Dive Into Python>: 在Python 中,and 和 or 执行布尔逻辑演算,如你所期待的 ...
- 转:eclipse载入extjs4出现内存溢出错误的解决方法
去掉.project文件中的以下部分:第一部分:<buildCommand> <name>org.eclipse.wst.jsdt.core.javascriptVali ...
- Hive SQL运行状态监控(HiveSQLMonitor)
引言 目前数据平台使用Hadoop构建,为了方便数据分析师的工作,使用Hive对Hadoop MapReduce任务进行封装,我们面对的不再是一个个的MR任务,而是一条条的SQL语句.数据平台内部 ...
- C#获取字符串生成图片后的长度
1. 使用g.MeasureString()获得 使用MeasureString测量出来的字符宽度,总是比实际宽度大一些,而且随着字符的长度增大,貌似实际宽度和测量宽度的差距也越来越大了.查了一 ...