题意就不用再说明了吧......如此经典

之前想用双向广搜、a*来写,但总觉得无力,现在用IDA*感觉其他的解法都弱爆了..............想法活跃,时间,空间消耗很小,给它跪了

启发式搜索关键还是找估价函数:此题估价函数可大致定性为每个数字(除去x,只要8个数字)当前位置与它期望位置的曼哈顿距离

即为:v += abs(i - pos[map[i][j] - 1][0]);     v += abs(j - pos[map[i][j] - 1][1]);     大致估算为几十步内得出结果。

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <climits>//形如INT_MAX一类的
#define MAX 100005
#define INF 0x7FFFFFFF
#define REP(i,s,t) for(int i=(s);i<=(t);++i)
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
#define mp(a,b) make_pair(a,b)
#define L(x) x<<1
#define R(x) x<<1|1
# define eps 1e-5
//#pragma comment(linker, "/STACK:36777216") ///传说中的外挂
using namespace std; int pos[9][2] = { //各个数字的初始位置
{0,0},{0,1},{0,2},
{1,0},{1,1},{1,2},
{2,0},{2,1},{2,2}
};
int map[4][4];
int buff[50];
char input[11];
int limit,ok;
int dx[] = {-1,0,1,0}; //u r d l____0 1 2 3
int dy[] = {0,1,0,-1};
char op[] = {'u','r','d','l'}; int h(int x,int y) {
int v = 0;
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
if(i != x || j != y) {
v += abs(i - pos[map[i][j] - 1][0]);
v += abs(j - pos[map[i][j] - 1][1]);
}
}
}
return v;
} int dfs(int x,int y,int step,int pre) {
int hn = h(x,y);
if(hn == 0) {
ok = 1;
return step;
}
if(hn + step > limit) return hn + step;
int minn = INF;
for(int i=0; i<4; i++) {
if(abs(i - pre) == 2) continue;
int xx = x + dx[i];
int yy = y + dy[i];
if(xx<0 || xx >=3 || yy<0 || yy >=3) continue;
buff[step] = i;
swap(map[x][y],map[xx][yy]);
int tmp = dfs(xx,yy,step+1,i);
if(ok) return tmp;
minn = min(tmp,minn);
swap(map[x][y],map[xx][yy]);
}
return minn;
} void IDA_star(int x,int y) {
ok = 0;
memset(buff,-1,sizeof(buff));
while(ok == 0 && limit <= 36) {
limit = dfs(x,y,0,-111);
}
if(ok == 1) {
for(int i=0; i<limit; i++) printf("%c",op[buff[i]]);
puts("");
} else puts("unsolvable");
} int main() {
for(int i=0; i<9; i++) cin >> input[i];
int t = 0,x,y;
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
if(input[t] == 'x') {
map[i][j] = 9;
x = i;
y = j;
t++;
} else map[i][j] = input[t++] - '0';
}
}
int limit = h(x,y);
if(limit == 0) {
puts("");
return 0;
}
IDA_star(x,y);
return 0;
}

想吐槽一下杭电的这题.........估计各种无法到达目标的数据,所以在输入时候通过求逆序数对数来判断是否有解,本来是TLE,一下蹦到171ms

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <climits>//形如INT_MAX一类的
#define MAX 100005
#define INF 0x7FFFFFFF
#define REP(i,s,t) for(int i=(s);i<=(t);++i)
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
#define mp(a,b) make_pair(a,b)
#define L(x) x<<1
#define R(x) x<<1|1
# define eps 1e-5
//#pragma comment(linker, "/STACK:36777216") ///传说中的外挂
using namespace std; int pos[9][2] = { //各个数字的初始位置
{0,0},{0,1},{0,2},
{1,0},{1,1},{1,2},
{2,0},{2,1},{2,2}
};
int map[4][4];
int buff[50];
char input[11];
int limit,ok;
int dx[] = {-1,0,1,0}; //u r d l____0 1 2 3
int dy[] = {0,1,0,-1};
char op[] = {'u','r','d','l'}; int h(int x,int y) {
int v = 0;
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
if(i != x || j != y) {
v += abs(i - pos[map[i][j] - 1][0]);
v += abs(j - pos[map[i][j] - 1][1]);
}
}
}
return v;
} int dfs(int x,int y,int step,int pre) {
int hn = h(x,y);
if(hn == 0) {
ok = 1;
return step;
}
if(hn + step > limit) return hn + step;
int minn = INF;
for(int i=0; i<4; i++) {
if(abs(i - pre) == 2) continue;
int xx = x + dx[i];
int yy = y + dy[i];
if(xx<0 || xx >=3 || yy<0 || yy >=3) continue;
buff[step] = i;
swap(map[x][y],map[xx][yy]);
int tmp = dfs(xx,yy,step+1,i);
if(ok) return tmp;
minn = min(tmp,minn);
swap(map[x][y],map[xx][yy]);
}
return minn;
}
int canget(int a[4][4]) { //这一步省了好多时间 求逆序数对数
int i,j,sum=0,b[10],k=0;
for(i=0; i<3; i++) {
for(j=0; j<3; j++) {
if(a[i][j] != 9)
b[++k]=a[i][j];
}
}
for(i=1; i<=8; i++) {
for(j=1; j<i; j++) {
if(b[i]<b[j])sum++;
}
}
if(sum % 2 ==0)return 1;
else return 0;
}
void IDA_star(int x,int y) {
ok = 0;
memset(buff,-1,sizeof(buff));
while(ok == 0 && limit <= 30) {
limit = dfs(x,y,0,-111);
}
if(ok == 1) {
for(int i=0; i<limit; i++) printf("%c",op[buff[i]]);
puts("");
} else puts("unsolvable");
} int main() {
while(cin >> input[0]) {
//memset(map,0,sizeof(map));
for(int i=1; i<9; i++) cin >> input[i];
int t = 0,x,y;
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
if(input[t] == 'x') {
map[i][j] = 9;
x = i;
y = j;
t++;
} else map[i][j] = input[t++] - '0';
}
}
limit = h(x,y);
if(limit == 0) {
puts("");
continue;
}
if(canget(map))
IDA_star(x,y);
else puts("unsolvable");
}
return 0;
}

