BZOJ.1085.[SCOI2005]骑士精神(迭代加深搜索)
最小步数这类,适合用迭代加深搜索。
用空格走代替骑士。
搜索时记录上一步防止来回走。
不需要每次判断是否都在位置,可以计算出不在对应位置的骑士有多少个。而且每次复原一个骑士至少需要一步。
空格是不计算未复原骑士数的。
//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]骑士精神(迭代加深搜索)的更多相关文章
- BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]
1085: [SCOI2005]骑士精神 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1800 Solved: 984[Submit][Statu ...
- Bzoj 1085: [SCOI2005]骑士精神 (dfs)
Bzoj 1085: [SCOI2005]骑士精神 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1085 dfs + 剪枝. 剪枝方法: ...
- BZOJ 1085: [SCOI2005]骑士精神( IDDFS + A* )
一开始写了个 BFS 然后就 T 了... 这道题是迭代加深搜索 + A* -------------------------------------------------------------- ...
- BZOJ 1085 [SCOI2005]骑士精神 【A*启发式搜索】
1085: [SCOI2005]骑士精神 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2838 Solved: 1663 [Submit][St ...
- BZOJ 1085 骑士精神 迭代加深搜索+A*
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1085 题目大意: 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士, 且有一个 ...
- bzoj 1085 [SCOI2005]骑士精神——IDA*
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1085 迭代加深搜索. 估价函数是为了预计步数来剪枝,所以要优于实际步数. 没错,不是为了确定 ...
- [BZOJ 1085] [SCOI2005] 骑士精神 [ IDA* 搜索 ]
题目链接 : BZOJ 1085 题目分析 : 本题中可能的状态会有 (2^24) * 25 种状态,需要使用优秀的搜索方式和一些优化技巧. 我使用的是 IDA* 搜索,从小到大枚举步数,每次 DFS ...
- [BZOJ 1085][SCOI2005]骑士精神(IDA*)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1085 分析: 首先第一感觉是宽搜,但是空间需要8^15*5*5,明显不够,又鉴于最大深 ...
- bzoj 1085: [SCOI2005]骑士精神
Description 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士,且有一个空位.在任何时候一个骑士都能按照骑士的走法(它可以走到和它横坐标相差为1,纵坐标相差为2或者横坐标相差为2,纵 ...
随机推荐
- CM记录-配置Hive on Spark
默认hive on spark是禁用的,需要在Cloudera Manager中启用.1.登录CM界面,打开hive服务.2.单击 配置标签,查找enable hive on spark属性.3.勾选 ...
- python学习笔记6--操作redis
一.redis操作 import redis r=redis.Redis(host='211.149.218.16',port=6379,password='123456',db=2) r.set(' ...
- python学习笔记6--操作Mysql
一.mysql操作 import pymysql #连上mysql ip 端口号 密码 账号 数据库 #建立游标 #执行sql #获取结果 #关闭连接.关闭游标 conn=pymysql.connec ...
- html之div始终停留在屏幕中间部分
需求: 使得某一个div始终停留在屏幕中间 实现: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...
- tensorflow随机张量创建
TensorFlow 有几个操作用来创建不同分布的随机张量.注意随机操作是有状态的,并在每次评估时创建新的随机值. 下面是一些相关的函数的介绍: tf.random_normal 从正态分布中输出随机 ...
- CF258D Little Elephant and Broken Sorting (带技巧的DP)
题面 \(solution:\) 这道题主要难在考场上能否想到这个思路(即如何设置状态)(像我这样的蒟蒻就想不到呀QAQ)不过这一题确实很神奇! \(f[i][j]:\)表示第 \(a_i\) 个数比 ...
- hibernate的多对多关联映射
在我们实际项目中,多对多的情况也时长存在,比如最常见的就是系统管理的五张表,如下面的一个结构: 在本文学习hibernate多对多关联映射的实验中我简单的写几个字段,达到学习目的即可. 1.多对多的关 ...
- 终极利器!利用appium和mitmproxy登录获取cookies
环境搭建 参考我之前写的https://www.cnblogs.com/c-x-a/p/9163221.html appium 代码start_appium.py # -*- coding: utf- ...
- springboot整合Thymeleaf模板引擎
引入依赖 需要引入Spring Boot的Thymeleaf启动器依赖. <dependency> <groupId>org.springframework.boot</ ...
- LCT摘要
介绍.用途 LCT是树链剖分中的一种,又叫实链剖分.动态树,常用于维护动态的树.森林. 维护方式 LCT并不直接维护原树,而是用一堆splay作为辅助树来维护.原树中的一条实链上的点在一棵splay中 ...