zoukankan      html  css  js  c++  java
  • vijosP1026毒药?解药?

    hash。

    怎么感觉叫状态压缩bfs比较合适呢?

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    const int maxn = 10 + 5;
    const int maxm = 100 + 10;
    const int maxg = 1024 + 10;
    
    bool st[maxn],h[maxg];
    int mp[maxm][maxn];
    int d[maxg][maxn];
    int n,m,finish;
    
    inline int Hash(bool a[]) {
        int x=1,s=0;
        for(int i=1;i<=n;i++) {
            s+=x*a[i];
            x*=2;        
        }
        return s;
    }
    
    void bfs() {
        int l=0,r=1;
        finish=(1<<n)-1;
        while(l<r) {
            for(int i=1;i<=m;i++) {
                for(int j=1;j<=n;j++) {
                    if(mp[i][j]==1) st[j]=1;
                    else if(mp[i][j]==-1) st[j]=0;
                    else st[j]=d[l][j];    
                }
                int x=Hash(st);
                if(x==finish) {
                    printf("%d
    ",d[l][0]+1);    
                    return;
                }
                if(!h[x]) {
                    h[x]=1;
                    d[r][0]=d[l][0]+1;
                    for(int j=1;j<=n;j++) d[r][j]=st[j];
                    r++;    
                }
            }
            l++;
        }
        printf("The patient will be dead.
    ");
    }
    
    int main() {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++)
            for(int j=1;j<=n;j++)
                scanf("%d",&mp[i][j]);
        bfs();
        return 0;
    }
  • 相关阅读:
    swift 学习线路
    常用linux命令
    位运算常用技巧
    我的算法练习
    mac 电脑配置cordova
    swift 2中关键字和解释整理
    C# 类
    C# 循环语句 for循环(嵌套 while 穷举 迭代)
    C# 循环语句 for循环
    C# 分支语句
  • 原文地址:https://www.cnblogs.com/invoid/p/5539107.html
Copyright © 2011-2022 走看看