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:\n",count++);
        if(ans == -1)
            printf("Impossible\n");
        else
            printf("The best score is %d.\n",ans);
        if(T)
            printf("\n");
    }
    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;
}

HDU Collect More Jewels 1044

时间: 2024-08-29 03:02:43

HDU Collect More Jewels 1044的相关文章

hdu Collect More Jewels

思路: 先用bfs求出入口,宝物,出口,两两之间的最短距离. 在用dfs搜索所有情况,求出从入口走到出口能获得的最大价值. 我们要解决几个问题:1.求入口到第一个取宝物的地方的最短距离 2.求第i个取宝物的地方到第i+1个取宝物的地方的最短距离 3.求第n个取宝物的地方到出口的最短距离 4.保证以上3点能在时间L内实现的情况下,取得的宝石价值最大 熟悉两种搜索的优缺点: BFS: 对于解决最短或最少问题特别有效,而且寻找深度小,但缺点是内存耗费量大(需要开大量的数组单元用来存储状态). DFS:

HDU 1044 Collect More Jewels 【经典BFS+DFS】

Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6597    Accepted Submission(s): 1527 Problem Description It is written in the Book of The Lady: After the Creation, the cruel

HDU 1044 Collect More Jewels【BFS+DFS+建立距离图】

Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6707    Accepted Submission(s): 1556 Problem Description It is written in the Book of The Lady: After the Creation, the cruel

hdu.1044.Collect More Jewels(bfs + 状态压缩)

Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5345    Accepted Submission(s): 1189 Problem Description It is written in the Book of The Lady: After the Creation, the cruel

Collect More Jewels(hdu1044)(BFS+DFS)

Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6857    Accepted Submission(s): 1592 Problem Description It is written in the Book of The Lady: After the Creation, the cruel

hdu 1044 Collect More Jewels

并没有做过这种类型的题目,不太会做...看了一下大神的代码,然后自己敲...额..思路一样了,代码也差不多.. http://www.cnblogs.com/kuangbin/archive/2012/08/14/2637512.html 先通过BFS预处理任意两点之间的距离,然后通过DFS暴力模拟路径,算出最优解. 感觉自己可能对BFS理解比DFS更深一点,或者说,BFS比较简单一点吧... 这题还有一种解法是状态压缩+BFS...通过开设一个int型变量记录是否拿到宝物,然后暴力..不过这种

HDU ACM 1044 Collect More Jewels BFS+DFS

题意:在一个迷宫中,有一些宝物,从起点走到终点,问在给定的时间内,到达终点后所能拾取珠宝的最大价值. 分析(BFS+DFS): 1.求入口到第一个取宝物的地方的最短距离 2.求第i个取宝物的地方到第i+1个取宝物的地方的最短距离 3.求第n个取宝物的地方到出口的最短距离 4.保证以上3点能在时间L内实现的情况下,取得的宝石价值最大. BFS特点:对于解决最短或最少问题特别有效,而且寻找深度小,但缺点是内存耗费量大(需要开大量的数组单元来存储状态) DFS特点:对于解决遍历和求所有问题有效,对于问

1044 [Collect More Jewels] DFS+BFS

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1044 题目大意:在地图中,有M个宝石,每个宝石有不同价值.问在时间限制L之内,从入口到出口这一路上获得的最大价值是多少.拿宝石不额外花时间,走一格用时为1. 关键思想:考虑到BFS和DFS的特点,BFS在解决最短(小)问题是很有效,但内存耗费巨大:DFS可以解决绝大多数搜索问题,但层数较深时,时间开销和栈的开销都很大. 这道题,只用DFS显然是不行的,地图比较大了.但是只用BFS也不行,因为取完之后

hdu 1044(bfs+dfs+剪枝)

Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6739    Accepted Submission(s): 1564 Problem Description It is written in the Book of The Lady: After the Creation, the cruel