码迷,mamicode.com
首页 > 其他好文 > 详细

codevs 1733 聪明的打字员 (Bfs)

时间:2016-06-11 21:29:47      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:

/*
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;
}

 

codevs 1733 聪明的打字员 (Bfs)

标签:

原文地址:http://www.cnblogs.com/yanlifneg/p/5575883.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!