题意:

  一个n*m的迷宫,在t时刻后就会坍塌,问:在逃出来的前提下,能带出来多少价值的宝藏。

其中:

  ’*‘:代表墙壁;

  '.':代表道路;

  '@':代表起始位置;

  '<':代表出口;

  'A'~'J':代表宝藏;

解题思路:

  本题有两种思路:1,bfs+状态压缩;相比之下耗时多;

          2,bfs+dfs;耗时较少;

在这里着重介绍第一种方法:(博主也是现学现卖啦^_^)

  因为宝藏的数目比较少,所以我们可以对宝藏进行状态压缩(用二进制表示宝藏是否已经拾取,1表示已拾取,0表示未拾取),

  标记数组vis[i][x][y],i就是宝藏的拾取状态,(x,y)是当前搜索位置;(当然知道这些也不一定能ac)。

注意:(想要把题目ac的看过来)

  1:不可能输出“Impossible”。

  2:题目中的数据问题,题目的例子相当于有一个围墙,而可能数据并不总是如此。

  3:首先是如何判断当前的路径是否应该结束(并不是到‘<’为止,因为可能还有时间到达其他的宝藏)。

下面就祝大家ac愉快啦

代码:

 #include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
#define N 55 struct node
{
int x, y, z, t, val;
}; int n, m, t, num;
int vis[<<][N][N], va[], dir[][] = {,, -,, ,, ,-};
char map[N][N]; int bfs (int sx, int sy)
{
int num = -; queue<node>Q;
node cur, next; cur.x = sx, cur.y = sy;
cur.val = cur.t = cur.z = ;
memset (vis, , sizeof(vis));
vis[][sx][sy] = ; Q.push(cur);
while (!Q.empty())
{
cur = Q.front();
Q.pop(); if (map[cur.x][cur.y] == '<')
{
if (cur.val > num)
num = cur.val;
}
if (cur.t > t)
return num;
for (int i=; i<; i++)
{
next.x = cur.x + dir[i][];
next.y = cur.y + dir[i][];
next.z = cur.z;
next.val = cur.val; if (next.x<m && next.x>= && next.y<n && next.y>=)
{
next.t = cur.t + ;
if (map[next.x][next.y] >= 'A' && map[next.x][next.y] <= 'Z')
{
int l = map[next.x][next.y] - 'A';
if ( !((<<l) & next.z) )
{
next.val += va[l];
next.z = (<<l) | next.z;
}
}
if (!vis[next.z][next.x][next.y] && map[next.x][next.y] != '*')
Q.push(next), vis[next.z][next.x][next.y] = ;
}
}
}
return num;
}
int main ()
{
int c, i, j, s, l = , sx, sy; scanf ("%d", &c); while (c --)
{
scanf ("%d %d %d %d", &n, &m, &t, &s); for (i=; i<s; i++)
scanf ("%d", &va[i]); for (i=; i<m; i++)
{
scanf ("%s", map[i]);
for (j=; j<n; j++)
if (map[i][j] == '@')
sx = i, sy = j;
} num = bfs (sx, sy);
if (l)
printf ("\n");
if (num != -)
printf ("Case %d:\nThe best score is %d.\n", ++l, num);
else
printf ("Case %d:\nImpossible\n", ++l, num);
} return ;
}

hdu 1044 Collect More Jewels的更多相关文章

  1. HDU 1044 Collect More Jewels(BFS+DFS)

    Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

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

    Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

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

    Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  4. hdu 1044(bfs+状压)

    非常经典的一类题型 没有多个出口.这里题目没有说清楚 Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limi ...

  5. HDU 1044 BFS

    Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  6. hdu 1044(bfs+dfs+剪枝)

    Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  7. Collect More Jewels(hdu1044)(BFS+DFS)

    Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  8. HDU Collect More Jewels 1044

    BFS + 状态压缩 险过 这个并不是最好的算法 但是写起来比较简单 , 可以AC,但是耗时比较多 下面是代码 就不多说了 #include <cstdio> #include <c ...

  9. hdu Collect More Jewels

    思路: 先用bfs求出入口,宝物,出口,两两之间的最短距离. 在用dfs搜索所有情况,求出从入口走到出口能获得的最大价值. 我们要解决几个问题:1.求入口到第一个取宝物的地方的最短距离 2.求第i个取 ...

随机推荐

  1. Spring @Value用法

    Spring 通过注解获取*.porperties文件的内容,除了xml配置外,还可以通过@value方式来获取. 使用方式必须在当前类使用@Component,xml文件内配置的是通过pakage扫 ...

  2. java集合框架 hashMap 简单使用

    参考文章:http://blog.csdn.net/itm_hadf/article/details/7497462 通常,默认加载因子 (.75) 在时间和空间成本上寻求一种折衷.      加载因 ...

  3. 解决运行scrapy是报错No module named cryptography,解决cryptography的安装问题,解决libffi的安装问题

    在linux下配置完运行是出现ImportError: No module named cryptography.hazmat.bindings.openssl.binding的错误.原因是crapt ...

  4. 爬取指定网页的源代码显示在GUI中

    建立一个GUI图形界面用来用来输入网址和代码显示的区域 #encoding=utf-8 __author__ = 'heng' #创建一个可以抓取输入网址源代码的GUI from urllib2 im ...

  5. Hibernate4之session核心方法

    在学习session的核心方法之前,我们先了解下hibernate中几种对象的状态: 暂时状态:这样的状态就好像咱们公司请的暂时员工一样,他在公司里没有相关的资料和id. 特点:在使用代理主键的情况下 ...

  6. 使用正則表達式的格式化与高亮显示json字符串

    使用正則表達式的格式化与高亮显示json字符串 json字符串非常实用,有时候一些后台接口返回的信息是字符串格式的,可读性非常差,这个时候要是有个能够格式化并高亮显示json串的方法那就好多了,以下看 ...

  7. 【PA2013】【BZOJ3733】Iloczyn

    Description 给定正整数n和k,问是否能将n分解为k个不同正整数的乘积 Input 第一行一个数T(T<=4000)表示測试组数 接下来T行每行两个数n(n<=10^9),k(k ...

  8. git clone新项目后如何拉取其他分支代码到本地

    1.git clone git@git.n.xxx.com:xxx/xxx.git 2.git fetch origin dev 命令来把远程dev分支拉到本地 - - 解读:git fetch命令用 ...

  9. JAVA学习(七):方法重载与方法重写、thiskeyword和superkeyword

    方法重载与方法重写.thiskeyword和superkeyword 1.方法重载 重载可以使具有同样名称但不同数目和类型參数的类传递给方法. 注: 一是重载方法的參数列表必须与被重载的方法不同,而且 ...

  10. javaScript定义函数的三种方式&amp;变量的作用域

    一.函数定义 方式1.普通方式定义函数 function 函数名(參数n){ 函数体 } function add(a,b){ return a+b; } 方式2.直接量定义函数 var 函数名=fu ...