loj 1165(bfs+康托展开)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26879
思路:题目意思很简单,就是通过一些位置的交换,最后变成有序数列,对于一组序列,我们可以用康托展开然后hash判重。
然后就是普通的bfs,稍微留意一下细节即可。
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- #include<queue>
- using namespace std;
- int is_prime[]={,,,,,,,,,,,,,,,,,};
- int fac[]={,,,,,,,,};
- struct Point{
- int num,flag;
- };
- struct Node{
- Point state[];
- int step;
- }st;
- bool mark[];
- int Get_Hash(Node &node)
- {
- int a[],val=,cnt;
- for(int i=;i<;i++)a[i]=abs(node.state[i].num);
- for(int i=;i<;i++){
- cnt=;
- for(int j=;j<i;j++){
- if(a[j]>a[i])cnt++;
- }
- val+=cnt*fac[i];
- }
- return val;
- }
- bool Judge(Node &node)
- {
- for(int i=;i<;i++){
- if(abs(node.state[i].num)>abs(node.state[i+].num))
- return false;
- }
- return true;
- }
- Node Get_Node(Node &p,int pos1,int pos2,int dir)
- {
- //pos1->pos2 left
- if(dir==){
- if(pos1<pos2){
- int tmp=p.state[pos1].num,flag=p.state[pos1].flag;
- for(int i=pos1;i<=pos2-;i++){
- p.state[i].num=p.state[i+].num;
- p.state[i].flag=p.state[i+].flag;
- }
- p.state[pos2-].num=tmp;
- p.state[pos2-].flag=flag;
- }else {
- int tmp=p.state[pos1].num,flag=p.state[pos1].flag;
- for(int i=pos1;i>pos2;i--){
- p.state[i].num=p.state[i-].num;
- p.state[i].flag=p.state[i-].flag;
- }
- p.state[pos2].num=tmp,p.state[pos2].flag=flag;
- }
- }else if(dir==){ //pos1->pos2 right
- if(pos1<pos2){
- int tmp=p.state[pos1].num,flag=p.state[pos1].flag;
- for(int i=pos1;i<=pos2-;i++){
- p.state[i].num=p.state[i+].num;
- p.state[i].flag=p.state[i+].flag;
- }
- p.state[pos2].num=tmp,p.state[pos2].flag=flag;
- }else {
- int tmp=p.state[pos1].num,flag=p.state[pos1].flag;
- for(int i=pos1;i>=pos2+;i--){
- p.state[i].num=p.state[i-].num;
- p.state[i].flag=p.state[i-].flag;
- }
- p.state[pos2+].num=tmp,p.state[pos2+].flag=flag;
- }
- }
- return p;
- }
- void bfs()
- {
- memset(mark,false,sizeof(mark));
- queue<Node>que;
- que.push(st);
- mark[Get_Hash(st)]=true;
- while(!que.empty()){
- Node q,pp,p=que.front();
- que.pop();
- if(Judge(p)){
- printf("%d\n",p.step);
- return ;
- }
- for(int i=;i<;i++){
- for(int j=;j<;j++)if(i!=j){
- if(p.state[i].flag!=p.state[j].flag&&is_prime[abs(p.state[i].num)+abs(p.state[j].num)]){
- for(int k=;k<;k++){
- pp=p;
- q=Get_Node(pp,i,j,k);
- int val=Get_Hash(q);
- q.step=p.step+;
- if(!mark[val]){
- mark[val]=true;
- que.push(q);
- }
- }
- }
- }
- }
- }
- puts("-1");
- }
- int main()
- {
- int _case,t=;
- scanf("%d",&_case);
- while(_case--){
- for(int i=;i<;i++){
- scanf("%d",&st.state[i].num);
- st.state[i].flag=(st.state[i].num>?:-);
- }
- st.step=;
- printf("Case %d: ",t++);
- bfs();
- }
- return ;
- }
loj 1165(bfs+康托展开)的更多相关文章
- HDU_1043 Eight 【逆向BFS + 康托展开 】【A* + 康托展开 】
一.题目 http://acm.hdu.edu.cn/showproblem.php?pid=1043 二.两种方法 该题很明显,是一个八数码的问题,就是9宫格,里面有一个空格,外加1~8的数字,任意 ...
- hdu1430魔板(BFS+康托展开)
做这题先看:http://blog.csdn.net/u010372095/article/details/9904497 Problem Description 在魔方风靡全球之后不久,Rubik先 ...
- Aizu0121 Seven Puzzle(bfs+康托展开)
https://vjudge.net/problem/Aizu-0121 比八数码要水的多,bfs. 但是做的时候我把康托展开记错了,wa了好几次. 附上康托展开博客详解:https://blog.c ...
- 蓝桥杯 历届试题 九宫重排 (bfs+康托展开去重优化)
Description 如下面第一个图的九宫格中,放着 1~8 的数字卡片,还有一个格子空着.与空格子相邻的格子中的卡片可以移动到空格中.经过若干次移动,可以形成第二个图所示的局面. 我们把第一个图的 ...
- HDU 1043 Eight(双向BFS+康托展开)
http://acm.hdu.edu.cn/showproblem.php?pid=1043 题意:给出一个八数码,求出到达指定状态的路径. 思路:路径寻找问题.在这道题里用到的知识点挺多的.第一次用 ...
- hdu 1430(BFS+康托展开+映射+输出路径)
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- HDU_1430 魔板 【BFS+康托展开+置换】
一.题面 POJ1430 二.分析 该题与之前做的八数码不同,它是一个2*4的棋盘,并且没有空的区域.这样考虑的情况是很少的,依然结合康托展开,这时康托展开最多也只乘7的阶乘,完全可以BFS先预处理一 ...
- HDU - 1430 魔板 【BFS + 康托展开 + 哈希】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1430 思路 我刚开始 想到的 就是 康托展开 但是这个题目是 多组输入 即使用 康托展开 也是会T的 ...
- hdu 5012 bfs 康托展开
Dice Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submi ...
随机推荐
- QueryHelp
//辅助查询 Author:高兵兵 public class QueryHelp { #region IList<T> ToList<T>(string cmdText,str ...
- iOS多线程介绍
一.线程概述 有些程序是一条直线,起点到终点:有些程序是一个圆,不断循环,直到将它切断.直线的如简单的Hello World,运行打印完,它的生命周期便结束了,像昙花一现那样:圆如操作系统,一直运行直 ...
- mingw32-g++.exe: *: No such file or directory错误解决方法
初次使用CodeBlocks,好不容易把环境配好, 编译没有错误了,但是程序并不生成exe,提示以下问题: mingw32-g++.exe: /W3: No such file or director ...
- ldconfig deferred processing now taking place
在ubuntu下面安装软件,安装结束后,提示:ldconfig deferred processing now taking place 到网上查询了一下,大概意思是说:软件安装完了,是否要重启电脑.
- jQuery1.11源码分析(9)-----初始化jQuery对象的函数和关联节点获取函数
这篇也没什么好说的,初始化jQuery对象的函数要处理多种情况,已经被寒冬吐槽烂了.关联节点获取函数主要基于两个工具函数dir和sibling,前者基于指定的方向遍历,后者则遍历兄弟节点(真的不能合并 ...
- Run UliPad 4.1 Under Windows 7 64bit and wxPython 3.0.2
Abstract: UliPad that is developed by limodou is an excellent code editor. It works well with wxPyth ...
- Windows主机里利用VMware安装Linux(CentOS)虚拟机,Host-only连接上网方式详解
关于Host-only指的是主机与虚拟机之间的互联,因此虚拟机是不能连网的,若需要连网则需要使用NAT模式: Host-only模式实现联网得考虑如下配置过程: 附:VMware虚拟机三种网络模式(B ...
- HTML中的IE条件注释
IE条件注释是一种特殊的HTML注释,这种注释只有IE5.0及以上版本才能理解.比如普通的HTML注释是: <!--This is a comment--> 而只有IE可读的IE条件注释是 ...
- unity3d iPhone文件目录介绍
原地址:http://cl314413.blog.163.com/blog/static/190507976201210259126559/ 如何查看iPhone文件存放目录?首先需要越狱,越狱后打开 ...
- HDU 4435 charge-station bfs图论问题
E - charge-station Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...