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. Java 原始数据类型转换

    在开发中经常遇到数据类型转换的问题,大多数都是拿来强制转换,强制转换可能会出现你意想不到的问题: int a = -1; 我们经过多重转换之后:int b = (int)(char)(byte) a ...

  2. react初识

    如下是在研究中记录的笔记: 1,作用:局部的更新dom结构;虚拟dom保证性能2,和mvc不同,mvc是对于技术上的分离(分类),而react是组件上的分离,每个视图模块分离,复用,以视图模块为单位3 ...

  3. Html5新增的语义化标签(部分)

    2014年10月29日,万维网联盟宣布,经过接近8年的艰苦努力,html5的标准规范终于制定完成.这是互联网的一次重大变革,这也许是一个时代的来临! 总结一些h5新增的语义化标签,记录下来方便自己学习 ...

  4. 数据的动态合并和导出至EXCEL

    最近一段时间都在处理数据的动态合并和导出EXCEL的问题,写个demo记录下,希望和我碰到同样问题的博友可以顺利解决:后面会提供demo下载链接. (VS2012,ASP.NET) 一.主要解决以下问 ...

  5. .Net framework.

    Figure 1 - .Net Framework The Common Language Runtime (CLR) is the mechanism through which .NET code ...

  6. javascript的选项卡

    主要用的索引值 首先 写三个按钮 <input type="button" > <input type="button" > <i ...

  7. corejava-chap01

    <java是什么:>Programming language 程序语言Development environment 开发环境Application environment 应用环境Dep ...

  8. C++中的static关键字的总结 (转载)

    C++的static有两种用法:面向过程程序设计中的static和面向对象程序设计中的static.前者应用于普通变量和函数,不涉及类:后者主要说明static在类中的作用. 1.面向过程设计中的st ...

  9. c 连接数据库 mysql

    sudo apt-get install mysql-server mysql-client 再装开发包代码:sudo apt-get install libmysqlclient15-dev 安装完 ...

  10. (转载)无缝滚动图片的js和jquery两种写法

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...