poj1077 Eight【爆搜+Hash(脸题-_-b)】
转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4298840.html ---by 墨染之樱花
题目链接:http://poj.org/problem?id=1077
题目描述:民间流传的推15游戏,不过这里简化为3*3,也就是八数码问题,‘x’表示空位。与AOJ0121的“Seven Puzzle”类似。
思路:没什么特别的,构造字符串队列,爆搜一下。注意Hash函数,哈得好就哈哈,哈得不好就只能呵呵了。。。我的hash函数是∑((str[i]*7^i))%1000007外加在x的位置加上i*10007,547MS水过。不过在一样的题hdu1043时限变成了5秒却还是TLE了,果然此题杭电的数据更强,看来是要用A*了。
#include <iostream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <climits>
using namespace std;
#define XINF INT_MAX
#define INF 1<<30
#define MAXN 1000007
#define eps 1e-10
#define zero(a) fabs(a)<eps
#define sqr(a) ((a)*(a))
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define PF(X) push_front(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
#define PI acos(-1.0)
#define test puts("OK");
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
typedef pair<int,int> PII;
typedef priority_queue<int,vector<int>,greater<int> > PQI;
typedef vector<PII> VII;
typedef vector<int> VI;
#define X first
#define Y second inline int Hash(string str)
{
int len=str.length(),s=,ans=;
REP(i,len)
{
if(str[i]>='' && str[i]<='')
ans+=(str[i]-'')*s;
else
ans+=i*;
s*=;
ans=ans%MAXN;
}
return ans;
} pair<int,int> par[MAXN];
bool vis[MAXN];
int dir[][]={{-,},{,},{,},{,-}};
char direction[]={'u','d','r','l'};
char ans[+]; void bfs(string start,string end)
{
CLR(vis,);
queue<string> Q;
int s=Hash(start);
Q.push(start);
vis[s]=;
par[s]=MP(s,-);
while(!Q.empty())
{
string temp=Q.front();Q.pop();
if(temp==end)
break;
int pos;
REP(i,)
if(temp[i]=='x')
pos=i;
int x=pos/,y=pos%;
REP(i,)
{
int tx=x+dir[i][],ty=y+dir[i][];
if(tx>= && tx< && ty>= && ty<)
{
string next=temp;
swap(next[tx*+ty],next[x*+y]);
int ll=Hash(next);
if(!vis[ll])
{
par[ll]=MP(Hash(temp),i);
vis[ll]=;
Q.push(next);
}
}
}
}
} int main()
{_
string s="";
REP(i,)
{
char c;
cin>>c;
s+=c;
}
bfs(s,"12345678x");
int res=Hash("12345678x"),tot=;
while(par[res].X!=res)
{
ans[tot++]=direction[par[res].Y];
res=par[res].X;
}
for(int i=tot-;i>=;i--)
cout<<ans[i];
cout<<endl;
return ;
}
poj1077 Eight【爆搜+Hash(脸题-_-b)】的更多相关文章
- 2015 UESTC 搜索专题F题 Eight Puzzle 爆搜
Eight Puzzle Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 ...
- BZOJ_1224_[HNOI2002]彩票_爆搜+打表
BZOJ_1224_[HNOI2002]彩票_爆搜+打表 Description 某地发行一套彩票.彩票上写有1到M这M个自然数.彩民可以在这M个数中任意选取N个不同的数打圈.每个彩民只能买一张彩票, ...
- 2015 UESTC 搜索专题E题 吴队长征婚 爆搜
吴队长征婚 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Descri ...
- BZOJ_4809_皇后_爆搜
BZOJ_4809_皇后_爆搜 Description 众所不知,rly现在不会玩国际象棋.但是,作为一个OIer,rly当然做过八皇后问题.这里再啰嗦几句,皇后可以攻击到同行同列同对角线,在n*n的 ...
- 2015 UESTC 搜索专题C题 基爷与加法等式 爆搜DFS
基爷与加法等式 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Desc ...
- 【BZOJ-1853&2393】幸运数字&Cirno的完美算数教室 容斥原理 + 爆搜 + 剪枝
1853: [Scoi2010]幸运数字 Time Limit: 2 Sec Memory Limit: 64 MBSubmit: 1817 Solved: 665[Submit][Status] ...
- POJ 1166 The Clocks (爆搜 || 高斯消元)
题目链接 题意: 输入提供9个钟表的位置(钟表的位置只能是0点.3点.6点.9点,分别用0.1.2.3)表示.而题目又提供了9的步骤表示可以用来调正钟的位置,例如1 ABDE表示此步可以在第一.二.四 ...
- hdu4536-XCOM Enemy Unknown(爆搜)
XCOM-Enemy Unknown是一款很好玩很经典的策略游戏. 在游戏中,由于未知的敌人--外星人入侵,你团结了世界各大国家进行抵抗.随着游戏进展,会有很多的外星人进攻事件.每次进攻外星人会选择3 ...
- BZOJ 1207: [HNOI2004]打鼹鼠【妥妥的n^2爆搜,dp】
1207: [HNOI2004]打鼹鼠 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3259 Solved: 1564[Submit][Statu ...
随机推荐
- less 工具
less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大.less 的用法比起 more 更加的有弹性.在 more 的时候,我们并没有办法向前面翻 ...
- codevs 1183 泥泞的道路 01分数规划
题目链接 题目描述 Description CS有n个小区,并且任意小区之间都有两条单向道路(a到b,b到a)相连.因为最近下了很多暴雨,很多道路都被淹了,不同的道路泥泞程度不同.小A经过对近期天气和 ...
- Web开发者需具备的8个好习惯
优秀的Web开发人员工作效率更高,因为他们拥有丰富的经验和良好的习惯.作者Gregor Dorfbauer分享了用于Web开发中的8个好习惯,这些良好的工作习惯不仅能提高效率,还能让您创建更加优秀的应 ...
- CCNP路由实验(1) -- EIGRP
EIGRP(Enhanced Interior Gateway Routing Protocol,增强型内部网关路由协议)是Cisco公司开发的一个平衡混合型路由协议,它融合了距离向量和链路状态两种路 ...
- cocos2dx 坐标和锚点
cocos2dx中使用opengl坐标系,左下角为坐标原点,在大部分情况下,都是使用这种坐标系的. 当我们创建了一个渲染对象到窗口后,那么这个对象本身也是也是有自己的坐标系的,这种坐标系是节点自己的坐 ...
- 具有 CSA CCM 证明的 SOC 2 可简化 Windows Azure 客户的安全性评估过程
编辑人员注释:本文章由 Windows Azure 产品市场营销总监 Sarah Fender 撰写. 今天,我们宣布 Microsoft 的公共审计师 Deloitte 已经发布了有关 Window ...
- 《Java4Android视频教程》学习笔记(二)
一:面向对象 1.对象 ①对象的使用方法 对象.变量 对象.方法 ②匿名对象 new A().方法 new A().变量 匿名对象会被分配到对内存中 java内存处理机制会对一定时间内无指针指向的对象 ...
- BZOJ 1103 [POI2007]大都市meg(树状数组+dfs序)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1103 [题目大意] 给出一棵树,每条边的经过代价为1,现在告诉你有些路不需要代价了, ...
- 利用 XPath-jQuery 集锦手册在 XPath 和 jQuery 之间做选择
利用 XPath-jQuery 集锦手册在 XPath 和 jQuery 之间做选择 利用 XPath-jQuery 集锦手册在 XPath 和 jQuery 之间做选择
- 网页解析不了PHP源代码的解决方法
一般出现这个错误都是因为修改了apache配置文件,但是没有使apache配置生效. 所以只要执行以下命令就行了: sudo apachectl restart sudo apachectl grac ...