zoukankan      html  css  js  c++  java
  • 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=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;
    }
  • 相关阅读:
    source命令
    [电脑配置]屏幕扩展过,找不到界面
    [SAS]方便查询Tips
    [Excel]方便查询Tips
    [SAS]运用函数等的一些问题
    [SAS]错误整理
    [SAS]易错例子之数值型转字符型
    [R]Precedence
    [sas]Missing Value
    [SAS]
  • 原文地址:https://www.cnblogs.com/yanlifneg/p/5575883.html
Copyright © 2011-2022 走看看