UVALive 6665 Dragonâs Cruller --BFS,类八数码问题
题意大概就是八数码问题,只不过把空格的移动方式改变了:空格能够向前或向后移动一格或三格(循环的)。
分析:其实跟八数码问题差不多,用康托展开记录状态,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,类八数码问题的更多相关文章
- HDU 1043 Eight (BFS·八数码·康托展开)
题意 输出八数码问题从给定状态到12345678x的路径 用康托展开将排列相应为整数 即这个排列在全部排列中的字典序 然后就是基础的BFS了 #include <bits/stdc++.h ...
- BFS:八数码问题
#include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> ...
- 八数码问题+路径寻找问题+bfs(隐式图的判重操作)
Δ路径寻找问题可以归结为隐式图的遍历,它的任务是找到一条凑够初始状态到终止问题的最优路径, 而不是像回溯法那样找到一个符合某些要求的解. 八数码问题就是路径查找问题背景下的经典训练题目. 程序框架 p ...
- Poj 1077 eight(BFS+全序列Hash解八数码问题)
一.题意 经典的八数码问题,有人说不做此题人生不完整,哈哈.给出一个含数字1~8和字母x的3 * 3矩阵,如: 1 2 X 3 4 6 7 5 8 ...
- 习题:八数码难题(双向BFS)
八数码难题(wikioi1225) [题目描述] 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中.要求解的问题是:给出 ...
- BFS(八数码) POJ 1077 || HDOJ 1043 Eight
题目传送门1 2 题意:从无序到有序移动的方案,即最后成1 2 3 4 5 6 7 8 0 分析:八数码经典问题.POJ是一次,HDOJ是多次.因为康托展开还不会,也写不了什么,HDOJ需要从最后的状 ...
- [cdoj1380] Xiper的奇妙历险(3) (八数码问题 bfs + 预处理)
快要NOIP 2016 了,现在已经停课集训了.计划用10天来复习以前学习过的所有内容.首先就是搜索. 八数码是一道很经典的搜索题,普通的bfs就可求出.为了优化效率,我曾经用过康托展开来优化空间,甚 ...
- JAVA四类八种基本数据类型
boolean类型 Boolean在内存中占用一个字节. 当java编译器把java源代码编译为字节码时,会用int或byte来表示boolean.在java虚拟机中,用整数零来表示false,用任意 ...
- 【算法】BFS+哈希解决八数码问题
15拼图已经有超过100年; 即使你不叫这个名字知道的话,你已经看到了.它被构造成具有15滑动砖,每一个从1到15上,并且所有包装成4乘4帧与一个瓦块丢失.让我们把丢失的瓷砖“X”; 拼图的目的是安排 ...
随机推荐
- PHP学习笔记:对命名空间(namespace)学习资料的翻译
Name collisions means: you create a function named db_connect, and somebody elses code that you use ...
- 【OpenCV】OpenCV中GPU模块使用
CUDA基本使用方法 在介绍OpenCV中GPU模块使用之前,先回顾下CUDA的一般使用方法,其基本步骤如下: 1.主机代码执行:2.传输数据到GPU:3.确定grid,block大小: 4.调用内核 ...
- [Android] 环境配置之基础开发环境(SDK/Android Studio)(转)
[Android] 环境配置之基础开发环境(SDK/Android Studio) 博客: blog.csdn.net/qiujuer 网站: www.qiujuer.net 开源库: Geniu ...
- webpack常用的插件安装命令
webpack常用的插件安装命令:1:npm install html-webpack-plugin --save-dev //自动快速的帮我们生成HTML.2:npm install css-loa ...
- Android Activity/Service/Broadcaster三大组件之间互相调用
我们研究两个问题,1.Service如何通过Broadcaster更改activity的一个TextView.(研究这个问题,考虑到Service从服务器端获得消息之后,将msg返回给activity ...
- SharePoint 2010 文档管理系列之准备篇
前言:很早自己就想写一个系列的文章,但是不知道写什么,最近在QQ群里,好多人说在做文档管理,其实文档管理也是SharePoint的一个很不错的功能点,自己想了想,也想多学习点东西,所以写这个主题吧,今 ...
- SPServices.SPDisplayRelatedInfo
Function $().SPServices.SPDisplayRelatedInfo Certification Functionality SPDisplayRelatedInfo is a f ...
- 结合源代码详解android消息模型
Handler是整个消息系统的核心,是Handler向MessageQueue发送的Message,最后Looper也是把消息通知给Handler,所以就从Handler讲起. 一.Handler H ...
- Python数据结构与算法--List和Dictionaries
Lists 当实现 list 的数据结构的时候Python 的设计者有很多的选择. 每一个选择都有可能影响着 list 操作执行的快慢. 当然他们也试图优化一些不常见的操作. 但是当权衡的时候,它们还 ...
- layout_weight的使用说明
近期学习了Mars老师的视频,看了十二课的有关layout_weight的讲解,就做了些总结. layout_weight用于分配剩余的布局空间. 首先,先看段代码,它定义了两个textview控件 ...