BZOJ1085 luogu2324骑士精神题解
没有什么特别好的办法,只好用搜索去做
因为一次移动最多归位一个骑士
所以可以想到用IDA*,为了简化状态
我们用k,x,y,sum来表示移动了k步,空格在x,y,还用sum个没有归位的情况
然后枚举转移即可
标准的IDA*
# include<cstring>
# include<iostream>
# include<cstdio>
# include<cmath>
# include<cstdio>
# include<algorithm>
using namespace std;
const int mn = ;
int b[mn][mn]={{},
{,,,,,},
{,,,,,},
{,,,-,,},
{,,,,,},
{,,,,,}};
int dx[]={-,-,-,,-,,,};
int dy[]={-,,, ,-,-,-,};
int a[mn][mn];
int t,sx,sy;
int ans=-;
int maxd;
bool check(int k,int x,int y,int sum,int la)
{
if(k+sum>maxd)
return false;
if(sum==)
return true;
bool flag=false;
int xx,yy,p,col;
for(int i=;i<=;i++)
{
if(i!=-la)
{
xx=x+dx[i];
yy=y+dy[i];
p=sum;
if(xx> && xx<= && yy> && yy<=)
{
if(a[xx][yy]==b[xx][yy] && a[xx][yy]!=b[x][y])
p++;
if(a[xx][yy]!=b[xx][yy] && a[xx][yy]==b[x][y])
p--;
if(b[xx][yy]==-)
p--;
if(b[x][y]==-)
p++;
col=a[xx][yy];
a[xx][yy]=-,a[x][y]=col;
flag=check(k+,xx,yy,p,i);
if(flag)
return true;
a[x][y]=-,a[xx][yy]=col;
}
}
}
return false;
}
void ida(int ret)
{
for(maxd=ret;maxd<=;maxd++)//注意此处要到16
{
if(check(,sx,sy,ret,-))
{
ans=maxd-;
return ;
}
}
}
int main()
{
int ret;
char ch;
scanf("%d",&t);
while(t--)
{
ans=-;
ret=;
for(int i=;i<=;i++)
for(int j=;j<=;j++)
{
cin>>ch;
if(ch=='*')
{
a[i][j]=-;
sx=i,sy=j;
}
else a[i][j]=ch-'';
if(a[i][j]!=b[i][j])
ret++;
}
ida(ret);
printf("%d\n",ans);
}
return ;
}
BZOJ1085 luogu2324骑士精神题解的更多相关文章
- BZOJ1085 SCOI2005 骑士精神【IDA* 启发式迭代加深】
BZOJ1085 SCOI2005 骑士精神 Description 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士, 且有一个空位.在任何时候一个骑士都能按照骑士的走法(它可以走到和它横坐 ...
- BZOJ1085:[SCOI2005]骑士精神——题解+IDA*粗略讲解
http://www.lydsy.com/JudgeOnline/problem.php?id=1085 Description 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士, 且有一个空 ...
- BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]
1085: [SCOI2005]骑士精神 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1800 Solved: 984[Submit][Statu ...
- 【日常学习】【IDA*】codevs2449 骑士精神题解
题目描写叙述 Description 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士, 且有一个空位.在不论什么时候一个骑士都能依照骑士的走法(它能够走到和它横坐标相差为1.纵坐标相差为2或 ...
- [BZOJ1085] [SCOI2005] 骑士精神 (A*)
Description 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士, 且有一个空位.在任何时候一个骑士都能按照骑士的走法(它可以走到和它横坐标相差为1,纵坐标相差为2或者横坐标相差为2, ...
- [BZOJ1085][SCOI2005]骑士精神 搜索
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1085 大的思路是迭代加深搜索,我们加一个明显的剪枝,当棋盘中位置不对的骑士的数目加上已经走 ...
- BZOJ1085: [SCOI2005]骑士精神
传送门 dfs+A*优化. A*是人工智能算法,属于启发式搜索的一部分.第一次知道这个名词是在写虫食算的时候闵神说这个用A*搞跑的比谁都快..但是当时搜了很多资料想搞清楚这个东西,但是当时还是太拿衣服 ...
- BZOJ1085 [SCOI2005]骑士精神(IDA*)
IDA*是IDS的基础上加上满足A*算法的估值函数来剪枝的搜索算法. 这题代码量挺少的,可以看出整个IDA*的框架: #include<cstdio> #include<cstrin ...
- bzoj1085 [SCOI2005]骑士精神——IDA*
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1085 搜索,IDA*,估价就是最少需要跳的步数: 代码意外地挺好写的,memcmp 用起来好 ...
随机推荐
- WCF常见问题
一.创建时,WCF Service中HttpContext.Current为null的解决办法 1. 在hosting WCF的web.config中加入: <system.serviceMod ...
- Hackerrank--Emma and sum of products (FFT)
题目链接 Emma is really fond of integers and loves playing with them. Her friends were jealous, and to t ...
- python禁止函数修改列表的实现方法
python禁止函数修改列表的实现方法 有时候,需要禁止函数修改列表.例如要对裂变进行修改操作,也要保留原来的未打印的设计列表,以供备案.为解决这个问题,可向函数传递列表的副本而不是原件:这样函数所做 ...
- 备忘 ubuntu ip 及 dns 的坑
以前都用 ubuntu 16.04 现在用 18.04 遇到几个恶心的事,现在解决了,记录下来. 1. 设置 DNS , DNS 设置老是不对,最后发现问题老版本 ubuntu 17.10以下 ...
- Activity、FragmentActivity和AppCompatActivity的区别
Activity Activity是最基础的一个,是其它类的直接或间接父类. Activity中只能使用系统自带的host Fragment(API Level 11中加入),对应getFragmen ...
- nginx链接末尾自动补全斜杠
放在locaation里边就行 if (-d $request_filename){ rewrite ^(.*[^/])$ $/ permanent;#加斜杠 } 这样,nginx就会进行判断了,如果 ...
- Could not parse mapping document from resource com/hs/model/StudentModel.hbm.xml
网上出现这个问题的 lei.hbm.xml配置写错的,文件头应该改为如下,并不是这个问题 <?xml version="1.0"?> <!DOCTYPE hibe ...
- TomCat 启动默认加载项目
在最后加上这句代码即可:<Context path="" docBase="\项目名称" reloadable="true" cros ...
- session失效刷新后登录页面嵌入在iframe中的解决办法
在login页面中添加以下一段代码: var _topWin = window; while (_topWin != _topWin.parent.window) { _topWin ...
- HDU5583 Kingdom of Black and White
Kingdom of Black and White Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...