給定 n×n 火柴棒方格陣(2n(n+1) ≤ 60 根火柴),部分火柴已移除。問最少再移除多少根火柴,使圖中不存在任何完整正方形(任意大小)。
uint64_t 位掩碼,每位代表一根火柴是否存在(1 存在,0 移除)。總火柴 ≤ 60,恰好放入 64 位。squares[]。s 下仍完整的正方形數。具有單調性:移除一根火柴最多破壞 1 個正方形(實際上可破壞多個,但 h(s') ≥ h(s) - 1),滿足 IDA* 剪枝條件。bound = h(initial) 遞增。每步選擇一根能破壞至少一個正方形的火柴移除(優先破壞較多正方形的火柴),遞歸搜索,直到 h == 0。using ULL = unsigned long long;
ULL squares[100]; // 每個正方形的火柴掩碼
int sqCnt;
ULL initState; // 初始火柴掩碼
int heuristic(ULL s) {
int cnt = 0;
for (int i = 0; i < sqCnt; ++i)
if ((s & squares[i]) == squares[i]) ++cnt; // 正方形完整
return cnt;
}
bool dfs(ULL s, int depth, int bound) {
int h = heuristic(s);
if (h == 0) return true;
if (depth + h > bound) return false; // f = g + h > bound
// 選擇一根火柴移除:優先能破壞最多正方形的
// ... 遍歷火柴位,嘗試 s ^ (1ULL<
搜索空間極大,但強啟發式與剪枝使實際運行極快(n≤5 時火柴≤60,正方形≤55)。