bzoj1085骑士精神 题解(IDA*入门题)
2016-08-12 18:31
417 查看
迭代加搜算法思路
总体上按照深度优先算法方法进行
对搜索深度需要给出一个深度限制dm,当深度达到了dm的时候,如果还没有找到解答,就停止对该分支的搜索,换到另外一个分支继续进行搜索。
dm从1开始,从小到大依次增大(因此称为迭代加深)
迭代加深搜索是最优的,也是完备的
h函数用的是不在应该在的位置上的个数(不计空格)
总体上按照深度优先算法方法进行
对搜索深度需要给出一个深度限制dm,当深度达到了dm的时候,如果还没有找到解答,就停止对该分支的搜索,换到另外一个分支继续进行搜索。
dm从1开始,从小到大依次增大(因此称为迭代加深)
迭代加深搜索是最优的,也是完备的
h函数用的是不在应该在的位置上的个数(不计空格)
#include <iostream> #include <stdio.h> #include <algorithm> #include <stdlib.h> #include <map> #include <stack> #include <vector> #include <string.h> #include <queue> #define msc(X) memset(X,-1,sizeof(X)) #define ms(X) memset(X,0,sizeof(X)) typedef long long LL; using namespace std; const int MAXN=1<<25; int chess[5][5]; int x[8]={1,1,2,2,-2,-2,-1,-1}, y[8]={2,-2,1,-1,1,-1,2,-2}; int pLt; int h(int s[][5]) { int rs=0; for(int i=0;i<5;i++) for(int j=0;j<5;j++) if(s[i][j]==1){ if(i==4) rs++; else if(i==3&&j!=4) rs++; else if(i==2&&j<3) rs++; else if(i==1&&j==0) rs++; } else if(!s[i][j]){ if(i==0) rs++; else if(i==1&&j!=0) rs++; else if(i==2&&j>1) rs++; else if(i==3&&j==4) rs++; } return rs; } bool IDA(int cur[][5],int d,int loc,int pre) { if(d>=pLt){ return !h(cur); } int next[5][5]; for(int i=0;i<8;i++) { if(pre+i==7) continue;//和上一步方向相反 int tx=loc/5+x[i],ty=loc%5+y[i]; if(tx<0||tx>4||ty<0||ty>4) continue; for(int j=0;j<5;j++) for(int k=0;k<5;k++) next[j][k]=cur[j][k]; int nloc=tx*5+ty; next[loc/5][loc%5]=cur[tx][ty]; next[tx][ty]=2; if(d+h(next)<pLt&&IDA(next,d+1,nloc,i)) return true; } return false; } int main(int argc, char const *argv[]) { int t; cin>>t; getchar(); while(t--){ char ch; int loc; for(int i=0;i<5;i++) { for(int j=0;j<5;j++) { scanf("%c",&ch); if(ch=='*') chess[i][j]=2,loc=5*i+j;//空格位置 else chess[i][j]=ch-'0'; } getchar(); } pLt=h(chess)-1; while(++pLt<=15&&!IDA(chess,0,loc,-1)) continue; if(pLt>15) puts("-1"); else printf("%d\n",pLt ); } return 0; } /* 1 11111 01111 00*11 00001 00000 2 10110 01*11 10111 01001 00000 01011 110*1 01110 01010 00100 */
相关文章推荐
- BZOJ1085:[SCOI2005]骑士精神——题解+IDA*粗略讲解
- 【bzoj1085】【SCOI2005】【骑士精神】【IDA*】
- [BZOJ 1085][SCOI 2005]骑士精神(IDA*搜索)
- [BZOJ 1085][SCOI2005]骑士精神(IDA*)
- 【IDA*】BZOJ1085(SCOI2005)[骑士精神]题解
- [IDA*] BZOJ1085: [SCOI2005]骑士精神
- 【BZOJ1085】【SCOI2005】骑士精神(IDA*)
- BZOJ1085 [SCOI2005]骑士精神(IDA*)
- BZOJ 1085: [SCOI2005]骑士精神 IDA* 估价函数
- bzoj 1085: [SCOI2005]骑士精神(IDA*)
- 【BZOJ】1085 [SCOI2005]骑士精神 IDA*
- BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]
- [BZOJ 1085] [SCOI2005] 骑士精神 [ IDA* 搜索 ]
- 【BZOJ1085】【SCOI2005】骑士精神(A*暴搜)
- [SCOI2005][BZOJ1085] 骑士精神
- bzoj 1085: [SCOI2005]骑士精神 IDA*
- bzoj1085骑士精神 A*
- 【BZOJ 1085】 [SCOI2005]骑士精神
- bzoj 1085: [SCOI2005]骑士精神
- bzoj1085 骑士精神