题意:经典的八数码=3=

3*3的格子,里面有1~8这8个数字,还有一个空格x,移动空格的位置,直到移到1~8按顺序排好,输出移动的序列。

解法:看到题果断写了个广搜……然后T了……百度了一下说广搜虽然慢了点但是也是可以过的嘛……默默看了眼自己代码……唔……好像他们都不是用string路径的……

实在懒得改了,学个新做法吧,哦吼吼吼这个A*看起来很神奇啊……学一下学一下

600ms+过了……嗯……跟别人广搜一个时间啊……_(:з」∠)_实在不想改记路径的方法啊……

A*我觉得就是一个更聪明的广搜……每个状态的权值为每个数到最终状态的曼哈顿距离之和加上已走过的步长,用优先队列维护……

还有就是每个状态序列可以转换成一个排列的id……涨姿势了0v0

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long using namespace std; int fac[] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880 };
int order(vector <int> v) {
int i, j, temp, num;
num = 0;
for (i = 0; i < 8; i++) {
temp = 0;
for (j = i + 1; j < 9; j++) {
if (v[j] < v[i])
temp++;
}
num += fac[v[i] -1] * temp;
}
return num;
}
bool vis[400000];
int dir1[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1};
char dir2[] = "udlr";
struct node
{
vector <int> v;
string path;
node(vector <int> tv, string tpath)
{
v = tv;
path = tpath;
}
node() {}
bool operator < (const node &tmp) const
{
int sum1 = 0, sum2 = 0;
for(int i = 0; i < 9; i++)
sum1 += abs((v[i] - 1) / 3 - i / 3) + abs((v[i] - 1) % 3 - i % 3);
for(int i = 0; i < 9; i++)
sum2 += abs((tmp.v[i] - 1) / 3 - i / 3) + abs((tmp.v[i] - 1) % 3 - i % 3);
return sum1 + path.size() > sum2 + tmp.path.size();
}
};
string bfs(vector <int> st)
{
memset(vis, 0, sizeof vis);
vis[order(st)] = 1;
priority_queue <node> q;
q.push(node(st, ""));
while(!q.empty())
{
node tmp = q.top();
if(order(tmp.v) == 0) return tmp.path;
q.pop();
int sx, sy;
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
if(tmp.v[i * 3 + j] == 9)
{
sx = i;
sy = j;
}
for(int i = 0; i < 4; i++)
{
int tx = sx + dir1[i][0], ty = sy + dir1[i][1];
if(tx < 0 || tx > 2 || ty < 0 || ty > 2) continue;
swap(tmp.v[tx * 3 + ty], tmp.v[sx * 3 + sy]);
int id = order(tmp.v);
if(!vis[id])
{
vis[id] = 1;
q.push(node(tmp.v, tmp.path + dir2[i]));
}
swap(tmp.v[tx * 3 + ty], tmp.v[sx * 3 + sy]);
}
}
return "unsolvable";
}
int main()
{
char input[2];
while(~scanf("%s", input))
{
vector <int> v;
if(input[0] == 'x')
v.push_back(9);
else
v.push_back(input[0] - '0');
for(int i = 0; i < 8; i++)
{
scanf("%s", input);
if(input[0] == 'x')
v.push_back(9);
else
v.push_back(input[0] - '0');
}
int sum = 0;
for(int i = 1; i < 9; i++)
for(int j = 0; j < i; j++)
if(v[i] != 9 && v[j] != 9 && v[i] < v[j]) sum++;
if((sum & 1) == 0)
cout << bfs(v) << endl;
else
puts("unsolvable1");
}
return 0;
}

  

POJ 1077 Eight的更多相关文章

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

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

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

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

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

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

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

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

  5. 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]*( ...

  6. hdu 1043 pku poj 1077 Eight (BFS + 康拓展开)

    http://acm.hdu.edu.cn/showproblem.php?pid=1043 http://poj.org/problem?id=1077 Eight Time Limit: 1000 ...

  7. poj 1077 Eight(双向bfs)

    题目链接:http://poj.org/problem?id=1077 思路分析:题目要求在找出最短的移动路径,使得从给定的状态到达最终状态. <1>搜索算法选择:由于需要找出最短的移动路 ...

  8. poj 1077 Eight (八数码问题——A*+cantor展开+奇偶剪枝)

    题目来源: http://poj.org/problem?id=1077 题目大意: 给你一个由1到8和x组成的3*3矩阵,x每次可以上下左右四个方向交换.求一条路径,得到12345678x这样的矩阵 ...

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

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

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

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

随机推荐

  1. REST和SOAP Web Service的区别比较

    本文转载自他人的博客,ArcGIS Server 推出了 对 SOAP 和 REST两种接口(用接口类型也许并不准确)类型的支持,本文非常清晰的比较了SOAP和Rest的区别联系! ///////// ...

  2. JsRender系列demo(1)-insert-data

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  3. hdu 4038 Stone

    思路: 如果负数的个数为偶数则不必改变,为奇数就将最大负数变为正: 对于正数,尽量将1,2变为3即可. 代码如下: #include<cstring> #include<iostre ...

  4. hdu 1271 整数对

    看了别人的解题报告a了, 大致思路就是 A=a+b*10^k+c*10^(k+1) B=a+c*10^k (在A中取出一位数后) N=A+B=2*a+b*10^k+11*c*10^k 这样就好做了,再 ...

  5. 匿名用户访问sharepoint2010中的列表

    Allow anonymous users to access listitems on a publishing site in SharePoint 2010 https://knowledge. ...

  6. Android:数据存储之SQLite

    Android在运行时集成了SQLite , 所以每个Android应用程序都可以使用SQLite数据库. 我们通过SQLiteDatabase这个类的对象操作SQLite数据库,而且不需要身份验证. ...

  7. IPC$命令详解

    一 摘要二 什么是ipc$三 什么是空会话四 空会话可以做什么五 ipc$所使用的端口六 ipc管道在hack攻击中的意义七 ipc$连接失败的常见原因八 复制文件失败的原因九 关于at命令和xp对i ...

  8. js问题总结

    .removeClass(" ") 移除一个css样式 $("#popUpBox i").removeClass("channel_icon" ...

  9. 自定义View(5)Paint常用的一些绘制滤镜,特效等介绍

    Shader 返回绘图过程中重复色块的基类 相关方法:Paint::setShader(Shader shader) BitmapShader 从位图加载重复色块 LinearGradient, Ra ...

  10. NFC(3)Android上的NFC,开启NFC,3种NDEF数据

    Android对NFC技术的支持 Android2.3.1(API Level = 9)开始支持NFC技术,但Android2.x和Android3.x对NFC的支持非常有限.而从Android4.0 ...