http://www.bnuoj.com/bnuoj/problem_show.php?pid=1071

【题意】:经过四个点的顺逆时针旋转,得到最终拼图

【题解】:康拓展开+BFS,注意先预处理,得到所有状态,然后用hash来调用存在的状态

【code】:

 #include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h> using namespace std;
#define N 363000 struct Nod
{
int b[];
int pos;
}nd1,nd2; int fac[] = {,,,,,,,,,}; //康拓展开用到的数组
//康托展开:
int cantor(int* a, int k)
{
int i, j, tmp, num = ;
for (i = ; i < k; i++) {
tmp = ;
for (j = i + ; j < k; j++)
if (a[j] < a[i])
tmp++;
num += fac[k - i - ] * tmp;
}
return num;
} int mark[N],pre[N];
char dir[N];
int cx[]={,,,};
int cy[]={,,,}; void exchange(int *a,int x,int y,int n) //旋转
{
int temp;
if(n%)
{
temp = a[*x+y];
a[*x+y] = a[*x+y+];
a[*x+y+] = a[*(x+)+y+];
a[*(x+)+y+] = a[*(x+)+y];
a[*(x+)+y] = temp;
}
else
{
temp = a[*(x+)+y];
a[*(x+)+y] = a[*(x+)+y+];
a[*(x+)+y+] = a[*x+y+];
a[*x+y+] = a[*x+y];
a[*x+y] = temp;
}
} void bfs(int *b) //广度优先搜索
{
queue<Nod> q;
memset(mark,,sizeof(mark));
memset(pre,,sizeof(pre));
memcpy(nd1.b,b,sizeof(int)*);
int i,temp;
temp = cantor(b,);
mark[temp] = ;
nd1.pos = temp;
q.push(nd1);
while(!q.empty())
{
nd2 = q.front();
q.pop();
for(i=;i<;i++)
{
memcpy(nd1.b,nd2.b,sizeof(int)*);
exchange(nd1.b,cx[i/],cy[i/],i);
temp = cantor(nd1.b,);
if(mark[temp]!=)
{
mark[temp] = ;
pre[temp] = pre[nd2.pos]+;
nd1.pos = temp;
q.push(nd1);
}
}
}
} int main()
{
char str[];
int a[],b[]={,,,,,,,,},c[],hash[];
bfs(b);
int t;
scanf("%d",&t);
while(t--)
{
int i;
for(i=;i<;i++)
{
scanf("%d",a+i);
hash[a[i]] = i+; //hash
}
for(i=;i<;i++)
{
scanf("%d",c+i);
c[i] = hash[c[i]];
}
int temp = cantor(c,);
printf("Number Of Move(s) Needed: %d\n",pre[temp]);
}
return ;
}

bnuoj 1071 拼图++(BFS+康拓展开)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 九宫重拍(bfs + 康拓展开)

    问题描述 如下面第一个图的九宫格中,放着 1~8 的数字卡片,还有一个格子空着.与空格子相邻的格子中的卡片可以移动到空格中.经过若干次移动,可以形成第二个图所示的局面. 我们把第一个图的局面记为:12 ...

  4. 8数码,欺我太甚!<bfs+康拓展开>

    不多述,直接上代码,至于康拓展开,以前的文章里有 #include<iostream> #include<cstdio> #include<queue> using ...

  5. hdu-1043 bfs+康拓展开hash

    因为是计算还原成一种局面的最短步骤,应该想到从最终局面开始做bfs,把所有能到达的情况遍历一遍,把值存下来. bfs过程中,访问过的局面的记录是此题的关键,9*9的方格在计算过程中直接存储非常占内存. ...

  6. HDU 4531 bfs/康拓展开

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4531 吉哥系列故事——乾坤大挪移 Time Limit: 2000/1000 MS (Java/Othe ...

  7. cdoj 414 八数码 (双向bfs+康拓展开,A*)

    一道关乎人生完整的问题. DBFS的优越:避免了结点膨胀太多. 假设一个状态结点可以扩展m个子结点,为了简单起见,假设每个结点的扩展都是相互独立的. 分析:起始状态结点数为1,每加深一层,结点数An ...

  8. ACM/ICPC 之 BFS(离线)+康拓展开(TSH OJ-玩具(Toy))

    祝大家新年快乐,相信在新的一年里一定有我们自己的梦! 这是一个简化的魔板问题,只需输出步骤即可. 玩具(Toy) 描述 ZC神最擅长逻辑推理,一日,他给大家讲述起自己儿时的数字玩具. 该玩具酷似魔方, ...

  9. ACM/ICPC 之 BFS(离线)+康拓展开 (HDU1430-魔板)

    魔板问题,一道经典的康拓展开+BFS问题,为了实现方便,我用string类来表示字符串,此前很少用string类(因为不够高效,而且相对来说我对char数组的相关函数比较熟),所以在这里也发现了很多容 ...

随机推荐

  1. poj 1904 强连通分量

    思路:先有每个儿子向所有他喜欢的姑娘建边,对于最后给出的正确匹配,我们建由姑娘到相应王子的边.和某个王子在同一强连通分量,且王子喜欢的姑娘都是该王子能娶得.思想类似匈牙利算法求匹配的时候,总能找到增广 ...

  2. maven的一些常用配置

    配置JDK<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupI ...

  3. 浅谈call和apply的联系&区别&应用匹配

    call和apply的联系和区别在之前查过资料了解了一番,昨天晚上睡不着觉忽然想到了这个问题,发现对于他们的联系和区别理解的还是很模糊.看来还是欠缺整理,知识没有连贯起来.反思一二,详情如下: 1作用 ...

  4. ActiveMQ(5.10.0) - Building a custom security plug-in

    If none of any built-in security mechanisms works for you, you can always build your own. Though the ...

  5. Linux 命令 - free: 显示系统的内存信息

    命令格式 free [-b | -k | -m] [-o] [-s delay ] [-t] [-l] [-V] 命令参数 -b 显示内存的单位为 Byte. -k 显示内存的单位为 KB. -m 显 ...

  6. sql查询统计,根据新闻类别ID统计,没有数据显示0

    有两张表,新闻信息表MessageInfo和新闻类别表MessageType.表结构如下: 然后需要实现下面这种查询结果: 这个是我面试时遇到的,上面的新闻类型是乱写的.当时没有做出来,然后回来又研究 ...

  7. 动态磁盘恢复为基本磁盘--DiskGenius

    近日在老电脑中安装了Win8.1,想不到使用起来比Win7还流畅. 周末,手贱,由于C盘只有10GB,为主分区,D盘有40GB,为扩展分区,想要将C.D两个分区合二为一,在Win8.1的磁盘管理器中, ...

  8. XCode中的特殊快捷键图标

    ⌘——Command () ⌃ ——Control ⌥——Option (alt) ⇧——Shift ⇪——Caps Lock fn——功能键就是fn

  9. Xcode7主题路径

    // Xcode7主题路径~/Library/Developer/Xcode/UserData/FontAndColorThemes

  10. Normalize [ 浏览器渲染格式化 ]

    /*! normalize.css v3.0.2 | MIT License | git.io/normalize */ /** * 1. Set default font family to san ...