IDA*

· · 个人记录

IDA*

什么是IDA*

众所周知,IDDFS 就是迭代加深搜索,而 A* 就是带有预估的算法。那么这个 IDA* 又是什么雷霆?

这个算法既避免了DFS的深度失控,又克服了A*算法对内存的高消耗,在实际运用中十分有效。(是不是有一股 PPT 的味道)
代码实现也并非容易想到,就是在 IDDFS 的基础上加估价的剪枝就可以了。 :::info[代码模板 \tiny{PPT上的,真的不容易看懂}]

bool IDAStar(State current, int depth, int limit) {
    if (isGoal(current)) { return true; }  // 找到目标状态    
    // 关键剪枝:如果当前深度+估计值超过限制,回溯
    if (depth + h(current) > limit) {    return false; }

    for (auto& next_state : getSuccessors(current))
        if (IDAStar(next_state, depth + 1, limit)) 
            return true;    

    return false;
}
//迭代加深过程
int solve() {
    int limit = h(start_state);
    while (! IDAStar(start_state, 0, limit))      
        limit++;  // 增加深度限制,继续搜索

    return limit;
}

:::

好吧,今天的内容只有这么多
例 :::info[题目大意] 给定 n 本书的乱序排列,每次操作可以选择一段连续的书,将其整体插入到序列中的任意位置。要求找到将序列调整为升序排列所需的最少操作次数。 ::: :::warning[题目思路]

::: :::success[ACcode?] ```cpp line-numbers #include<iostream> using namespace std; int n, a[6][N], mxdep; int h(int dep){ int res = 0; for(int i = 1; i < n; i ++){ if(a[dep][i] + 1 != a[dep][i + 1]){ res ++; } } return /*不知道要填什么*/; } bool dfs(int dep){ int gj = h(dep - 1); if(dep > mxdep){ if(!gj) return 1; return 0; } if(gj > mxdep - dep + 1) return 0; for(int i = 1; i < n; i ++){ for(int l = i + 1; l <= n; l ++){ for(int r = l; r <= n; r ++){//n的数据范围很小,三重循环可以过 int tot = 0; for(int j = 1; j < i; j ++) a[dep][++tot] = a[dep - 1][j]; for(int j = l; j <= r; j++) a[dep][++tot] = a[dep - 1][j]; for(int j = i; j < l; j ++) a[dep][++tot] = a[dep - 1][j]; for(int j = r + 1; j <= n; j ++) a[dep][++tot] = a[dep - 1][j]; if(dfs(dep + 1)) return 1; } } } return 0; } int main(){ int O; cin >> O; while(O--){ cin >> n; for(int i = 1; i <= n; i ++) cin >> a[0][i]; mxdep = 0; while(!dfs(1) && mxdep < 5) mxdep ++; if(mxdep < 5) cout << mxdep << '\n'; else cout << "5 or more\n"; } return 0; } ``` ::: [例2](https://bbcoj.cn/training/278/problem/ST13DLK/full-screen) :::info[题目大意] 棋盘为特殊的井字形结构,分布着数字1、2、3,共有8种可执行的旋转操作改变棋盘布局。 核心目标是通过最少的旋转步骤,使棋盘中心8个关键位置的数字全部变为同一个数(1、2、3其一)。 ::: :::warning[题目思路] 将井字形棋盘的24个有效位置映射为长度为24的一维数组,每个元素存储对应位置的数字,聚焦中心8个关键位置,统计数字1、2、3的出现频次,取最大频次计算需改变的数字总数tot。 ::: :::success[ACcode?] ```cpp line-numbers #include<iostream> using namespace std; const int centre[8] = {7, 8, 9, 12, 13, 16, 17, 18}; const int opp[8] = {5, 4, 7, 6, 1, 0, 3, 2}; const int op[8][7] = {/**/}; int a[26]; int p[26]; int mxdep; int h(){ int t[5] = {0}, res = 0; for(int i = 0; i < 8; i ++){ t[a[centre[i]]] ++; res = max(res, t[a[centre[i]]]); } return 8 - res; } void handle(int x){ int p = a[op[x][0]]; for(int i = 1; i < 7; i ++) a[op[x][i - 1]] = a[op[x][i]]; a[op[x][6]] = p; } bool dfs(int dep, int lst){ int gj = h(); if(dep > mxdep){ if(gj == 0) return 1; return 0; } if(gj + dep > mxdep + 1) return 0; for(int i = 0; i < 8; i ++){ if(lst == opp[i]) continue; handle(i); p[dep] = i; if(dfs(dep + 1, i)) return 1; handle(opp[i]); } return 0; } int main(){ while(cin >> a[1], a[1]){ for(int i = 2; i <= 24; i ++) cin >> a[i]; //多测要清空 while(!dfs(1, -1)) mxdep++; if(mxdep == 0) cout << "No moves needed"; else{ for(int i = 1; i <= mxdep; i ++) cout << char (p[i] + 'A'); } cout << '\n' << a[7] << '\n'; } return 0; } ``` ::: --- :::align{center} $\Huge{Thank}$ $\Huge{for}$ $\Huge{your}$ $\Huge{watching!!!}

:::