8数码,欺我太甚!<bfs+康拓展开>
来源:互联网 发布:android编程实战 pdf 编辑:程序博客网 时间:2024/06/05 17:20
不多述,直接上代码,至于康拓展开,以前的文章里有
#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;}
0 0
- 8数码,欺我太甚!<bfs+康拓展开>
- 九数码(康拓展开+BFS)
- UVA 10085(bfs+康拓展开)八数码问题
- HDU 1043 八数码 反向BFS+康拓展开
- poj 1077 Eight 八数码问题( 康拓展开+BFS状态压缩)
- hdu1043 bfs 康拓展开
- 8数码 BFS
- 八数码问题 poj 1077 康拓展开
- 康拓展开(八数码编码、排列组合)
- 康拓展开及应用(八数码编码,排列组合)
- HDU 1043 Eight(BFS+康拓展开)
- POJ 1077 Eight(康拓展开 BFS 双向BFS)
- poj 1077 bfs+康托展开(8数码问题)
- HDU 1043 八数码问题 A*搜索+康拓展开+逆序对判断+路径输出
- POJ 1077 Eight BFS + 康拓展开式
- hdu1430(康拓展开+bfs打表)
- hdu 1430 魔板(bfs+康拓展开)
- Hdoj 1430 魔板 【BFS】+【康拓展开】+【预处理】
- 【git】日志更改
- 剑指offer 面试题10:二进制中1的个数 题解
- 详解新创建的安卓项目下的各个文件夹用途
- Android Studio 进阶详细配置
- Java核心技术(四) —— 继承(1)
- 8数码,欺我太甚!<bfs+康拓展开>
- 反向工程--Visio 2007自动生成ER图
- Javascript实现4种--弹出确认_取消框,和刷新页面清楚 输入框中之前输入的内容
- Java多线程中join方法的理解
- oracle 11g 安装之后网络连接问题
- 我的你的他们的
- HDU 2205 亲和串
- Git 的origin和master分析(转载)
- 二叉树中和为某一值的路径