经过猥琐的测试,limit最小限制在29步....................

POJ 1077 HDU 1043 Eight (IDA*)的更多相关文章

  1. Eight POJ - 1077 HDU - 1043 八数码

    Eight POJ - 1077 HDU - 1043 八数码问题.用hash(康托展开)判重 bfs(TLE) #include<cstdio> #include<iostream ...

  2. POJ 1077 && HDU 1043 Eight A*算法,bfs,康托展开,hash 难度:3

    http://poj.org/problem?id=1077 http://acm.hdu.edu.cn/showproblem.php?pid=1043 X=a[n]*(n-1)!+a[n-1]*( ...

  3. BFS(八数码) POJ 1077 || HDOJ 1043 Eight

    题目传送门1 2 题意:从无序到有序移动的方案,即最后成1 2 3 4 5 6 7 8 0 分析:八数码经典问题.POJ是一次,HDOJ是多次.因为康托展开还不会,也写不了什么,HDOJ需要从最后的状 ...

  4. HDU 1043 & POJ 1077 Eight(康托展开+BFS | IDA*)

    Eight Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30176   Accepted: 13119   Special ...

  5. HDU 1043 & POJ 1077 Eight(康托展开+BFS+预处理)

    Eight Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30176   Accepted: 13119   Special ...

  6. HDU - 1043 - Eight / POJ - 1077 - Eight

    先上题目: Eight Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  7. HDU 1403 Eight&POJ 1077(康拖,A* ,BFS,双广)

    Eight Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  8. POJ 2104&HDU 2665 Kth number(主席树入门+离散化)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 50247   Accepted: 17101 Ca ...

  9. POJ-1077 HDU 1043 HDU 3567 Eight (BFS预处理+康拓展开)

    思路: 这三个题是一个比一个令人纠结呀. POJ-1077 爆搜可以过,94ms,注意不能用map就是了. #include<iostream> #include<stack> ...

随机推荐

  1. IO-02

    /** 2 *A2-IO-02. 整数四则运算(10) 3 *C语言实现 4 *测试已通过 5 */ #include "stdio.h" #include "stdli ...

  2. Landsat8数据不同波段组合的用途

    2013年2月11日发射的Landsat系列最新卫星Landsat8,携带有OLI陆地成像仪和TIRS热红外传感器,Landsat8的OLI陆地成像仪包括9个波段,OLI包括了ETM+传感器所有的波段 ...

  3. error: ld returned 1 exit status 和 error:undefined reference

    undefined reference 往往是链接时出现错误,无法解析引用.这篇文章总结的很好undefined reference问题总结 error: ld returned 1 exit sta ...

  4. Spring、Spring依赖注入与编码剖析Spring依赖注入的原理

    Spring依赖注入 新建PersonIDao 和PersonDao底实现Save方法: public interface PersonIDao { public void save(); } pub ...

  5. 网易云课堂_程序设计入门-C语言_期末考试编程题

    1 字数统计(10分) 题目内容: 你的程序要读入一篇英文文章,然后统计其中的单词数来输出.需要统计的数据为: 总的单词数量: 含有1个字母到10个字母的单词的数量. 单词和单词的间隔是由以下标点符号 ...

  6. iOS多线程编程指南(一)关于多线程编程(转)

    原文:http://www.dreamingwish.com/article/ios-multi-threaded-programming-a-multi-threaded-programming.h ...

  7. 【插队问题-线段树-思维巧妙】【poj2828】Buy Tickets

    可耻的看了题解 巧妙的思维 逆序插入,pos 代表的意义为前面要有pos个空格才OK: 证明:仔细思考一下就觉得是正确的,但是要想到这种方式还是要很聪明,空格是前面的几个数字所形成的,所以要特地留出来 ...

  8. 执行SQL查询脚本

    static void Main(string[] args) { Console.WriteLine("输入用户编号:"); string cusernum = Console. ...

  9. Mvc里查询商品页面

    /// <summary> /// 商品小类筛选页面 GoodsTypeName ----------------SelectGoods--商品筛选 --图文 /// Home/Selec ...

  10. bootstrap注意事项(五)表单

    1.基本实例 单独的表单控件会被自动赋予一些全局样式.所有设置了 .form-control类的 <input>.<textarea> 和 <select> 元素都 ...