POJ 1077 Eight(八数码第八境界|IDA*+曼哈顿距离+判断是否有解)
来源:互联网 发布:淘宝搜索的算法 编辑:程序博客网 时间:2024/05/21 19:35
题意:八数码。
思路:IDA*+曼哈顿距离+判断是否有解。因为要用到IDA*搜索,所以我们搜索之前先判断一下是否有解。
判断的方法是学习一个大神的: 判断八数码问题是否有解
IDA*比起BFS的好处是空间复杂度极低,同时因为有剪枝,比起BFS降低了盲目性;比起A*的好处是不用去维护一个堆。
#include<cstdio>#include<cstring>#include<cmath>#include<cstdlib>#include<iostream>#include<algorithm>#include<vector>#include<map>#include<queue>#include<stack>#include<string>#include<map>#include<set>#include<ctime>#define eps 1e-6#define LL long long#define pii pair<int, int>//#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;const int MAXN = 400000;//const int INF = 0x3f3f3f3f;int dx[] = {-1, 0, 1, 0};int dy[] = {0, -1, 0, 1};int mov[1000];int maxd;char c;struct State {int s[9];int pos;} state;int cal_pos(int pos, int i) {int nx = pos/3+dx[i], ny = pos%3+dy[i];if(nx<0 || nx>2 || ny<0 || ny>2) return -1;return nx*3 + ny;}int cal_h() {int ans = 0;for(int i = 0; i < 9; i++) {if(state.s[i] == 9) continue;int x = (state.s[i]-1)/3, y = (state.s[i]-1)%3;int nx = i/3, ny = i%3;ans += abs(x-nx) + abs(y-ny);}return ans;}void init() {if(c != 'x') state.s[0] = c - '0';else {state.s[0] = 9;state.pos = 0;}for(int i = 1; i < 9; i++) {cin >> c;if(c == 'x') {state.s[i] = 9;state.pos = i;}else state.s[i] = c - '0';}}bool dfs(int d, int last) {if(d == maxd) {if(!cal_h()) return true;return false;}for(int i = 0; i < 4; i++) {if(abs(i-last) == 2) continue;int newpos = cal_pos(state.pos, i);if(newpos<0) continue;swap(state.s[state.pos], state.s[newpos]);swap(state.pos, newpos);if(d+cal_h() > maxd) {swap(state.s[state.pos], state.s[newpos]);swap(state.pos, newpos);continue;}mov[d+1] = i;if(dfs(d+1, i)) return true;swap(state.s[state.pos], state.s[newpos]);swap(state.pos, newpos);}return false;} void IDA_star() {for(maxd = 0; ; maxd++) {if(dfs(0, 100)) return;}}void print_path(int dep) {if(!dep) return;print_path(dep-1);if(!mov[dep]) printf("u");else if(mov[dep]==1) printf("l");else if(mov[dep]==2) printf("d");else printf("r");}bool is_solvable() {int cnt = 0;for(int i = 0; i < 9; i++) {if(state.s[i] == 9) continue;for(int j = 0; j < i; j++) {if(state.s[j] == 9) continue;if(state.s[j] > state.s[i]) cnt++;}}return !(cnt%2);}int main() { //freopen("input.txt", "r", stdin);while(cin >> c) { init();if(!is_solvable()) printf("unsolvable");else {IDA_star();print_path(maxd);}puts("");} return 0;}
0 0
- POJ 1077 Eight(八数码第八境界|IDA*+曼哈顿距离+判断是否有解)
- 八数码问题 IDA*算法+曼哈顿距离 poj 1077
- HDU 1043 Eight(八数码第七境界|A*+哈希+曼哈顿距离)
- POJ 1077 Eight(八数码A*+IDA*)
- POJ1077&HDU1043 Eight 八数码第八境界 IDA* hash 康托展开 奇偶剪枝
- POJ 1077 Eight(八数码第四境界|双向广搜+哈希)
- PKU-1077 Eight (八数码之IDA*)
- HDU-1043 Eight(经典八数码问题, A*+康拓+曼哈顿距离+逆序数判断可解性、双向搜索)
- 八数码第八境界——IDA*+逆序数判无解
- IDA* 解八数码问题: POJ 1077
- poj 1077 八数码 ida*
- 八数码问题判断是否有解
- 搜索 ( 八数码问题详解:BFS,A*,IDA* )——Eight ( POJ 1077 )
- uva 652---Eight Poj 1077 ---Eight zoj 1217---Eight (八数码解法1)
- poj 1077 Eight A*解八数码问题
- POJ 1077(HDU 1043)Eight(八数码DBFS)
- hdu 1043 poj 1077 Eight Time (搜索&八数码)
- HDU 1043 Eight poj 1077 (八数码 启发式搜索)
- Ubuntu 14 配置java环境
- POJ 3714 Raid
- A*算法入门
- UNIX的各种文字游戏
- <LeetCode><Easy> 328. Odd Even Linked List
- POJ 1077 Eight(八数码第八境界|IDA*+曼哈顿距离+判断是否有解)
- 八数码的几种做法的总结以及是否有解的判断
- leetCode 226. Invert Binary Tree
- **LeetCode 60. Permutation Sequence
- (一二二)运算符重载——第十一章
- Android Studio——SAX解析XML
- (一二二)友元函数
- Learning how to learn(1)
- Linux(Ubuntu, Fedora, Vmware)使用笔记(1)