POJ 1077 Eight
题意:经典的八数码=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的更多相关文章
- HDU 1043 & POJ 1077 Eight(康托展开+BFS+预处理)
Eight Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30176 Accepted: 13119 Special ...
- HDU 1043 & POJ 1077 Eight(康托展开+BFS | IDA*)
Eight Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30176 Accepted: 13119 Special ...
- Eight POJ - 1077 HDU - 1043 八数码
Eight POJ - 1077 HDU - 1043 八数码问题.用hash(康托展开)判重 bfs(TLE) #include<cstdio> #include<iostream ...
- HDU - 1043 - Eight / POJ - 1077 - Eight
先上题目: Eight Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- 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]*( ...
- 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 ...
- poj 1077 Eight(双向bfs)
题目链接:http://poj.org/problem?id=1077 思路分析:题目要求在找出最短的移动路径,使得从给定的状态到达最终状态. <1>搜索算法选择:由于需要找出最短的移动路 ...
- poj 1077 Eight (八数码问题——A*+cantor展开+奇偶剪枝)
题目来源: http://poj.org/problem?id=1077 题目大意: 给你一个由1到8和x组成的3*3矩阵,x每次可以上下左右四个方向交换.求一条路径,得到12345678x这样的矩阵 ...
- BFS(八数码) POJ 1077 || HDOJ 1043 Eight
题目传送门1 2 题意:从无序到有序移动的方案,即最后成1 2 3 4 5 6 7 8 0 分析:八数码经典问题.POJ是一次,HDOJ是多次.因为康托展开还不会,也写不了什么,HDOJ需要从最后的状 ...
- HDU 1403 Eight&POJ 1077(康拖,A* ,BFS,双广)
Eight Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
随机推荐
- git安装及使用
一.安装 1.从http://code.google.com/p/msysgit/下载Git-1.8.4-preview20130916.exe,并安装. 2.新建git目录,右键选择Git Bash ...
- 在AngularJS中学习javascript的new function意义及this作用域的生成过程
慢慢入门吧,不着急. 至少知道了controller和service的分工. new function时,隐含有用this指向function的prototype之意. 这样,两个JAVASCRIPT ...
- java split IP地址要用双斜杠
示例代码: public void test() { String address = "11.12.13.14:800"; System.out.println(address. ...
- 【转】Wireshark:“There are no interfaces on which a capture can be done ”
linux环境下 两种解决方案: 第一种方法:使用root用户登陆 xiaoshancun@xiaoshancun-VM500:~$ sudo wireshark 第二种方法 ...
- lintcode 中等题:Palindrome Linked List 回文链表
题目 回文链表 设计一种方式检查一个链表是否为回文链表. 样例 1->2->1 就是一个回文链表. 挑战 O(n)的时间和O(1)的额外空间. 解题 法一: 再定义一个链表,存放链表反转的 ...
- java:内部类与外部类的区别和联系
注意事项一:在内部类中可以随意使用外部类的成员方法以及成员变量. 众所周知,在定义成员方法或者成员变量的时候,可以给其加上一些权限的修饰词,以防止其他类的访问.如在成员变量或者成员方法前面,加上Pri ...
- Hibernate逍遥游记-第12章 映射值类型集合-001映射set(<element>)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- 用React.addons.TestUtils、Jasmine进行单元测试
一.用到的工具 1.React.addons.TestUtils 2.Jasmine 3.Browserify(处理jsx文件的require依赖关系) 4.Reactify(能和browserify ...
- python 模拟浏览器
想用python模拟浏览器访问web的方法测试些东西,有哪几种方法呢? 一类:单纯的访问web,不解析其js,css等. 1. urllib2 #-*- coding:utf-8 -* import ...
- ARM CPU与Intel x86 CPU性能比较
Qualcomm ARM CPU与Intel x86 CPU性能比较 随着移动互联网时代的到来,Qualcomm(高通).Texas Instruments(德州仪器)等基于ARM架构的CPU受到越来 ...