版本比较
题解:P2346 四子连棋
正文
增加 76 行删除 70 行
1123345678910118 行未改动2021222322242524262728293031323334353436373838394041424344454647484950513940414243444546474849505152535454555656575758595960616263646566676869707172737475767778798081828384858687606162636465666768697071727374757677787980818283848586878889909192939495909697929394959697989899100101102103104105106101102103104105106107108109110111107108109110111112113114115116117118~~[成功击杀所有题解](https://www.luogu.com.cn/ticket/KTWA375828)~~[P2346 四子连棋](https://www.luogu.com.cn/problem/P2346)
很好的 IDA* 习题,[题目传送门](https://www.luogu.com.cn/problem/P2346) 。~~[成功击杀所有题解](https://www.luogu.com.cn/ticket/KTWA375828)。~~
很好的 IDA* 习题。
## 题意简述 给定一个 $4\times 4$ 的棋盘,共 $14$ 个棋子($7$ 黑 $7$ 白)和 $2$ 个空格。黑棋用 `B`、白棋用 `W`、空格用 `O` 表示。黑白双方交替走棋(任意一方可先走),每一步可将一枚棋子移至相邻(上下左右)的空格。若某一步完成后,棋盘上出现任意颜色的四子连成一线(横、竖、斜均可)即达到目标状态。求达成目标所需的最少步数。
## 解法 8 行未改动
由于本题目标状态为“任意颜色形成四子一线”。一种显而易见的 $h$ 为: - 若当前棋盘已经满足目标,则 $h = 0$;- 否则,至少还需要走 $1$ 步才能达到目标, 故取 $h = 0$(若胜利)或 $h = 1$(若未胜利)。故取 $h = 0$(若达到目标状态)或 $h = 1$(若未达到目标状态)。
这样的估值函数虽然剪枝力度不大,但棋盘规模小,迭代加深深度通常不超过 $20$,搜索完全可行。 这样的估值函数虽然剪枝力度不大,但对于本题棋盘规模较小,迭代加深深度较浅的情况下,搜索完全可行。
也可以设计更优秀的 $h$ 提升时间复杂度,但~~我懒~~实现比较复杂复杂且收益有限,读者可以自行思考。
## 代码 ```cpp#include <bits/stdc++.h>using namespace std;typedef long long LL;//十年OI一场空,不开long long见祖宗const int n = 4;char s[5][5];const int D[4] = {-4 , 4 , -1 , 1}; bool check(string &b){ for(int i = 0 ; i < 4 ; ++i){ int p = i * 4; if(b[p] != 'O' && b[p] == b[p + 1] && b[p + 1] == b[p + 2] && b[p + 2] == b[p + 3]) return 1; } for(int j = 0 ; j < 4 ; ++j){ if(b[j] != 'O' && b[j] == b[j + 4] && b[j + 4] == b[j + 8] && b[j + 8] == b[j + 12]) return 1; } if(b[0] != 'O' && b[0] == b[5] && b[5] == b[10] && b[10] == b[15]) return 1; if(b[3] != 'O' && b[3] == b[6] && b[6] == b[9] && b[9] == b[12]) return 1; return 0; for(int i = 0 ; i < 4 ; ++i){ int p = i * 4; if(b[p] != 'O' && b[p] == b[p + 1] && b[p + 1] == b[p + 2] && b[p + 2] == b[p + 3]) return 1; } for(int j = 0 ; j < 4 ; ++j){ if(b[j] != 'O' && b[j] == b[j + 4] && b[j + 4] == b[j + 8] && b[j + 8] == b[j + 12]) return 1; } if(b[0] != 'O' && b[0] == b[5] && b[5] == b[10] && b[10] == b[15]) return 1; if(b[3] != 'O' && b[3] == b[6] && b[6] == b[9] && b[9] == b[12]) return 1; return 0;}int h(string &b){ return check(b) ? 0 : 1; return check(b) ? 0 : 1;}bool dfs(string cur , int turn , int g , int limit , unordered_set<string> &vis){ if(g + h(cur) > limit)bool dfs(string cur , int turn , int g , int lit , unordered_set<string> &vis){ if(g + h(cur) > lit) return 0; if(check(cur)) return 1; int p1 = -1 , p2 = -1; for(int i = 0 ; i < 16 ; ++i) if(cur[i] == 'O'){ if(p1 == -1) p1 = i; else p2 = i; } int blank[2] = {p1 , p2}; char cur_c = (turn == 0 ? 'B' : 'W'); for(int b = 0 ; b < 2 ; ++b){ int pos = blank[b]; int x = pos / 4 , y = pos % 4; for(int k = 0 ; k < 4 ; ++k){ if(k == 2 && y == 0) continue; if(k == 3 && y == 3) continue; int npos = pos + D[k]; if(npos < 0 || npos >= 16) continue; if(cur[npos] != cur_c) continue; string nxt = cur; swap(nxt[pos] , nxt[npos]); if(vis.count(nxt)) continue; vis.insert(nxt); if(dfs(nxt , 1 - turn , g + 1 , limit , vis)) return 1; vis.erase(nxt); } } return 0; if(check(cur)) return 1; int p1 = -1 , p2 = -1; for(int i = 0 ; i < 16 ; ++i) if(cur[i] == 'O'){ if(p1 == -1) p1 = i; else p2 = i; } int blank[2] = {p1 , p2}; char cur_c = (turn == 0 ? 'B' : 'W'); for(int b = 0 ; b < 2 ; ++b){ int pos = blank[b]; int x = pos / 4 , y = pos % 4; for(int k = 0 ; k < 4 ; ++k){ if(k == 2 && y == 0) continue; if(k == 3 && y == 3) continue; int npos = pos + D[k]; if(npos < 0 || npos >= 16) continue; if(cur[npos] != cur_c) continue; string nxt = cur; swap(nxt[pos] , nxt[npos]); if(vis.count(nxt)) continue; vis.insert(nxt); if(dfs(nxt , 1 - turn , g + 1 , lit , vis)) return 1; vis.erase(nxt); } } return 0;}int IDA(string start , int turn){ if(check(start)) if(check(start)) return 0; for(int i = 1 ; i <= 50 ; ++i){ unordered_set<string> vis; vis.insert(start); if(dfs(start , turn , 0 , i , vis)) return i; } return 100; for(int i = 1 ; i <= 50 ; ++i){ unordered_set<string> vis; vis.insert(start); if(dfs(start , turn , 0 , i , vis)) return i; } return -1;}int main(){ //freopen(".in" , "r" , stdin); //freopen(".out" , "w" , stdout); for(int i = 0 ; i < 4 ; ++i) scanf("%s" , s[i]); string start = ""; for(int i = 0 ; i < 4 ; ++i) for(int j = 0 ; j < 4 ; ++j) start += s[i][j]; int ans = min(IDA(start , 0) , IDA(start , 1)); printf("%d" , ans); return 0; //freopen(".in" , "r" , stdin); //freopen(".out" , "w" , stdout); for(int i = 0 ; i < 4 ; ++i) scanf("%s" , s[i]); string start = ""; for(int i = 0 ; i < 4 ; ++i) for(int j = 0 ; j < 4 ; ++j) start += s[i][j]; int ans = min(IDA(start , 0) , IDA(start , 1)); printf("%d" , ans); return 0;}