您的位置:首页 > 其它

codevs 1733 聪明的打字员 (Bfs)

2016-06-11 20:38 405 查看
/*
Bfs+Hash 跑的有点慢 但是codevs上时间限制10s 也ok
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define maxn 10000010
using namespace std;
int len;
bool f[maxn];
string ls,rs;
struct node
{
int step,place;
string s;
};
queue<node>q;
int Hash(node x)
{
int re=0;
len=x.s.length();
for(int i=1;i<=len-1;i++)
re=re*10+x.s[i]-'0';
re=re*10+x.place;
return re;
}
int main()
{
cin>>ls>>rs;
ls=' '+ls;rs=' '+rs;
node st;st.s=ls;
st.step=0;st.place=1;
q.push(st);f[Hash(st)]=1;
while(!q.empty())
{
node k=q.front();q.pop();
string si=k.s;
int p=k.place,t=k.step;
if(si==rs)
{
cout<<t;
return 0;
}
for(int i=1;i<=6;i++)
{
int pi,ki;node x;
string ss=si;
if(i==1&&p<6)
{
pi=p;pi++;
x.place=pi;x.step=t+1;x.s=ss;
int hash=Hash(x);
if(f[hash]==0)
{
f[hash]=1;
q.push(x);
}
}
else if(i==2&&ss[p]<'9')
{
ss[p]++;pi=p;
x.place=pi;x.step=t+1;x.s=ss;
int hash=Hash(x);
if(f[hash]==0)
{
f[hash]=1;
q.push(x);
}
}
else if(i==3&&ss[p]>'0')
{
ss[p]--;pi=p;
x.place=pi;x.step=t+1;x.s=ss;
int hash=Hash(x);
if(f[hash]==0)
{
f[hash]=1;
q.push(x);
}
}
else if(i==4)
{
char tmps=ss[p];ss[p]=ss[1];ss[1]=tmps;pi=p;
x.place=pi;x.step=t+1;x.s=ss;
int hash=Hash(x);
if(f[hash]==0)
{
f[hash]=1;
q.push(x);
}
}
else if(i==5)
{
char tmps=ss[p];ss[p]=ss[6];ss[6]=tmps;pi=p;
x.place=pi;x.step=t+1;x.s=ss;
int hash=Hash(x);
if(f[hash]==0)
{
f[hash]=1;
q.push(x);
}
}
else if(i==6&&p>1)
{
pi=p;pi--;
x.place=pi;x.step=t+1;x.s=ss;
int hash=Hash(x);
if(f[hash]==0)
{
f[hash]=1;
q.push(x);
}
}
}
}
return 0;
}


/*
加上剪枝的话就ok了 200ms
对于2 3 4 5这几个点 左移右移对答案是没有贡献的 只有1 6 左移右移再加上swap0 1才有贡献
所以2 3 4 5这几个只有已经和目标相同了才左右移
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define maxn 10000010
using namespace std;
int len;
bool f[maxn];
string ls,rs;
struct node
{
int step,place;
string s;
};
queue<node>q;
int Hash(node x)
{
int re=0;
len=x.s.length();
for(int i=1;i<=len-1;i++)
re=re*10+x.s[i]-'0';
re=re*10+x.place;
return re;
}
int main()
{
//freopen("clever.in","r",stdin);
//freopen("clever.out","w",stdout);
cin>>ls>>rs;
ls=' '+ls;rs=' '+rs;
node st;st.s=ls;
st.step=0;st.place=1;
q.push(st);f[Hash(st)]=1;
while(!q.empty())
{
node k=q.front();q.pop();
string si=k.s;
int p=k.place,t=k.step;
if(si==rs)
{
cout<<t;
return 0;
}
for(int i=1;i<=6;i++)
{
int pi,ki;node x;
string ss=si;
if(i==1&&p<6)
{
if((p==2||p==3||p==4)&&ss[p]!=rs[p])continue;
pi=p;pi++;
x.place=pi;x.step=t+1;x.s=ss;
int hash=Hash(x);
if(f[hash]==0)
{
f[hash]=1;
q.push(x);
}
}
else if(i==2&&ss[p]<'9')
{
ss[p]++;pi=p;
x.place=pi;x.step=t+1;x.s=ss;
int hash=Hash(x);
if(f[hash]==0)
{
f[hash]=1;
q.push(x);
}
}
else if(i==3&&ss[p]>'0')
{
ss[p]--;pi=p;
x.place=pi;x.step=t+1;x.s=ss;
int hash=Hash(x);
if(f[hash]==0)
{
f[hash]=1;
q.push(x);
}
}
else if(i==4)
{
char tmps=ss[p];ss[p]=ss[1];ss[1]=tmps;pi=p;
x.place=pi;x.step=t+1;x.s=ss;
int hash=Hash(x);
if(f[hash]==0)
{
f[hash]=1;
q.push(x);
}
}
else if(i==5)
{
char tmps=ss[p];ss[p]=ss[6];ss[6]=tmps;pi=p;
x.place=pi;x.step=t+1;x.s=ss;
int hash=Hash(x);
if(f[hash]==0)
{
f[hash]=1;
q.push(x);
}
}
else if(i==6&&p>1)
{
if((p==2||p==3||p==4)&&ss[p]!=rs[p])continue;
pi=p;pi--;
x.place=pi;x.step=t+1;x.s=ss;
int hash=Hash(x);
if(f[hash]==0)
{
f[hash]=1;
q.push(x);
}
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: