zoukankan      html  css  js  c++  java
  • hdu 2209 bfs+状压

    http://acm.hdu.edu.cn/showproblem.php?pid=2209


    不知为啥有种直觉。会出状压+搜索的题,刷几道先

    简单的BFS。状压表示牌的状态,

    //#pragma comment(linker, "/STACK:102400000,102400000")
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <string>
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    #include <map>
    #include <set>
    #include <queue>
    using namespace std;
    
    #define ls(rt) rt*2
    #define rs(rt) rt*2+1
    #define ll long long
    #define ull unsigned long long
    #define rep(i,s,e) for(int i=s;i<e;i++)
    #define repe(i,s,e) for(int i=s;i<=e;i++)
    #define CL(a,b) memset(a,b,sizeof(a))
    #define IN(s) freopen(s,"r",stdin)
    #define OUT(s) freopen(s,"w",stdout)
    const ll ll_INF = ((ull)(-1))>>1;
    const double EPS = 1e-8;
    const double pi = acos(-1.0);
    const int INF = 100000000;
    
    int len,s;
    int vis[1<<21];
    char in[50];
    int legal[25];
    struct Node{
        int s;
        int cnt;
        Node(int ss,int cc):s(ss),cnt(cc){}
    };
    
    void change()
    {
        s=0;
        len=strlen(in);
        for(int i=0;i<len;i++)
        {
            s<<=1;
            if(in[i] == '1')s+=1;
        }
    }
    
    int bfs()
    {
        queue<Node>q;
        q.push(Node(s,0));
        CL(vis,0);
        vis[s]=1;
        while(!q.empty())
        {
            Node tp=q.front();q.pop();///
            if(tp.s==0)return tp.cnt;
            int s,cnt=tp.cnt+1;
            for(int i=0;i<len;i++)
            {
                if(i==0)s=tp.s^3;
                else
                {
                    if(i==len-1)s=tp.s^(3<<(len-2));//
                    else s=(tp.s^(7<<(i-1)));
                }
                if(!vis[s])
                {
                    vis[s]=1;
                    q.push(Node(s,cnt));
                }
    
            }
    
        }
        return -1;
    }
    
    int main()
    {
        //IN("hdu2209.txt");
        while(~scanf("%s",in))
        {
            change();
            int ans=bfs();
            if(ans==-1)puts("NO");
            else printf("%d
    ",bfs());
        }
        return 0;
    }
    

     

  • 相关阅读:
    Win7 64位 php-5.5.13+Apache 2.4.9+mysql-5.6.19 配置
    C# .NET 使用第三方类库DotNetZip解压/压缩Zip rar文件
    64位window7,php5.5.10 +IIS7 配置
    eclipse下编译openfire3.9.1源码
    asp.net 解决IE11下 From身份验证失效问题
    MySQL 数据类型 详解
    ASP.NET安全
    Microsoft Anti-Cross Site Scripting Library V4.2 下载地址
    Entityframework Core去掉外键
    VS2019发布Docker实践
  • 原文地址:https://www.cnblogs.com/zsychanpin/p/6899097.html
Copyright © 2011-2022 走看看