九宫重拍(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的 ...
随机推荐
- Android JNI 之 环境安装
在配置环境之前,我们得了解 JNI 和NDK JNI JNI是Java Native Interface的缩写,中文为JAVA本地调用.它提供了若干的API实现了和Java和其他语言的通信(主要是C& ...
- Navicat 选择语句
1.进入数据库后,点击Query 2.点击new query 3.左边提供界面的筛选条件,如果不清楚sql语句,可直接在上面操作 4.右边可自己编写sql语句 5.写完语句后,点击Run,在resul ...
- mysql 数据库查询与实例。
资料是从教材弄下来的,加上了我的理解.主要内容是练习实例,在写博文中学习命令行,当然也希望这篇博文能帮助其他人学习mysq数据库命令 SELECT 语句可以从一个或多个表中选取特定的行和列 SELEC ...
- 关于一个注册邮箱的demo
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- 3.5 linux 0.11 目标文件格式
在Linux0.11系统中,GNU gcc或gas编译输出的目标模块文件和链接程序生成的可执行文件都使用了UNIX传统的a.out格式.这是一种被称为汇编与链接输出(Assembly & li ...
- js循环便利json数据
var data=[{name:"a",age:12},{name:"b",age:11},{name:"c",age:13},{name: ...
- python模块之time和datetime
33.python模块之time 1.>>> time.time() 1470900847.8458395 ==>时间戳,从1970年到现在. 2.> ...
- 探究Android中Listview显示错乱问题
问题 最近在项目中遇到过一个很棘手的问题,就是ListView在滑动后就莫名其妙的显示错乱,网上查阅资料后问题很容易的就解决了,但是对于问题产生的原因仍是一知半解,所以不甘心的我定下心来,狠读源码,终 ...
- C语言字符数组越界现象
今天在用C的过程中发现一个奇怪的现象.代码如下: ]; chs[] = 'a'; chs[] = 'b'; printf(]); 结果 输出 是 a. 在网上查了一下.有个网友是这样回答的: “ 我 ...
- PHP 5.6正式发布:新特性、及功能改进介绍
经过了长时间的开发测试,新版本PHP程序(PHP5.6正式版)终于发布了.新版本中加入了一些实用的新特性,也摒弃了一些冗余的功能.同时,也对部分原有功能进行了改进.下面就一起看看PHP 5.6正式版到 ...