题目链接

最小步数这类,适合用迭代加深搜索。

用空格走代替骑士。

搜索时记录上一步防止来回走。

不需要每次判断是否都在位置,可以计算出不在对应位置的骑士有多少个。而且每次复原一个骑士至少需要一步。

空格是不计算未复原骑士数的。

//820kb	84ms
#include <cstdio>
#include <cstring>
#include <algorithm>
#define n (5)
typedef long long LL;
const int way_x[9]={1,1,2,2,-2,-2,-1,-1},way_y[9]={2,-2,1,-1,1,-1,2,-2};
const int End[6][6]=
{{0},
{0,1,1,1,1,1},
{0,0,1,1,1,1},
{0,0,0,2,1,1},
{0,0,0,0,0,1},
{0,0,0,0,0,0},
}; int mp[7][7];
char s[10]; bool DFS(int x,int y,int left,int sum,int las)
{
if(sum>left) return 0;
if(!sum) return 1;
for(int xn,yn,res,i=0; i<8; ++i)
if(i!=7-las&&(xn=x+way_x[i])>0&&(yn=y+way_y[i]) >0&&xn<=n&&yn<=n)
{
res=sum;
if(mp[xn][yn]==End[xn][yn]) ++res; std::swap(mp[x][y],mp[xn][yn]); if(mp[x][y]==End[x][y]) --res; bool f=DFS(xn,yn,left-1,res,i);
if(f) return 1;
std::swap(mp[x][y],mp[xn][yn]);
}
return 0;
} int main()
{
int T,sx,sy,init; scanf("%d",&T);
while(T--)
{
for(int i=1; i<=n; ++i)
{
scanf("%s",s+1);
for(int j=1; j<=n; ++j)
if(s[j]!='*') mp[i][j]=s[j]-'0';
else mp[i][j]=2,sx=i,sy=j;
}
init=0;
for(int i=1; i<=n; ++i)
for(int j=1; j<=n; ++j)
if(mp[i][j]!=End[i][j]) ++init;//init:至少需要 多少步。
if(sx!=3||sy!=3) --init;//空格不计算未复原骑士数。
// printf("init:%d\n",init);
for(int dep=init; ; ++dep)
if(dep==16) {puts("-1"); break;}
else if(DFS(sx,sy,dep,init,8)) {printf("%d\n",dep); break;}
}
return 0;
}

附上sb哈希的代码吧。。真是学傻了。

#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define n (5)
typedef long long LL;
const int way_x[9]={1,1,2,2,-1,-1,-2,-2},way_y[9]={2,-2,1,-1,2,-2,1,-1};
const int End[6][6]=
{{0},
{0,1,1,1,1,1},
{0,0,1,1,1,1},
{0,0,0,2,1,1},
{0,0,0,0,0,1},
{0,0,0,0,0,0},
}; int mp[7][7];
short Ans;
char s[10];
std::map<LL,short> vis;
std::set<LL> st; bool Victory()
{
for(int i=1; i<=n; ++i)
for(int j=1; j<=n; ++j)
if(mp[i][j]!=End[i][j]) return 0;
return 1;
}
LL Encode()
{
LL res=0;
for(int i=1; i<=n; ++i)
for(int j=1; j<=n; ++j) res=res*3+mp[i][j];
// if(Victory()){
// printf("%I64d:\n",res);
// for(int i=1; i<=n; ++i,putchar('\n'))
// for(int j=1; j<=n; ++j) printf("%d ",mp[i][j]);
// }
return res;
}
short DFS(int x,int y,short step,LL s)
{
if(step>15) return 16;
if(Ans<=step) return 17;
if(x==3&&y==3&&Victory()) {Ans=std::min(Ans,step); return step;} short res=17; LL ss;
for(int xn,yn,i=0; i<8; ++i)
if((xn=x+way_x[i])>0&&(yn=y+way_y[i])>0&&xn<=n&&yn<=n)
{
std::swap(mp[x][y],mp[xn][yn]);
ss=Encode();
if(!st.count(ss))
st.insert(ss),res=std::min(res,DFS(xn,yn,step+1,ss)),st.erase(ss);
std::swap(mp[x][y],mp[xn][yn]);
}
return res;
} int main()
{
int T,sx,sy; scanf("%d",&T);
while(T--)
{
Ans=16, st.clear(), vis.clear();
for(int i=1; i<=n; ++i)
{
scanf("%s",s+1);
for(int j=1; j<=n; ++j)
if(s[j]!='*') mp[i][j]=s[j]-'0';
else mp[i][j]=2,sx=i,sy=j;
}
LL s=Encode();
st.insert(s);
DFS(sx,sy,0,s);
printf("%d\n",Ans<=15?Ans:-1);
}
return 0;
}

BZOJ.1085.[SCOI2005]骑士精神(迭代加深搜索)的更多相关文章

  1. BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]

    1085: [SCOI2005]骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1800  Solved: 984[Submit][Statu ...

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

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

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

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

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

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

  5. BZOJ 1085 骑士精神 迭代加深搜索+A*

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1085 题目大意: 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士, 且有一个 ...

  6. bzoj 1085 [SCOI2005]骑士精神——IDA*

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1085 迭代加深搜索. 估价函数是为了预计步数来剪枝,所以要优于实际步数. 没错,不是为了确定 ...

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

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

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

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

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

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

随机推荐

  1. Python基础数据类型-函数传参详解

    Python基础数据类型-函数传参详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.位置参数 #!/usr/bin/env python #_*_coding:utf-8_* ...

  2. Spring Boot 源码分析 数据源 + Mybatis 配置

    公司今年开始使用 Spring Boot 开发,当然使用 Spring Boot 也是大势所趋,尤其是现在微服务的趋向,当然是选择基于Spring Boot 的 Spring Cloud.(所谓的 S ...

  3. shell 示例1 从1叠加到100

    从1叠加到100 echo $[$(echo +{..})] echo $[(+)*(/)] seq -s |bc

  4. Python 入门基础6 --字符编码、文件操作1

    今日内容: 1.字符编码 2.字符与字节 3.文件操作 一.字符编码 了解: cpu:将数据渲染给用户 内存:临时存放数据,断电消失 硬盘:永久存放数据,断电后不消失 1.1 什么是编码? 人类能够识 ...

  5. python 入门基础23 选课系统 项目

    选课系统 代码地址:github_code # 选课系统 # 角色:学校.学员.课程.讲师 # 要求: # 1. 创建北京.上海 2 所学校 # 2. 创建linux , python , go 3个 ...

  6. C#使用redis学习笔记

    1.官网:http://redis.io/(英)  http://www.redis.cn/(中) 2.下载:https://github.com/dmajkic/redis/downloads(Wi ...

  7. zabbix报警Too many processes on zabbix server

    zabbix大量报警,运行进程过多,但实际有部分机器可以忽略,需要关闭相关的报警 Configuration-->Templates找到Template_Linux点该行的 Triggers选择 ...

  8. yum安装软件报错:curl#6 - "Could not resolve host: mirrorlist.centos.org; Temporary failure in name resolut

    # yum install -y epel-release Loaded plugins: fastestmirror Repository base is listed more than once ...

  9. javascript对话框(弹出层)组件

    http://www.blueidea.com/download/product/2010/7513.asp#comment 1. 从头到尾对一遍<<Javascript高级程序设计> ...

  10. vue里面使用Velocity.js

    英文文档:http://velocityjs.org/ https://github.com/julianshapiro/velocity 中文手册(教程):http://www.mrfront.co ...