转载请注明出处,谢谢: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)】的更多相关文章

  1. 2015 UESTC 搜索专题F题 Eight Puzzle 爆搜

    Eight Puzzle Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 ...

  2. BZOJ_1224_[HNOI2002]彩票_爆搜+打表

    BZOJ_1224_[HNOI2002]彩票_爆搜+打表 Description 某地发行一套彩票.彩票上写有1到M这M个自然数.彩民可以在这M个数中任意选取N个不同的数打圈.每个彩民只能买一张彩票, ...

  3. 2015 UESTC 搜索专题E题 吴队长征婚 爆搜

    吴队长征婚 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Descri ...

  4. BZOJ_4809_皇后_爆搜

    BZOJ_4809_皇后_爆搜 Description 众所不知,rly现在不会玩国际象棋.但是,作为一个OIer,rly当然做过八皇后问题.这里再啰嗦几句,皇后可以攻击到同行同列同对角线,在n*n的 ...

  5. 2015 UESTC 搜索专题C题 基爷与加法等式 爆搜DFS

    基爷与加法等式 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Desc ...

  6. 【BZOJ-1853&2393】幸运数字&Cirno的完美算数教室 容斥原理 + 爆搜 + 剪枝

    1853: [Scoi2010]幸运数字 Time Limit: 2 Sec  Memory Limit: 64 MBSubmit: 1817  Solved: 665[Submit][Status] ...

  7. POJ 1166 The Clocks (爆搜 || 高斯消元)

    题目链接 题意: 输入提供9个钟表的位置(钟表的位置只能是0点.3点.6点.9点,分别用0.1.2.3)表示.而题目又提供了9的步骤表示可以用来调正钟的位置,例如1 ABDE表示此步可以在第一.二.四 ...

  8. hdu4536-XCOM Enemy Unknown(爆搜)

    XCOM-Enemy Unknown是一款很好玩很经典的策略游戏. 在游戏中,由于未知的敌人--外星人入侵,你团结了世界各大国家进行抵抗.随着游戏进展,会有很多的外星人进攻事件.每次进攻外星人会选择3 ...

  9. BZOJ 1207: [HNOI2004]打鼹鼠【妥妥的n^2爆搜,dp】

    1207: [HNOI2004]打鼹鼠 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3259  Solved: 1564[Submit][Statu ...

随机推荐

  1. day6_python学习笔记_chapter8_条件,循环

    1. if elif else 2. 条件表达式:三元操作符: smaller = x if x < y else y   == if x < y : smaller =x  else : ...

  2. Java之工厂模式

    interface Fruit {     void eat(); } class Apple implements Fruit {     public void eat() {         S ...

  3. eclipse注解快捷键

    Search 功能:全局文件内容搜索快捷键: Ctrl + H -------------------------------------------------------------------- ...

  4. HTTP 错误 401.3 - Unauthorized由于 Web 服务器上此资源的访问控制列表(ACL)解决办法

    对应站点目录的IUSR的权限没设造成的...在属性——>安全——> 高级 中把IUSR用户找出来添加好就OK了 注:IUSR(匿名访问 Internet 信息服务的内置帐户)

  5. PHP用户登录与注册页面

    PHP用户登录模块实现 项目包含的功能脚本: login.php//登录 reg.php//注册用户 user_add.php//注册校验脚本 user_login_check.php//登录校验脚本 ...

  6. Streams Studio配置Build options

    Defining build options You can change the build options of the internal builder for building an SPL ...

  7. spoj 1557 GSS3 - Can you answer these queries III 线段树

    题目链接 给出n个数, 2种操作, 一种是将第x个数改为y, 第二种是询问区间[x,y]内的最大连续子区间. 开4个数组, 一个是区间和, 一个是区间最大值, 一个是后缀的最大值, 一个是前缀的最大值 ...

  8. Node爬虫

    Node爬虫 参考 http://www.cnblogs.com/edwardstudy/p/4133421.html 所谓的爬虫就是发送请求,并将响应的数据做一些处理 只不过不用浏览器来发送请求 需 ...

  9. 59 pages的Delphi源码

    http://www.codesc.net/source/list_10_59.shtml

  10. Q_D宏

    Qt 源码中有很多Q_Q和Q_D宏,使用这些宏的地方总会看到有q指针和d指针,查了查KDE文档,大体搞清了其中的机理,欧也!Qt的这些私有数据访问策略还是挺值得借鉴.下面就简单总结一下. 访问器 , ...