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 ...
随机推荐
- 在Ubuntu 11.10工具栏上用数字显示网速、CPU负荷和内存占用量『译』
基本上照抄了<How To Display Network Upload / Download Speed On The Panel In Ubuntu 11.04>,只不过我的实践环境是 ...
- Oracle ODI系列之一(ODI知识模块)
Oracle ODI系列之一(ODI知识模块) ODI简介 ODI(Oracle Data Integrator)前身是Sunopsis Active Integration Platform ...
- 在Linux下使用iconv转换字符串编码
在Linux下写C程序,尤其是网络通信程序时经常遇到编码转换的问题,这里要用到iconv函数库. iconv函数库有以下三个函数 123456 #include <iconv.h>icon ...
- [置顶] What is the difference between Category and Class Extension?
细心的人会发现当我们new 一个文件的时候会发现下图的部分. 但是这个问题来了Category 和 Extension 就近又什么区别呢? 1:什么是Category? 实现这样一种场景,当我们用我们 ...
- hdu4491 Windmill Animation(计算几何)
Windmill Animation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- gallery 从最左边开始显示并且默认选中第一个
import android.content.Context; import android.graphics.Camera; import android.graphics.Matrix; impo ...
- Javascript高级程序设计学习笔记一
看完w3school的javascript的概念,有点基础,开始红皮书的路程,今晚总结前二章的心得. 第一章:javascript简介 重点是javascript的实现是由 ECMAScript(核心 ...
- angularJS使用$watch监控数据模型的变化
使用$watch监控数据模型的变化 在scope 内置的全部函数中,用得最多的可能就是$watch 函数了.当你的数据模型中某一部分发生变化时,$watch 函数能够向你发出通知.你能够监控单个对象的 ...
- FusionCharts 学习总结
FusionCharts和Charts一样都是对数据进行统计并绘制成图标的控件,但FusionCharts带有跨浏览器的flash图表组件解决方案,功能更为强大.. 在这里我将采用Js来加载Fusio ...
- JavaSE_ Java基础 总目录(1~6)
JavaSE学习总结第01天_Java概述01.01 计算机概述01.02 计算机硬件和软件概述01.03 软件开发和计算机语言概述01.04 人机交互01.05 键盘功能键和快捷键01.06 如何打 ...