codevs 1733 聪明的打字员 (Bfs)

/*
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=;
len=x.s.length();
for(int i=;i<=len-;i++)
re=re*+x.s[i]-'';
re=re*+x.place;
return re;
}
int main()
{
cin>>ls>>rs;
ls=' '+ls;rs=' '+rs;
node st;st.s=ls;
st.step=;st.place=;
q.push(st);f[Hash(st)]=;
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 ;
}
for(int i=;i<=;i++)
{
int pi,ki;node x;
string ss=si;
if(i==&&p<)
{
pi=p;pi++;
x.place=pi;x.step=t+;x.s=ss;
int hash=Hash(x);
if(f[hash]==)
{
f[hash]=;
q.push(x);
}
}
else if(i==&&ss[p]<'')
{
ss[p]++;pi=p;
x.place=pi;x.step=t+;x.s=ss;
int hash=Hash(x);
if(f[hash]==)
{
f[hash]=;
q.push(x);
}
}
else if(i==&&ss[p]>'')
{
ss[p]--;pi=p;
x.place=pi;x.step=t+;x.s=ss;
int hash=Hash(x);
if(f[hash]==)
{
f[hash]=;
q.push(x);
}
}
else if(i==)
{
char tmps=ss[p];ss[p]=ss[];ss[]=tmps;pi=p;
x.place=pi;x.step=t+;x.s=ss;
int hash=Hash(x);
if(f[hash]==)
{
f[hash]=;
q.push(x);
}
}
else if(i==)
{
char tmps=ss[p];ss[p]=ss[];ss[]=tmps;pi=p;
x.place=pi;x.step=t+;x.s=ss;
int hash=Hash(x);
if(f[hash]==)
{
f[hash]=;
q.push(x);
}
}
else if(i==&&p>)
{
pi=p;pi--;
x.place=pi;x.step=t+;x.s=ss;
int hash=Hash(x);
if(f[hash]==)
{
f[hash]=;
q.push(x);
}
}
}
}
return ;
}
/*
加上剪枝的话就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=;
len=x.s.length();
for(int i=;i<=len-;i++)
re=re*+x.s[i]-'';
re=re*+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=;st.place=;
q.push(st);f[Hash(st)]=;
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 ;
}
for(int i=;i<=;i++)
{
int pi,ki;node x;
string ss=si;
if(i==&&p<)
{
if((p==||p==||p==)&&ss[p]!=rs[p])continue;
pi=p;pi++;
x.place=pi;x.step=t+;x.s=ss;
int hash=Hash(x);
if(f[hash]==)
{
f[hash]=;
q.push(x);
}
}
else if(i==&&ss[p]<'')
{
ss[p]++;pi=p;
x.place=pi;x.step=t+;x.s=ss;
int hash=Hash(x);
if(f[hash]==)
{
f[hash]=;
q.push(x);
}
}
else if(i==&&ss[p]>'')
{
ss[p]--;pi=p;
x.place=pi;x.step=t+;x.s=ss;
int hash=Hash(x);
if(f[hash]==)
{
f[hash]=;
q.push(x);
}
}
else if(i==)
{
char tmps=ss[p];ss[p]=ss[];ss[]=tmps;pi=p;
x.place=pi;x.step=t+;x.s=ss;
int hash=Hash(x);
if(f[hash]==)
{
f[hash]=;
q.push(x);
}
}
else if(i==)
{
char tmps=ss[p];ss[p]=ss[];ss[]=tmps;pi=p;
x.place=pi;x.step=t+;x.s=ss;
int hash=Hash(x);
if(f[hash]==)
{
f[hash]=;
q.push(x);
}
}
else if(i==&&p>)
{
if((p==||p==||p==)&&ss[p]!=rs[p])continue;
pi=p;pi--;
x.place=pi;x.step=t+;x.s=ss;
int hash=Hash(x);
if(f[hash]==)
{
f[hash]=;
q.push(x);
}
}
}
}
return ;
}
上一篇:QMediaPlayer的duration问题


下一篇:java的if语句,少于一行可以省略大括号