A*搜索。

A*搜索的基础百度百科(实在偷懒没看论文之类的),在这里不说了。

A*搜索很关键的是h(n)估价函数的选取(表示现在到结束节点需要的距离)

设d(n)为实际到结束节点的距离。h(n)<=d(n)就又不会丢掉最优解,又可以中途就排除很多不行的答案。

bfs就是一个h(n)=0的A*搜索。。。。。(所有状态都会被加进去,错的离谱的也要)。。。。。

好感人。。。。

。。。说到哪了。。。。

如果俩个状态有n个不一样的地方,至少要走n-1步,这个估价函数相当好。

在这个程序具体实现中dep=d时,其实已经走了(d+1)步。。所以估价函数不用-1.

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std; const int dx[]={2,1,-1,-2,-2,-1,1,2},dy[]={1,2,2,1,-1,-2,-2,-1}; char EndStatus[5][10] = {
"11111",
"01111",
"00*11",
"00001",
"00000", }; struct Status {
char a[5][6];
int x,y; bool operator ==(Status b) {
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
if(a[i][j] != b.a[i][j]) return false;
return true;
} int differ(Status b) {
int res=0;
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
if(a[i][j] != b.a[i][j]) res++;
return res;
} Status step(int d) {
Status res=*this;
res.x=x+dx[d],res.y=y+dy[d];
if(res.x<0 || res.x>4 || res.y<0 || res.y>4) return *this;
swap(res.a[x][y],res.a[res.x][res.y]);
return res;
} void get() {
for(int i=0;i<5;i++) {
scanf("%s",a[i]);
for(int j=0;j<5;j++) if(a[i][j]=='*') {
x=i; y=j;
}
}
} void get(char b[5][10]) {
for(int i=0;i<5;i++)
for(int j=0;j<5;j++) {
a[i][j]=b[i][j];
if(a[i][j]=='*') {x=i; y=j;}
}
}
}Start,End,t;
int lim,T; bool dfs(int dep,Status s) {
if(dep==lim) return s==End;
for(int d=0;d<8;d++) {
t=s.step(d);
if(!(t==s) && dep+t.differ(End)<=lim) if(dfs(dep+1,t)) return true;
}
return false;
} int main() {
End.get(EndStatus);
scanf("%d",&T);
while(T--) {
Start.get();
for(lim=0;lim<=15;lim++) if(dfs(0,Start)) break;
printf("%d\n",lim>15 ? -1 : lim); }
return 0;
}

1085: [SCOI2005]骑士精神的更多相关文章

  1. BZOJ 1085: [SCOI2005]骑士精神( IDDFS + A* )

    一开始写了个 BFS 然后就 T 了... 这道题是迭代加深搜索 + A* -------------------------------------------------------------- ...

  2. BZOJ 1085 [SCOI2005]骑士精神 【A*启发式搜索】

    1085: [SCOI2005]骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 2838  Solved: 1663 [Submit][St ...

  3. Bzoj 1085: [SCOI2005]骑士精神 (dfs)

    Bzoj 1085: [SCOI2005]骑士精神 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1085 dfs + 剪枝. 剪枝方法: ...

  4. BZOJ(7) 1085: [SCOI2005]骑士精神

    1085: [SCOI2005]骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3233  Solved: 1911[Submit][Stat ...

  5. 【BZOJ】1085: [SCOI2005]骑士精神(A*启发式搜索)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1085 囧啊囧,看了题解后写了个程序,但是样例总过不了T+T,调试了不下于1个小时,肉眼对拍看了根本看 ...

  6. bzoj 1085: [SCOI2005]骑士精神

    Description 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士,且有一个空位.在任何时候一个骑士都能按照骑士的走法(它可以走到和它横坐标相差为1,纵坐标相差为2或者横坐标相差为2,纵 ...

  7. 1085. [SCOI2005]骑士精神【IDA※】

    Description 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士, 且有一个空位.在任何时候一个骑士都能按照骑 士的走法(它可以走到和它横坐标相差为1,纵坐标相差为2或者横坐标相差为2 ...

  8. [BZOJ 1085] [SCOI2005] 骑士精神 [ IDA* 搜索 ]

    题目链接 : BZOJ 1085 题目分析 : 本题中可能的状态会有 (2^24) * 25 种状态,需要使用优秀的搜索方式和一些优化技巧. 我使用的是 IDA* 搜索,从小到大枚举步数,每次 DFS ...

  9. [BZOJ 1085][SCOI2005]骑士精神(IDA*)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1085 分析: 首先第一感觉是宽搜,但是空间需要8^15*5*5,明显不够,又鉴于最大深 ...

  10. 【BZOJ】1085: [SCOI2005]骑士精神

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1085 $${if (cs+val-1>ans) return ;}$$ #inclu ...

随机推荐

  1. github 中删除/更名版本库(repository)

    问题描述: github 中版本库创建/删除/更该名称 问题解决:            (1)创建版本库(Repository) 注:        在上图中的+按钮图标指示的是创建版本库的按钮 注 ...

  2. 使用node的http模块实现爬虫功能,并把爬到的数据存入mongondb

    刚开始使用http中间件做爬虫其实蛮多坑的,最主要的坑就是编码问题,有很多中文网站的采用的gb2313的编码方式,这个在爬到的报文解析就很蛋碎, 因为http中间件对utf-8支持的比较好,所以针对这 ...

  3. MemSQL Start[c]UP 2.0 - Round 1

    A. Eevee http://codeforces.com/contest/452/problem/A 字符串水题 #include<cstdio> #include<cstrin ...

  4. win7 安装Oracle 10G,11G

    安装 10G : 安装说明: http://wenku.baidu.com/view/a73d048bd0d233d4b14e69a8.html 按这个安装成功过.   11G R2: 在Win7 6 ...

  5. 解Linux进程间通信(IPC)方式

    http://blog.csdn.net/liuhongxiangm/article/details/7928790 linux下的进程通信手段基本上是从Unix平台上的进程通信手段继承而来的.而对U ...

  6. 使用函数的递归调用来解决Hanoi(汉诺)塔问题。

    #include<stdio.h> void hanoi(int n, char x, char y, char z); void move(char x, char y); int ti ...

  7. uva 10887

    是个 hash  用的容器类水过 #include <iostream> #include <cstdio> #include <string> #include ...

  8. JS内存管理测试

    打开调试器,切换到timer,点击左下角的record按钮开始,切换到memory视图,在文档中点击鼠标左右键,看股价走势图 function Allocate(kbs){ this.mem = ne ...

  9. jQuery1.9.1源码分析--Ajax模块

    //Serialize an array of form elements or a set of //key/values into a query string // 将数组形式的表单元素或者哈希 ...

  10. hdoj 1879 继续畅通工程

    继续畅通工程 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...