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[];
bool vis[<<][][];
char map[][]; typedef struct
{
int step;
int x, y;
int sorce;
int stau;
}Point; int bfs(Point P); int main()
{
int i, j, T, ans, count = ;
Point P;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d",&n,&m,&time,&k); for(i=; i<k; i++)
scanf("%d",&sorce[i]); for(i=; i<m; i++)
{
scanf("%s",map[i]); for(j=; j < n; j++)
{
if(map[i][j] == '@')
P.x = i, P.y = j, P.step = P.sorce = P.stau = ;
}
} ans = bfs(P); printf("Case %d:\n",count++);
if(ans == -)
printf("Impossible\n");
else
printf("The best score is %d.\n",ans);
if(T)
printf("\n");
}
return ;
} int bfs(Point P)
{
int i, max = -, key;
int dir[][] = {,,,,-,,,-};
Point Pn;
queue <Point> q;
q.push(P);
memset(vis,,sizeof(vis));
vis[P.stau][P.x][P.y] = ;
while( !q.empty() )
{
P = q.front();
q.pop();
for(i=; i<; i++)
{
Pn.x = P.x + dir[i][];
Pn.y = P.y + dir[i][];
Pn.step = P.step + ;
Pn.sorce = P.sorce;
Pn.stau = P.stau;
if(Pn.step > time)
break;
if(Pn.x>= && Pn.x<m && Pn.y>= && Pn.y<n && map[Pn.x][Pn.y] != '*')
{
if(map[Pn.x][Pn.y] == '<')
{
max = Max(max,Pn.sorce);
if(Pn.stau == ((<<k)-) )
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)&(<<key)) == )
{
Pn.sorce += sorce[key];
Pn.stau += <<key;
}
}
if( !vis[Pn.stau][Pn.x][Pn.y])
{
vis[Pn.stau][Pn.x][Pn.y] = ;
q.push(Pn);
} }
}
}
return max;
}

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

  1. hdu Collect More Jewels

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

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

    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 Collect More Jewels(bfs+状态压缩)

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

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

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

  6. hdu 1044 Collect More Jewels

    题意: 一个n*m的迷宫,在t时刻后就会坍塌,问:在逃出来的前提下,能带出来多少价值的宝藏. 其中: ’*‘:代表墙壁: '.':代表道路: '@':代表起始位置: '<':代表出口: 'A'~ ...

  7. hdu 1044(bfs+状压)

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

  8. HDU 1044 BFS

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

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

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

随机推荐

  1. (tomcat访问不了的两种解决方法)Bad Request(Invalid Hostname)

    显示这个页面的时候一般有几中解决方法: 第一种就是如下图所示的方法: 具体步骤是: 1.也就是左下角win的“运行”中输入cmd进入doc窗口中 2.输入代码:netstat -ano 3.找到占用8 ...

  2. TypeScript笔记[5]泛型+Dictionary 转

    TypeScript笔记[5]泛型   在C++.C#.Java等主流编程语言中,一般对泛型编程提供了支持.合理利用泛型,可以提高开发效率.提升代码质量. 例如在C++编程语言中,常常利用下面的结构表 ...

  3. eclipse alt + '/' not working.

    searching for google,I observed that the 'content assist' shortcut key was take placed with 'ctrl + ...

  4. oracle约束条件状态

    Oracle完整性约束有一下4种: • DISABLE NOVALIDATE • ENABLE NOVALIDATE • DISABLE VALIDATE • ENABLE VALIDATE   •  ...

  5. ios里面如何压缩图片

    在iOS里面,压缩图片跟在其他环境里面差不多,都和累死, 就是对当前图片从新画图,制定一个尺寸的问题 UIImage* image = [UIImage imageNamed:@"cat.j ...

  6. 读懂IL代码(二)

    上一篇提到了最基本的IL代码,应该是比较通俗易懂的,所以有了上一篇的基础之后,这篇便要深入一点点的来讲述了. 首先我必须再来说一些重要的概念: Evaluation Stack(评估栈):这是由.NE ...

  7. ejs 基本语法

    1.基本语法.<% code %>    无缓冲的条件语句元素.<%= code %>    转义HTML,该code并且会打印出来.<%- code %>     ...

  8. Template 模式

    Template 模式是很简单模式,但是也应用很广的模式.Template 是采用继承的方式实现算法的异构,其关键点就是将通用算法封装在抽象基类中,并将不同的算法细节放到子类中实现.Template ...

  9. SGU 163.Wise King

    一道题目长的水题.... 总结就一句话,给出n个(-3~3)的数,一个数m,取任意个数是使这些数的m次幂之和最大. code #include <iostream> #include &l ...

  10. ubuntu 解压,压缩

    .rar解压:rar x FileName.rar压缩:rar a FileName.rar DirName