hdu 1044 Collect More Jewels

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1044

题目大意:给出矩阵的长宽,时间限制,宝石的个数。

[.]:可走;

[*]:不可走;

[@]:起点;

[>]:出口;

[A~J]:宝石;

题目分析:本来以为和之前的题目没有什么区别,后来仔细看了样例3才发现,原来也并不是走过的路就不能走,看了其他人的感觉用vis[x][y][stauts]记录比较符合我之前的想法,正好10颗宝石的话也数组也开得下,同一个点相同状态下就不能再走。

代码参考:

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 100;
const int M = 1<<11; //从0~1111111111
char g[N][N];
int val[N];
int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int limit, maxVal, jewel, n, m;
bool vis[N][N][M]; //记录某个点在某个状态下是否被访问过

struct Node
{
    int x, y, step, value, stauts;
};
queue<Node> q;

int bfs(Node s)
{
    q.push(s);
    Node head;
    while(!q.empty()) {
        head = q.front();
        q.pop();
        if(head.step > limit) return maxVal; //如果超过了时间限制,就返回当前最大值
        for(int i = 0; i < 4; ++ i) {
            int x = head.x + dir[i][0];
            int y = head.y + dir[i][1];
            int s = head.stauts;
            if(x >= 0 && x < m && y >= 0 && y < n && g[x][y] != '*') {
                Node t;
                t.x = x;
                t.y = y;
                t.step = head.step + 1;
                t.value = head.value;
                t.stauts = s;
                if(t.step > limit) break; //如果已经超过了时间限制,就不再计算下去
                if(g[x][y] >= 'A' && g[x][y] <= 'J') { //如果是宝石
                    int id = g[x][y] - 'A';
                    if((t.stauts & (1 << id)) == 0) { //如果这个宝石没有获得过
                        t.value += val[id]; //加上当前宝石的价格
                        t.stauts |= (1 << id); //改变当前的状态
                    }
                } else if(g[x][y] == '<') { //如果到了出口
                    if(maxVal < t.value) { 
                        maxVal = t.value; //更新最大值
                    }
                    if(t.stauts == (1 << jewel) - 1) return maxVal; //如果全部宝石都拿到了,就返回当前的最大值
                }
                if(!vis[x][y][t.stauts]) { //如果这个点这个状态没有达到过
                    vis[x][y][t.stauts] = 1;
                    q.push(t);
                }
            }
        }
    }
    return maxVal;
}

int main()
{
    int t, k = 1;
    scanf("%d", &t);
    while(t --) {
        scanf("%d%d%d%d", &n, &m, &limit, &jewel);
        for(int i = 0; i < jewel; ++ i) {
            scanf("%d", &val[i]);
        }
        while(!q.empty()) q.pop(); //记得初始化,因为并不是只有当队列为空的时候返回函数值的
        memset(vis, 0, sizeof(vis));
        maxVal = -1;
        Node start;
        start.step = 0;
        start.value = 0;
        start.stauts = 0;
        for(int i = 0; i < m; ++ i) {
            scanf("%s", g[i]);
            int len = strlen(g[i]);
            for(int j = 0; j < len; ++ j) {
                if(g[i][j] == '@') {
                    start.x = i, start.y = j;
                    vis[i][j][0] = 1; 
                }
            }
        }
        maxVal = bfs(start);
        if(k > 1) puts(""); //注意输出格式
        printf("Case %d:\n", k ++);
        if(maxVal == -1) {
            puts("Impossible");
        } else {
            printf("The best score is %d.\n", maxVal);
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值