题意:

  一个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. Github配置SSH

    以前也配置过ssh,但是没有注意用法,在配置一次熟悉流程 检查本机是否有ssh key设置 $ cd ~/.ssh 或cd .ssh 如果没有则提示: No such file or director ...

  2. centos 安装python2.7

    安装pip sudo yum -y install epel-release sudo yum -y install python-pip 下载解压Python-2.7.3 #wget http:// ...

  3. Redis系列之-—Redis-cli命令总结【转】

    Redis-cli命令最新总结 参考资料: http://redisdoc.com/ 或者 http://doc.redisfans.com http://redis.io/commands 一. 进 ...

  4. 我的第一个开源控件-DragGridView

    我的第一个开源控件出炉了,希望各个小伙伴给个star,支持下.项目地址 1. 前言 因为项目须要,要做一个相似腾讯视频.频道管理.拖拽排序的效果.这个控件是在原地址 之上改造出来的.先看下效果图. 1 ...

  5. C++手稿:std::string

    字符串在非常多编程语言中已经成为基本数据类型,C语言中我们使用char*来手动申请和维护字符串, 在C++中,能够使用std::string来方便地创建和操作字符串. string是一个模板类.它有b ...

  6. create-react-app 使用 webpack 打包压缩失败

    问题,正常 npm run build 打包后,发现打包后的文件异常大,有 > 20M 的大小 分析, 1.起初以为是代码本身过大的原因导致的,所以一直在想如何进行代码拆分使得文件能尽可能的小, ...

  7. JavaScript基本类型与引用类型

    前面已经说过,JavaScript变量是松散类型,它可以保存任何类型的值.变量的值以及数据类型可以在脚本的生命周期内发生改变.变量包含两种不同类型的值:基本类型和引用类型.基本类型值的是简单的数据段, ...

  8. 实现@using{}代码块

    前几天,我要写个插件,放在asp.net mvc的view上.这需要写一些扩展HtmlHelper功能的方法.这些方法的一个参数是一段javascript代码. 假如这个参数类型是字符型,当然很简单了 ...

  9. 2015/12/29 eclipse 设置要点 空间 项目 类 eclipse汉化

    开始使用eclipse,双击eclipse.exe文件,启动eclipse.程序会显示一个工作空间的对话框,工作空间用来存放你的项目文件,你可以使用程序默认的,点击确定即可,你也可以重新选择一个文件夹 ...

  10. jupyter环境的安装

    1,什么是jupyter notebook? 简介:jupyter notebook是基于网页的用户交互计算机的应用程序,其可被用于全过程计算:开发,文档编写,运行代码,和展示结果 简而言之,Jupy ...