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,纵 ...
随机推荐
- 用yaml来编写配置文件
yaml是一个数据序列化的标准,适用于所有开发语言,最大的特点是可读性好. yaml的一个主要应用方向就是编写配置文件,有非常多的系统和框架采用yaml进行配置. yaml有以下基本规则: 1.大小写 ...
- Linux-Xshell会话保持
1.Xshell客户端设置 2.服务端设置 修改/etc/ssh/sshd_config文件,找到 ClientAliveInterval 0和ClientAliveCountMax 3并将注释符号( ...
- Codeforces Round #481 (Div. 3) D. Almost Arithmetic Progression
http://codeforces.com/contest/978/problem/D 题目大意: 给你一个长度为n的b(i)数组,你有如下操作: 对数组中的某个元素+1,+0,-1.并且这个元素只能 ...
- HDU 3094 树上删边 NIM变形
基本的树上删边游戏 写过很多遍了 /** @Date : 2017-10-13 18:19:37 * @FileName: HDU 3094 树上删边 NIM变形.cpp * @Platform: W ...
- Training (deep) Neural Networks Part: 1
Training (deep) Neural Networks Part: 1 Nowadays training deep learning models have become extremely ...
- IIS发布,无法显示CSS样式和图片
描述: 最近给同事安装web程序时,把IIS安装好后,发布网站时,图片和css样式不显示. 程序没问题,发布也没问题. 后来网上一查,出错的原因可能是IIS的配置问题. 果然,按照网上的方法,顺利显示 ...
- 【ORACLE】oracl基本操作笔记
1.用命令导入导出表 C:\Users\xiang>imp bjlims/bjlims@orcl file="c:\tjlims.dmp" full=y C:\Users\x ...
- CodeForces Contest #1110: Global Round 1
比赛传送门:CF #1110. 比赛记录:点我. 涨了挺多分,希望下次还能涨. [A]Parity 题意简述: 问 \(k\) 位 \(b\) 进制数 \(\overline{a_1a_2\cdots ...
- 【网络编程】使用getnameinfo()/getaddrinfo()/InetPton()
1.简要 从前用的网络编程函数现在又做了一定的改动,报了这么3个错误. error C4996: 'inet_ntoa': Use inet_ntop() or InetNtop() instead ...
- Plus One & Plus One Linked List
Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...