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数组的相关函数比较熟),所以在这里也发现了很多容 ...
随机推荐
- 笔记整理--Linux守护进程
Linux多进程开发(三)进程创建之守护进程的学习 - _Liang_Happy_Life__Dream - 51CTO技术博客 - Google Chrome (2013/10/11 16:48:2 ...
- android命令行网络时间同步
一.简介 Android基于Linux平台的开源手机操作系统. 二.原理 既然是Linux,那就应该支持linux的各种命令行,高度的可配置,但实验发现Android是Google的一个高度阉割版的l ...
- TCP/IP,http,socket,长连接,短连接
TCP/IP TCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层. 在网络层有IP协议.ICMP协议.ARP协议.RARP协议和BOOTP协议. 在传输层中有TCP协议与UDP协议. 在应 ...
- MFC中获取系统当前时间
1.使用CTime类 CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime(); str=tm.Format("现在时间是%Y年% ...
- debian上安装lua编辑器
Debian服务器上安装lua 1)下载压缩包 wget http://www.lua.org/ftp/lua-5.1.4.tar.gz 2)解压文件 tar zxvf lua-5.1.4.tar. ...
- centos装openoffice
一.安装openOffice1.使用tar -xzvf OOo_3.2.0_LinuxIntel_install_wJRE_en-US.tar.gz解压缩后,会得到OOO320_m12_native_ ...
- 在CentOS 7下ISCSI和多路径部署文档【转】
部署环境 设备:MD3860i, R630, S4820T(生产网络), N2024(管理网络) 操作系统:Centos 7 默认管理口的地址:A控192.168.129.101, B 控192.16 ...
- Linux系统故障处理案例(一)【转】
2016-08-05 14:41 运行环境:CentOS6.7 故障原因: 昨天在线执行命令yum -y update 在命令执行途中,强制中断并直接运行poweroff命令关机.再次开机出现如图所示 ...
- jni中的参数含义
#include <jni.h> JNIEXPORT jstring JNICALL Java_com_example_hellojni_MainActivity_helloFromC ( ...
- 使WiFi具有保存历史连接的功能
在wpa_supplicant.conf里面添加这个功能 update_config=1 就能更新了,保存了历史的连接AP,不用再输入密码