8数码,欺我太甚!<bfs+康拓展开>
不多述,直接上代码,至于康拓展开,以前的文章里有
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
int fac[]={1,1,2,6,24,120,720,5040,40320,362880};//阶乘表
int dir[4][2]={1,0,0,1,-1,0,0,-1};//方向
int vis[362881];
int kangst,kanged;
int t1[3][3];
int t2[3][3];
pair<int,int>p;
struct node{
int maze[3][3];
pair<int,int>pos;
int kang;
int step;
};
int Kang_open (int t[3][3])
{
int s[9],num=0,k=0,sum=0;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
s[k++]=t[i][j];
for(int i=0;i<9;i++){
num=0;
for(int j=i+1;j<9;j++)
{
if(s[i]>s[j])
num++;
}
sum+=num*fac[8-i];
}
return sum;
}
int bfs()
{
node now;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
now.maze[i][j]=t1[i][j];
now.kang=kangst;
now.step=0;
now.pos=p;
vis[kangst]=1;
queue<node>que;
que.push(now);
while(!que.empty())
{
now=que.front();
que.pop();
if(now.kang==kanged)
return now.step;
node next=now;
for(int i=0;i<4;i++)
{
next=now;
next.pos.first = now.pos.first+dir[i][0];
next.pos.second= now.pos.second+dir[i][1];
if(next.pos.first >=0&&next.pos.first <3&&next.pos.second>=0&&next.pos.second<3)
{
next.maze[now.pos.first][now.pos.second]=now.maze[next.pos.first][next.pos.second];
next.maze[next.pos.first][next.pos.second]=0;
next.kang=Kang_open(next.maze);
if(!vis[next.kang])
{
vis[next.kang]=1;
next.step++;
que.push(next);
}
}
}
}
return -1;
}
int main ()
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++){
scanf("%d",&t1[i][j]);
if(t1[i][j]==0)
p.first=i,p.second=j;
}
kangst=Kang_open(t1);
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
scanf("%d",&t2[i][j]);
kanged=Kang_open(t2);
printf("%d\n",bfs());
return 0;
}
8数码,欺我太甚!<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 ...
- 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 ...
- HDU 4531 bfs/康拓展开
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4531 吉哥系列故事——乾坤大挪移 Time Limit: 2000/1000 MS (Java/Othe ...
- bnuoj 1071 拼图++(BFS+康拓展开)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=1071 [题意]:经过四个点的顺逆时针旋转,得到最终拼图 [题解]:康拓展开+BFS,注意先预处理,得 ...
- 九宫重拍(bfs + 康拓展开)
问题描述 如下面第一个图的九宫格中,放着 1~8 的数字卡片,还有一个格子空着.与空格子相邻的格子中的卡片可以移动到空格中.经过若干次移动,可以形成第二个图所示的局面. 我们把第一个图的局面记为:12 ...
- hdu-1043 bfs+康拓展开hash
因为是计算还原成一种局面的最短步骤,应该想到从最终局面开始做bfs,把所有能到达的情况遍历一遍,把值存下来. bfs过程中,访问过的局面的记录是此题的关键,9*9的方格在计算过程中直接存储非常占内存. ...
- cdoj 414 八数码 (双向bfs+康拓展开,A*)
一道关乎人生完整的问题. DBFS的优越:避免了结点膨胀太多. 假设一个状态结点可以扩展m个子结点,为了简单起见,假设每个结点的扩展都是相互独立的. 分析:起始状态结点数为1,每加深一层,结点数An ...
- hdu 1043 Eight (八数码问题)【BFS】+【康拓展开】
<题目链接> 题目大意:给出一个3×3的矩阵(包含1-8数字和一个字母x),经过一些移动格子上的数后得到连续的1-8,最后一格是x,要求最小移动步数. 解题分析:本题用BFS来寻找路径,为 ...
- ACM/ICPC 之 BFS(离线)+康拓展开 (HDU1430-魔板)
魔板问题,一道经典的康拓展开+BFS问题,为了实现方便,我用string类来表示字符串,此前很少用string类(因为不够高效,而且相对来说我对char数组的相关函数比较熟),所以在这里也发现了很多容 ...
随机推荐
- leetcode83,删除有序链表中的重复元素
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- FCFS
(First Come First Served) 按照作业进入系统的先后次序来挑选作业,先进入系统的作业优先被挑选. FCFS算法的优缺点: 算法容易实现.但效率不高,只顾及作业等候时间,没考虑作业 ...
- CSS样式与选择器
CSS构造块的样式: 1. h1{color:red;background-color:yellow} 其中:h1是选择器,花括号内是声明部分.多个声明之间用分号隔开. 2.为样式规则添加注释:/* ...
- hdu_1011_Starship Troopers(树形DP)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1011 题意:有N个房间,房间的连通性为树形的,就是说你要占领子结点,必须要先占领 父结点,每个房间有第 ...
- Linux系统故障处理案例(一)【转】
2016-08-05 14:41 运行环境:CentOS6.7 故障原因: 昨天在线执行命令yum -y update 在命令执行途中,强制中断并直接运行poweroff命令关机.再次开机出现如图所示 ...
- eclipse如何快速抽取样式(style)或者include
在视图模式上选中要抽取的模块,然后点击右键就可以抽取了
- 安卓之PreferenceActivity分享
PerferenceActivity是什么,看下面的截图: Android的系统截图 乐手设置截图 好了,我们看到Android系统本身就大量用到了PreferenceActivity来对系统进行信息 ...
- 根据View获取该控制器
//根据View获取控制器 - (UIViewController*)viewController { for (UIView* next = [self superview]; next; next ...
- 关于spring的注解方式注入默认值(转) -- 首字母小写
1.是首字母小写 比如 UserAction对应的id是userAction 可以通过ApplicationContext 对象的act.getBean("userAction") ...
- 【字母全排列】 poj 1256
深搜 注意与STL模版的去重函数唯一的区别就是有去重. #include <iostream> #include <cstdio> #include <string. ...