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

题目链接 : BZOJ 1085

题目分析 :

  本题中可能的状态会有 (2^24) * 25 种状态,需要使用优秀的搜索方式和一些优化技巧。

  我使用的是 IDA* 搜索,从小到大枚举步数,每次 DFS 验证在当前枚举的步数之内能否到达目标状态。

  如果不能到达,就枚举下一个步数,重新搜索,即使某些状态在之前的 DFS 中已经搜索过,我们仍然搜索。

  并且在一次 DFS 中,我们不需要判定重复的状态。

  在 IDA* 中,重要的剪枝优化是 估价函数 ,将一些不可能存在可行解的枝条剪掉。

  如果估价函数写得高效,就能有极好的效果。我们写估价函数的原则是,绝对不能剪掉可能存在可行解的枝条。

  因此,在预估需要步数时,应让估计值尽量接近实际步数,但一定不能大于实际需要的步数。

  本题的一个很有效的估价函数是,比较当前状态的黑白骑士与目标状态的黑白骑士有多少不同,我们把这个值作为估价函数值,因为每一步最多将当前状态的一个骑士改变为与目标状态相同。但是应该注意的是,空格所在的格子不要算入其中。

代码如下:

  

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
 
using namespace std;
 
const int MaxStep = 15;
const int Dx[8] = {1, 1, -1, -1, 2, 2, -2, -2}, Dy[8] = {2, -2, 2, -2, 1, -1, 1, -1};
 
int Te, Ans;
 
char Str[6];
 
struct ES
{
    int num, pos;
    bool operator == (const ES &e) const {
        return (num == e.num) && (pos == e.pos);
    }
} S, T;
 
inline bool Inside(int x, int y) {
    if (x < 0 || x > 4) return false;
    if (y < 0 || y > 4) return false;
    return true;
}
 
void Print(ES x) {
    for (int i = 0; i < 5; ++i) {
        for (int j = 0; j < 5; ++j) {
            if (x.pos == (i * 5 + j)) printf("*");
            else {
                if (x.num & (1 << (i * 5 + j))) printf("1");
                else printf("0");
            }
        }
        printf("\n");
    }
}
 
inline int Expect(ES x) {
    int Temp, Cnt;
    Temp = x.num ^ T.num;
    Cnt = 0;
    if (x.pos != T.pos) {
        if (Temp & (1 << T.pos)) --Cnt;
        if (Temp & (1 << x.pos)) --Cnt;
    }
    while (Temp) {
        ++Cnt;
        Temp -= Temp & -Temp;
    }
    return Cnt;
}
 
bool DFS(ES Now, int Step, int Limit) {
    if (Now == T) return true;
    if (Step == Limit) return false;
    if (Expect(Now) > Limit - Step) return false;
    int x, y, xx, yy;
    ES Next;
    x = Now.pos / 5; y = Now.pos % 5;
    for (int i = 0; i < 8; i++) {
        xx = x + Dx[i]; yy = y + Dy[i];
        if (!Inside(xx, yy)) continue;
        Next = Now;
        Next.pos = xx * 5 + yy;
        Next.num &= (1 << 25) - 1 - (1 << (xx * 5 + yy));
        if (Now.num & (1 << (xx * 5 + yy))) Next.num |= (1 << (x * 5 + y));
        if (DFS(Next, Step + 1, Limit)) return true;
    }
    return false;
}
 
int IDAStar(ES S) {
    if (S == T) return 0;
    for (int i = 1; i <= MaxStep; ++i)
        if (DFS(S, 0, i)) return i;
    return -1;
}
 
int main()
{
    scanf("%d", &Te);
    T.num = 549855; T.pos = 12;
    for (int Case = 1; Case <= Te; ++Case) {
        S.num = 0;
        for (int i = 0; i < 5; ++i) {
            scanf("%s", Str);
            for (int j = 0; j < 5; ++j) {
                if (Str[j] == ‘1‘) S.num |= (1 << (i * 5 + j));
                if (Str[j] == ‘*‘) S.pos = i * 5 + j;
            }
        }
        Ans = IDAStar(S);
        printf("%d\n", Ans);
    }
    return 0;
}

  

时间: 2024-08-06 03:39:31

[BZOJ 1085] [SCOI2005] 骑士精神 [ IDA* 搜索 ]的相关文章

bzoj 1085: [SCOI2005]骑士精神 IDA*

题目链接 给一个图, 目标位置是确定的, 问你能否在15步之内达到目标位置. 因为只有15步, 所以直接ida* #include<bits/stdc++.h> using namespace std; #define pb(x) push_back(x) #define ll long long #define mk(x, y) make_pair(x, y) #define lson l, m, rt<<1 #define mem(a) memset(a, 0, sizeof(

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

Bzoj 1085: [SCOI2005]骑士精神 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1085 dfs + 剪枝. 剪枝方法: 1.每次交换只能改变一个位置.若发现之间相差的步数加上以前走的步数大于15的话,直接舍弃这一状态. 2.初始时,\(ans\)设为\(16\) 有了上面两个剪枝就A了. 照这节奏,SCOI2005就刷完了??? #include <iostream> #include <cstdio>

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

一开始写了个 BFS 然后就 T 了... 这道题是迭代加深搜索 + A* ------------------------------------------------------------------------------ #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #define rep( i , n ) for( int i = 0 ; i

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

题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1085 分析: 首先第一感觉是宽搜,但是空间需要8^15*5*5,明显不够,又鉴于最大深度为15,所以可以用迭代加深做. 但是普通的迭代加深还是会TLE.于是考虑加上估价函数 设当前层数的上界为Kmax,当前搜索的层数为K,我们知道一次移动顶多改变目前矩阵和目标矩阵的一个差别,于是可以求出当前与目标矩阵不同的位置的个数s,如果k+s>Kmax那么就可以直接不做了.

BZOJ 1085 [SCOI2005]骑士精神

据说是A_star的裸题,感觉就是爆搜剪枝. //Twenty #include<cstdio> #include<cstdlib> #include<iostream> #include<algorithm> #include<cmath> #include<cstring> #include<queue> #include<vector> using namespace std; int ans,T,x,y

[BZOJ 1085][SCOI 2005]骑士精神(IDA*搜索)

题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1085 考虑到深度不超过15,IDA*搜索可做. 估价函数h()=当前不在目标位置的棋子个数. 然后其他细节就和普通的迭代加深一样了. #include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <algorithm>

【BZOJ】1085: [SCOI2005]骑士精神

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1085 $${if (cs+val-1>ans) return ;}$$ 1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<vector> 5 #include<cstdlib> 6 #include<cmath> 7 #inc

bzoj1085 [SCOI2005]骑士精神

1085: [SCOI2005]骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2490  Solved: 1422[Submit][Status][Discuss] Description 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士, 且有一个空位.在任何时候一个骑士都能按照骑士的走法(它可以走到和它横坐标相差为1,纵坐标相差为2或者横坐标相差为2,纵坐标相差为1的格子)移动到空位上. 给定一个初始的棋盘,怎样才能经过移动

【bzoj1085】[SCOI2005]骑士精神

1085: [SCOI2005]骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1757  Solved: 961[Submit][Status][Discuss] Description 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士, 且有一个空位.在任何时候一个骑士都能按照骑士的走法(它可以走到和它横坐标相差为1,纵坐标相差为2或者横坐标相差为2,纵坐标相差为1的格子)移动到空位上. 给定一个初始的棋盘,怎样才能经过移动变