题意:

  一个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. arcgis安装路径的获得

    //Get the ArcGIS install location string sInstall = ESRI.ArcGIS.RuntimeManager.ActiveRuntime.Path; / ...

  2. jmete命令行停止失败的原因分析

    1.在jmeter的master机器上使用如下方式启动远程IP地址2.2.2.2,3.3.3.3上的jmeter slave服务,执行到最后生成报告: sh apache-jmeter-3.1/bin ...

  3. Linux监測某一时刻对外的IP连接情况

    相信大家都熟悉netstat命令吧,这里就主要採用此命令.网上流传的DDoS Deflate工具就是採用IP数量来统计对外连接数,然后结合Iptables的方法来实现某个IP增加黑名单和解禁某IP n ...

  4. hibernate4中HHH000273的错误

    今天配置hibernate4.发现报 17:55:06,815 INFO AbstractPoolBackedDataSource:522 - Initializing c3p0 pool... co ...

  5. NS3网络仿真(12): ICMPv4协议

    快乐虾 http://blog.csdn.net/lights_joy/ 欢迎转载,但请保留作者信息 ICMP的全称是 Internet ControlMessage Protocol . 其目的就是 ...

  6. Python开发【第2节】【Python运算符】

    Python语言支持以下类型的运算符: 算术运算符 比较(关系)运算符 赋值运算符 逻辑运算符 位运算符 成员运算符 身份运算符 运算符优先级 1.算术运算符 假设变量a = 10,变量b = 21: ...

  7. java8--网络编程(java疯狂讲义3复习笔记)

    重点复习一下网络通信和代理 java的网络通信很简单,服务器端通过ServerSocket建立监听,客户端通过Socket连接到指定服务器后,通信双方就可以通过IO流进行通信. 需要重点看的工具类:I ...

  8. 替换Android自带apk【转】

    本文转载自:http://www.voidcn.com/article/p-gonowdjh-vz.html 安卓自带的app放在/system/app/下,当我们想要替换这些应用时可以参考如下步骤: ...

  9. html5--js函数在canvas中的应用

    html5--js函数在canvas中的应用 总结: 1.script中的函数写了要调用 2.rgb()这样的模式的色彩比较适合做变量 3.body的onload事件 4.带参函数 效果: 代码: & ...

  10. POJ - 2418 Hardwood Species(map,trie,BST)

    1.输入若干行树名,输入结束后,按字典序输出树名及其所占百分比. 2.多种方法:map,trie,BST 3. map: #include<iostream> #include<st ...