九宫重拍(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的 ...
随机推荐
- 【USACO 1.2.2】方块转换
[问题描述] 一块N x N(1<=N<=10)正方形的黑白瓦片的图案要被转换成新的正方形图案.写一个程序来找出将原始图案按照以下列转换方法转换成新图案的最小方式: 1:转90度:图案按顺 ...
- ubuntu系统mysql.h no such file or directory
在Ubuntu系统中,你已经安装了mysql,即你使用sudo apt-get install mysql-server mysql-client然而使用C语言访问mysql数据库时,却发现出现了如下 ...
- DEDE站点从网站根目录移到子目录
修改DedeCms系统配置参数-站点设置 a.站点根网址修改为:http://域名/子目录 b.网页主页链接:/子目录 修改DedeCms系统配置参数-核心设置 a.安装目录:/子目录 批量修改原数据 ...
- phonegap学习入门
phonegap 开发入门 PhoneGap官方网站上有详细的入门示例教程,这里,我针对使用PhoneGap进行Android移动应用的开发对其官网的Get Started进行一些介绍.补充. Ste ...
- JQuery Dialog(JS模态窗口,可拖拽的DIV) 效果实现代码
效果图 调用示意图 交互示意图 如上图所示,这基本是JQueryDialog的完整逻辑流程了. 1.用户点击模态窗口的“提交”按钮时,调用JQueryDialog.Ok()函数,这个函数对应了用户 ...
- JS学习之页面加载
1.window.opener.location.reload(); 意思是让打开的父窗口刷新.window.opener指的是本窗口的父窗口,window.opener.location.h ...
- 重构后的程序:通过rsync命令抓取日志文件
push.sh #!/bin/bash function push() { local ip=$ local user=$ local password=$ local path=$ local lo ...
- MySQL AB复制
http://tonychiu.blog.51cto.com/656605/326541
- NTOPNG,用来平时优化网站性能,用处大的
最近考察一下NTOPNG和NGX-REQ模块,看哪个对网站优化性能用户更大... 参考URL: http://www.68idc.cn/help/jiabenmake/qita/20150109164 ...
- Ural 1099 Work Scheduling
http://acm.timus.ru/problem.aspx?space=1&num=1099 题意:有n个人,很多对合作关系,每个人只能和一个人合作,求最多能选出多少人. 一般图匹配 # ...