题意大概就是八数码问题,只不过把空格的移动方式改变了:空格能够向前或向后移动一格或三格(循环的)。

分析:其实跟八数码问题差不多,用康托展开记录状态,bfs即可。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstdlib>
#define Mod 1000000007
#define SMod 10007
#define INint 2147483647
#define LL (0x3f3f3f3f3f3f3f3f)*2
#define ll long long
using namespace std;
#define N 500007 struct node
{
ll cantor,cost;
int pos;
bool operator <(const node& a)const
{
return cost > a.cost;
}
}S,E; priority_queue<node> que;
ll fact[] = {,,,,,,,,,,};
int dx[] = {,-,,-};
int a[],b[],Can[N][],vis[N];
ll ans,ch,cv; ll Cantor(int *a)
{
int i,j;
ll cnt;
ll res = ;
for(i=;i<;i++)
{
cnt = ;
for(j=i+;j<;j++)
if(a[i] > a[j])
cnt++;
res += cnt*fact[-i];
}
return res;
} void getcantor(ll cantor,int *a)
{
for(int i=;i<;i++)
Can[cantor][i] = a[i];
} void geta(ll cantor)
{
for(int i=;i<;i++)
a[i] = Can[cantor][i];
} void bfs()
{
while(!que.empty())
que.pop();
memset(vis,,sizeof(vis));
int i,j;
que.push(S);
//vis[S.cantor] = 1;
while(!que.empty())
{
node tmp = que.top();
que.pop();
ll cantor = tmp.cantor;
ll cost = tmp.cost;
int pos = tmp.pos;
if(vis[cantor])
continue;
vis[cantor] = ;
if(cost >= ans)
continue;
if(cantor == E.cantor)
{
ans = min(ans,cost);
break;
}
geta(cantor);
for(int k=;k<;k++)
{
int v = (pos+dx[k]+)%;
swap(a[v],a[pos]);
ll newcantor = Cantor(a);
if(vis[newcantor])
{
swap(a[v],a[pos]);
continue;
}
getcantor(newcantor,a);
swap(a[v],a[pos]);
node now;
now.cantor = newcantor;
now.pos = v;
if(k < )
now.cost = cost + ch;
else
now.cost = cost + cv;
if(now.cost >= ans)
continue;
//vis[newcantor] = 1;
que.push(now);
}
}
} int main()
{
int i,j;
while(scanf("%lld%lld",&ch,&cv) && (ch||cv))
{
for(i=;i<;i++)
{
scanf("%d",&a[i]);
if(a[i] == )
S.pos = i;
}
S.cantor = Cantor(a);
S.cost = ;
for(i=;i<;i++)
scanf("%d",&b[i]);
E.cantor = Cantor(b);
getcantor(S.cantor,a);
getcantor(E.cantor,b);
ans = (1LL<<);
bfs();
printf("%lld\n",ans);
}
return ;
}

UVALive 6665 Dragon’s Cruller --BFS,类八数码问题的更多相关文章

  1. HDU 1043 Eight (BFS&#183;八数码&#183;康托展开)

    题意  输出八数码问题从给定状态到12345678x的路径 用康托展开将排列相应为整数  即这个排列在全部排列中的字典序  然后就是基础的BFS了 #include <bits/stdc++.h ...

  2. BFS:八数码问题

    #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> ...

  3. 八数码问题+路径寻找问题+bfs(隐式图的判重操作)

    Δ路径寻找问题可以归结为隐式图的遍历,它的任务是找到一条凑够初始状态到终止问题的最优路径, 而不是像回溯法那样找到一个符合某些要求的解. 八数码问题就是路径查找问题背景下的经典训练题目. 程序框架 p ...

  4. Poj 1077 eight(BFS+全序列Hash解八数码问题)

    一.题意 经典的八数码问题,有人说不做此题人生不完整,哈哈.给出一个含数字1~8和字母x的3 * 3矩阵,如: 1  2  X            3 4  6            7  5  8 ...

  5. 习题:八数码难题(双向BFS)

    八数码难题(wikioi1225) [题目描述] 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中.要求解的问题是:给出 ...

  6. BFS(八数码) POJ 1077 || HDOJ 1043 Eight

    题目传送门1 2 题意:从无序到有序移动的方案,即最后成1 2 3 4 5 6 7 8 0 分析:八数码经典问题.POJ是一次,HDOJ是多次.因为康托展开还不会,也写不了什么,HDOJ需要从最后的状 ...

  7. [cdoj1380] Xiper的奇妙历险(3) (八数码问题 bfs + 预处理)

    快要NOIP 2016 了,现在已经停课集训了.计划用10天来复习以前学习过的所有内容.首先就是搜索. 八数码是一道很经典的搜索题,普通的bfs就可求出.为了优化效率,我曾经用过康托展开来优化空间,甚 ...

  8. JAVA四类八种基本数据类型

    boolean类型 Boolean在内存中占用一个字节. 当java编译器把java源代码编译为字节码时,会用int或byte来表示boolean.在java虚拟机中,用整数零来表示false,用任意 ...

  9. 【算法】BFS+哈希解决八数码问题

    15拼图已经有超过100年; 即使你不叫这个名字知道的话,你已经看到了.它被构造成具有15滑动砖,每一个从1到15上,并且所有包装成4乘4帧与一个瓦块丢失.让我们把丢失的瓷砖“X”; 拼图的目的是安排 ...

随机推荐

  1. Oracle锁表与解锁

    查看锁表语句:方法1:    select sess.sid,           sess.serial#,           lo.oracle_username,           lo.o ...

  2. nginx模块开发(31)—定时器模型

    http://cjhust.blog.163.com/blog/static/175827157201348112639361/   Hello world 模块功能:注册一个定时事件,每过一秒钟打印 ...

  3. js中获取css属性

    直接获取 window.onload = function() { var but = document.getElementById('button'); var div = document.ge ...

  4. QT4/5中文乱码问题解决

    QT4 : QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8")); QT5: #if defined(_MSC_ ...

  5. Sharepoint学习笔记—习题系列--70-573习题解析 --索引目录

                  Sharepoint学习笔记—习题系列--70-573习题解析 为便于查阅,这里整理并列出了我前面播客中的关于70-573习题解析系列的所有问题,有些内容可能会在以后更新, ...

  6. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q127-Q130)

    Question 127You create a custom list named Products.You need to perform a Representational State Tra ...

  7. RecyclerView解析--onViewDetachedFromWindow()/onViewAttachedToWindow()

    先看这段源码介绍: /** * Called when a view created by this adapter has been detached from its window. * * &l ...

  8. 【读书笔记】iOS-装箱

    通常将一个基本类型的数据包装成对象叫做装箱,从对象中提取基本类型的数据叫做取消装箱.有些语言有自动装箱功能,它可以自动包装基本基础类型的数据,也可以自动从包装后的对象中提取基础数据.Objective ...

  9. iOS设计模式简介

    开闭原则: 一个模块的修改,对拓展开放而对修改关闭. 举个例子:有一个类在项目中很多地方被使用了,但是由于需求,想对这个类进行拓展,这里可以使用继承拓展出子类,可以对子类进行修改,尽量不要修改原来的类 ...

  10. linq扩展之动态排序

    前两天看QQ群里面,一位朋友问的问题,说在linq中怎么实现动态排序呢,自己想了半天,没有头绪,网上找了下相关的资料,看了下,收益挺多,记录下来. 之前我们没有如果不知道动态排序的方法的话,我们可能会 ...