zoukankan      html  css  js  c++  java
  • HDU Collect More Jewels 1044

    BFS + 状态压缩 险过 这个并不是最好的算法 但是写起来比较简单 , 可以AC,但是耗时比较多

    下面是代码 就不多说了

    #include <cstdio>  
    #include <cstring>  
    #include <algorithm>  
    #include <vector>  
    #include <queue>
    using namespace std; 
    #define Max(a,b) (a>b?a:b)
    #define Min(a,b) (a>b?a:b)
    #define maxn 100
    
    int m, n, time, k, sorce[20];
    bool vis[1<<11][51][51];
    char map[52][52];
    
    typedef struct
    {
        int step;
        int x, y;
        int sorce;
        int stau;
    }Point;
    
    int bfs(Point P);
    
    int main()
    {
        int i, j, T, ans, count = 1;
        Point P;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d%d%d%d",&n,&m,&time,&k);
            
            for(i=0; i<k; i++)
                scanf("%d",&sorce[i]);
            
            for(i=0; i<m; i++)
            {
                scanf("%s",map[i]);
                
                for(j=0; j < n; j++)
                {
                    if(map[i][j] == '@')
                        P.x = i, P.y = j, P.step = P.sorce = P.stau = 0;
                }
            }
            
            ans = bfs(P);
            
            printf("Case %d:
    ",count++);
            if(ans == -1)
                printf("Impossible
    ");
            else
                printf("The best score is %d.
    ",ans);
            if(T)
                printf("
    ");
        }
        return 0;
    }
    
    int bfs(Point P)
    {
        int i, max = -1, key;
        int dir[4][2] = {1,0,0,1,-1,0,0,-1};
        Point Pn;
        queue <Point> q;
        q.push(P);
        memset(vis,0,sizeof(vis));
        vis[P.stau][P.x][P.y] = 1;
        while( !q.empty() )
        {
            P = q.front();
            q.pop();
            for(i=0; i<4; i++)
            {
                Pn.x = P.x + dir[i][0];
                Pn.y = P.y + dir[i][1];
                Pn.step = P.step + 1;
                Pn.sorce = P.sorce;
                Pn.stau = P.stau;
                if(Pn.step > time)
                    break;
                if(Pn.x>=0 && Pn.x<m && Pn.y>=0 && Pn.y<n && map[Pn.x][Pn.y] != '*')
                {
                    if(map[Pn.x][Pn.y] == '<')
                    {
                        max = Max(max,Pn.sorce);
                        if(Pn.stau == ((1<<k)-1) )
                            return max;
                    }
                    else if(map[Pn.x][Pn.y] >= 'A' && map[Pn.x][Pn.y] <= 'J')
                    {
                        key = map[Pn.x][Pn.y] - 'A';
                        if( ((Pn.stau)&(1<<key)) == 0 )
                        {
                            Pn.sorce += sorce[key];
                            Pn.stau += 1<<key;
                        }
                    }
                    if( !vis[Pn.stau][Pn.x][Pn.y])
                    {
                        vis[Pn.stau][Pn.x][Pn.y] = 1;
                        q.push(Pn);
                    }
                    
                }
            }
        }
        return max;
    }
  • 相关阅读:
    python3.x元组打印错误 TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple'
    LoRa---她的简介和她的专业术语
    单片机程序在内存和FLASH中的空间分配
    STM32烧录的常用方式
    【原创】MapReduce编程系列之表连接
    Maven基础配置—本地Maven配置
    Maven基础配置--nexus私服配置
    OSGI入门笔记
    Maven仓库搭建--nexus私服
    javascript基础 方法 函数 闭包 集合
  • 原文地址:https://www.cnblogs.com/chenchengxun/p/3914644.html
Copyright © 2011-2022 走看看