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数组的相关函数比较熟),所以在这里也发现了很多容 ...
随机推荐
- POJ 2062 HDU 1528 ZOJ 2223 Card Game Cheater
水题,感觉和田忌赛马差不多 #include<cstdio> #include<cstring> #include<cmath> #include<algor ...
- Redis常用API-使用文档
一.Redis Client介绍 1.1.简介 Jedis Client是Redis官网推荐的一个面向java客户端,库文件实现了对各类API进行封装调用. Jedis源码工程地址:https://g ...
- thinkphp 实现微信公众号开发(二)--实现自定义菜单
IndexController.class.php <?php namespace Home\Controller; use Think\Controller; class IndexContr ...
- nefu 115 斐波那契的整除
Description 已知斐波那契数列有如下递归定义,f(1)=1,f(2)=1, 且n>=3,f(n)=f(n-1)+f(n-2),它的前几项可以表示为1, 1,2 ,3 ,5 ,8,13, ...
- Android Studio的使用(十四)--如何查看资源或者函数在哪些类中被引用
1.我们都知道在Eclipse中可以通过快捷键Ctrl+Shift+G开快速搜索方法.类.资源都在那个类中被使用了. 2.在Android Studio中则使用快捷键Ctrl+G.
- A - 娜娜梦游仙境系列——诡异的钢琴
A - 娜娜梦游仙境系列——诡异的钢琴 Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Othe ...
- BFS and Queue
BFS (Bridth First Search) can be implemented by a queue. Procedure is like this: (Q is Queue) 1, Put ...
- ffmpeg 录屏 screen capture recorder
ffmpeg在Linux下用X11grab进行屏幕录像,在Windows下用DirectShow滤镜 首先需要安装一个软件,screen capture recorder 编译好的下载地址是: htt ...
- c#高级编程
1..net才程序编译经过2步.首先把源代码编译成IL,这个是在visual studio中编译,然后是IL编译成机器语言,这个是在程序执行的时候进行的.
- 客户端 HttpUtils.java
package com.http.post; import java.io.ByteArrayOutputStream; import java.io.IOException; import java ...