p1199八数码问题
oj上简化的八数码问题,最强的数据仅仅是20步;
根据曼哈顿距离构造启发函数;
主算法:IDA*;(使用方法好像不太对......)
未用位运算优化;
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<ctime>
#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
#include<iomanip>
#include<queue>
using namespace std;
#define FILE "dealing"
#define up(i,j,n) for(int i=(j);i<=(n);i++)
#define pii pair<int,int>
#define LL long long
namespace IO{
char buf[<<],*fs,*ft;
int gc(){return (fs==ft&&(ft=(fs=buf)+fread(buf,,<<,stdin),fs==ft))?-:*fs++;}
int read(){
int ch=gc(),f=,x=;
while(ch<''||ch>''){if(ch=='-')f=;ch=gc();}
while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=gc();}
return f?-x:x;
}
}using namespace IO;
const int maxn=;
char ch[maxn],en[maxn]={'','','','','','','','',''};
int a[][];
int p[][]={
{,,,,,,,,},
{,,,,,,,,},
{,,,,,,,,},
{,,,,,,,,},
{,,,,,,,,},
{,,,,,,,,},
{,,,,,,,,},
{,,,,,,,,},
{,,,,,,,,}
};
int x[],y[];
inline int check(int a[][]){
int ans=;
up(i,,)x[a[][i-]]=i-;
up(i,,)x[a[][i-]]=i-;
up(i,,)x[a[][i-]]=i-;
up(i,,)ans+=p[x[i]][y[i]];
return ans;
}
const int dx[]={,,,-},dy[]={,-,,};
bool flag=;
int ans=;
int MaxH=;
void dfs(int a[][],int d){
int xx,yy;
xx=check(a);
if(!xx){flag=;ans=min(ans,d);return;}
if(d+xx>MaxH)return;
up(i,,)up(j,,){
if(a[i][j])continue;
up(k,,){
xx=i+dx[k],yy=j+dy[k];
if(xx<||yy<||xx>||yy>)continue;
swap(a[xx][yy],a[i][j]);
dfs(a,d+);
swap(a[xx][yy],a[i][j]);
}
}
return;
}
int main(){
freopen(FILE".in","r",stdin);
freopen(FILE".out","w",stdout);
scanf("%s",ch);
up(i,,)y[en[i]-'']=i;
up(i,,)a[][i-]=ch[i-]-'';up(i,,)a[][i-]=ch[i-]-'';up(i,,)a[][i-]=ch[i-]-'';
for(int i=;;i+=){
MaxH=i;
dfs(a,);
if(flag)break;
}
printf("%d\n",ans);
cout<<"The time has passed "<<clock()<<" ms."<<endl;
return ;
}
p1199八数码问题的更多相关文章
- A*算法 -- 八数码问题和传教士过河问题的代码实现
前段时间人工智能的课介绍到A*算法,于是便去了解了一下,然后试着用这个算法去解决经典的八数码问题,一开始写用了挺久时间的,后来试着把算法的框架抽离出来,编写成一个通用的算法模板,这样子如果以后需要用到 ...
- 八数码问题:C++广度搜索实现
毕竟新手上路23333,有谬误还请指正. 课程设计遇到八数码问题(这也是一坨),也查过一些资料并不喜欢用类函数写感觉这样规模小些的问题没有必要,一开始用深度搜索却发现深搜会陷入无底洞,如果设定了深度限 ...
- ACM/ICPC 之 BFS-广搜进阶-八数码(经典)(POJ1077+HDU1043)
八数码问题也称为九宫问题.(本想查查历史,结果发现居然没有词条= =,所谓的历史也就不了了之了) 在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的某一数字,不同棋子上标的数字不相同.棋盘上还有一个 ...
- BFS(八数码) POJ 1077 || HDOJ 1043 Eight
题目传送门1 2 题意:从无序到有序移动的方案,即最后成1 2 3 4 5 6 7 8 0 分析:八数码经典问题.POJ是一次,HDOJ是多次.因为康托展开还不会,也写不了什么,HDOJ需要从最后的状 ...
- 双向广搜+hash+康托展开 codevs 1225 八数码难题
codevs 1225 八数码难题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Yours和zero在研究A*启 ...
- UVALive 6665 Dragonâs Cruller --BFS,类八数码问题
题意大概就是八数码问题,只不过把空格的移动方式改变了:空格能够向前或向后移动一格或三格(循环的). 分析:其实跟八数码问题差不多,用康托展开记录状态,bfs即可. 代码: #include <i ...
- P1379 八数码问题
aoapc上的八数码问题,在luogu上也有类似的题,p1379,经典题目,lrj给出了一个算法,同时给出了三种判重的方法.本来想用std::queue改写一下,但是出了各种问题,只好抄代码ac掉这道 ...
- [cdoj1380] Xiper的奇妙历险(3) (八数码问题 bfs + 预处理)
快要NOIP 2016 了,现在已经停课集训了.计划用10天来复习以前学习过的所有内容.首先就是搜索. 八数码是一道很经典的搜索题,普通的bfs就可求出.为了优化效率,我曾经用过康托展开来优化空间,甚 ...
- hdu 1043 Eight 经典八数码问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 The 15-puzzle has been around for over 100 years ...
随机推荐
- Linux(14):集群架构进阶 --- CentOS 7
CentOS 7 CentOS 7 管理软件常用命令: vim /etc/selinux/config # 修改 SELINUX 开机自启动与否 setenforce 0 # 关闭 SELINUX g ...
- sring->list->del->string->int:解析左右编码器的,和#号
#def test_sprintf(): import string ' str1="1234567890," print'str1 is',str1 list_raw=list( ...
- Java中的网络基础
先来一张图记录一下大概思路,之后再更新具体的代码实现.基本上来说,前半部分自己会编写一个基于socket编程的多客户端dos聊天服务器,后半部分可以实现与已有的一些服务器(比如www.google.c ...
- Maven的仓库
以下内容引用自https://ayayui.gitbooks.io/tutorialspoint-maven/content/book/maven_repositories.html: 什么是Mave ...
- 【spring boot jpa】hql语句报错 :antlr.NoViableAltException: unexpected token: roleName
使用场景:在spring data jpa下使用@Query("hql语句") 然后在项目启动的时候报错 hql语句报错:antlr.NoViableAltException: u ...
- js获取table的值,js获取td里input的值
1.如果想让table具有可以编辑的功能,可以在table里嵌入input标签 写法{{ list_one[1] or '' }}的作用是,当list_one[1]取值为None时,前端web界面不至 ...
- ubuntu 添加和删除用户
Without a home directory sudo useradd myuser With home directory sudo useradd -m myuser Then set the ...
- SQL SELECT TOP, LIMIT, ROWNUM 子句
SQL SELECT TOP, LIMIT, ROWNUM 子句 SQL SELECT TOP 子句 SELECT TOP 子句用于规定要返回的记录的数目. SELECT TOP 子句对于拥有数千条记 ...
- php闭包实例
php闭包函数,一个典型的实例 function getMoney() { $rmb = 1; $dollar = 6; $func = function($dollar) use (&$rm ...
- oracle死锁解决经常用法(屡试不爽)
--1.查询被锁的情况 select object_name,machine,s.sid,s.serial# from v$locked_object l,dba_objects o ,v$sessi ...