思路:

先用bfs求出入口,宝物,出口,两两之间的最短距离。

在用dfs搜索所有情况,求出从入口走到出口能获得的最大价值。

我们要解决几个问题:1、求入口到第一个取宝物的地方的最短距离

2、求第i个取宝物的地方到第i+1个取宝物的地方的最短距离

3、求第n个取宝物的地方到出口的最短距离

4、保证以上3点能在时间L内实现的情况下,取得的宝石价值最大

熟悉两种搜索的优缺点:

BFS: 对于解决最短或最少问题特别有效,而且寻找深度小,但缺点是内存耗费量大(需要开大量的数组单元用来存储状态)。

DFS:对于解决遍历求和问题有效,对于问题搜索深度小的时候处理速度迅速,然而在深度很大的情况下效率不高

dfs剪枝:
1.step>time直接return。
2.ans==sum时就不用再搜了  因为已经到最大了。
3.如果搜到一个点,这个点以前已经搜过,而且现在到达这个点时珠宝价值比以前少而且走的步数却比以前多,就不用搜这个点了。
真是被自己傻到了 。。。花了一个小时写了一个代码如下,感觉自己真是太弱智了,居然放这么大的错误,忽略了两件宝物可以在一条路径上,所以 以后编程还是要考虑周全在下手 ,尤其是比赛的时候,一旦有大的漏洞,就会被 坑哭了。。。
错误代码:
#include"iostream"
#include"stdio.h"
#include"algorithm"
#include"cmath"
#include"string"
#include"string.h"
#include"queue"
#include"stack"
#include"vector"
#define mx 105
using namespace std;
int g[mx][mx];
int value[mx][mx];//存放每个点的价值
int time[mx][mx];//存放每个点的时间
int M[mx];//存放宝物的价值
int h,w,m,l,sx,sy,ex,ey,maxvalue;
int dir[][]={{,},{,-},{,},{-,}};
struct node
{
int x,y;
int times;
int values;
};
bool judge1(int x,int y)//判断能不能走
{
if(x>=&&x<h&&y>=&&y<w&&g[x][y]!=-)
return true;
return false;
}
bool judge2(int x,int y,int v,int t)//判断应不应该走
{
if(v>value[x][y]) return true;
if(t<time[x][y]) return true;
return false;
}
void bfs()
{
queue<node>q;
node cur,next;
int i;
cur.x=sx;cur.y=sy;cur.times=;cur.values=;
q.push(cur);
while(!q.empty())
{
cur=q.front();
q.pop();
if(cur.x==ex&&cur.y==ey&&cur.times<=l)
{
if(maxvalue<cur.values) maxvalue=cur.values;
}
for(i=;i<;i++)
{
next.x=cur.x+dir[i][];
next.y=cur.y+dir[i][];
if(judge1(next.x,next.y))
{
next.times=cur.times+;
next.values=cur.values+g[next.x][next.y];
if(judge2(next.x,next.y,next.values,next.times))
{
q.push(next);
time[next.x][next.y]=next.times;
value[next.x][next.y]=next.values;
g[next.x][next.y]=;
}
}
}
}
}
int main()
{
int i,j,t,cou=;;
char ch;
cin>>t;
while(t--)
{
cou++;
cin>>w>>h>>l>>m;
for(i=;i<m;i++) cin>>M[i];
getchar();
for(i=;i<h;i++)
{
for(j=;j<w;j++)
{
cin>>ch;
switch(ch)
{
case '*':g[i][j]=-;break;
case '.':g[i][j]=;break;
case '@':g[i][j]=;sx=i;sy=j;break;
case '<':g[i][j]=;ex=i;ey=j;break;
default: g[i][j]=M[ch-''-];break;
}
}
}
memset(value,,sizeof(value));
for(i=;i<h;i++)
for(j=;j<w;j++) time[i][j]=;
maxvalue=-;
bfs();
cout<<"Case "<<cou<<":"<<endl;
if(maxvalue>=)
cout<<"The best score is "<<maxvalue<<"."<<endl<<endl;
else cout<<"Impossible"<<endl<<endl;
}
return ;
}

在网上搜了一下别人的代码,大致就是先利用bfs构建一个隐氏图,然后再用dfs进行搜索,找到最大价值总和。

#include"iostream"
#include"stdio.h"
#include"algorithm"
#include"cmath"
#include"string.h"
#include"string"
#include"queue"
#define mx 105
using namespace std;
char mp[mx][mx];
bool used[mx][mx];//标记bfs走过的路径
bool vis[mx];//标记 dfs走过的路径
int value[mx];//记录宝物的价值
int H,W,L,M,ans,sum;
int step[mx][mx];//记录每个位置的最小步骤
int dis[mx][mx];//记录出口、入口、宝物两两之间的最短距离
queue<int>q;
int dir[][]={{,},{,-},{,},{-,}};
bool judge(int x,int y)//判断该位置是否可走
{
if(x>=&&x<H&&y>=&&y<W&&mp[x][y]!='*') return true;
return false;
}
void bfs(int x1,int y1,int s)
{
while(!q.empty()) q.pop();
int dx,dy,i,u,x,y;
memset(used,false,sizeof(used));
memset(step,,sizeof(step));
u=x1*W+y1;
q.push(u);
used[x1][y1]=true;
step[x1][y1]=;
while(!q.empty())
{
u=q.front();
q.pop();
x=u/W;
y=u%W;
for(i=;i<;i++)
{
dx=x+dir[i][];
dy=y+dir[i][];
if(judge(dx,dy))
{
if(!used[dx][dy])
{
used[dx][dy]=true;
step[dx][dy]=step[x][y]+;
if(mp[dx][dy]=='@')
dis[s][]=step[dx][dy];
else if(mp[dx][dy]=='<')
dis[s][M+]=step[dx][dy];
else if(mp[dx][dy]>='A'&&mp[dx][dy]<='J')
dis[s][mp[dx][dy]-'A'+]=step[dx][dy];
q.push(dx*W+dy);
}
}
}
}
}
void dfs(int s,int val,int time)
{
int i;
if(time>L) return;
if(ans==sum) return;
if(s>M)
{
if(val>ans)ans=val;
return;
}
for(i=;i<=M+;i++)
{
if(dis[s][i]==||vis[i]) continue;
vis[i]=true;
dfs(i,value[i]+val,time+dis[s][i]);
vis[i]=false;
}
}
int main()
{
int i,j,t,icase=;
cin>>t;
while(t--)
{
sum=;ans=-;
memset(dis,,sizeof(dis));//记得初始化dis
icase++;
cin>>W>>H>>L>>M;
for(i=;i<=M;i++) {cin>>value[i];sum+=value[i];}
for(i=;i<H;i++) cin>>mp[i];
value[]=;value[M+]=;
for(i=;i<H;i++)
{
for(j=;j<W;j++)
{
if(mp[i][j]=='@') bfs(i,j,);
else if(mp[i][j]=='<')bfs(i,j,M+);
else if(mp[i][j]>='A'&&mp[i][j]<='J') bfs(i,j,mp[i][j]-'A'+);
}
}
memset(vis,false,sizeof(vis));
vis[]=true;
dfs(,,);
cout<<"Case "<<icase<<":"<<endl;
if(ans>)
cout<<"The best score is "<<ans<<"."<<endl;
else cout<<"Impossible"<<endl;
if(t) cout<<endl;
}
}

hdu Collect More Jewels的更多相关文章

  1. HDU Collect More Jewels 1044

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

  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. tcp ip detatils

    tcp ip detatils 8.关于TCP协议,下面哪种说法是错误的()A.TCP关闭连接过程中,两端的socket都会经过TIME_WAIT状态B.对一个Established状态的TCP连接, ...

  2. iOS和android游戏纹理优化和内存优化(cocos2d-x)(转载)

    转自http://blog.csdn.net/langresser_king/article/details/8426708 (未完成) 1.2d游戏最占内存的无疑是图片资源. 2.cocos2d-x ...

  3. iOS7 中的新加入的下载类NSURLSession(随ios版本更新而更新)

    想详细的了解网络下载的相关知识,要仔细阅读URL Loading System Programming Guide 这里有篇好文章(http://www.shinobicontrols.com/blo ...

  4. -fomit-frame-pointer 编译选项在gcc 4.8.2版本中的汇编代码研究

    #include void fun(void) { printf("fun"); } int main(int argc, char *argv[]){ fun(); return ...

  5. 解决java.lang.UnsupportedClassVersionError

    出现java.lang.UnsupportedClassVersionError 错误的原因,是因为我们使用高版本的JDK编译的Java class文件试图在较低版本的JVM上运行,所报的错误. 版本 ...

  6. 9.装饰者模式(Decorator Pattern)

    using System; namespace ConsoleApplication7 { class Program { static void Main(string[] args) { // 我 ...

  7. 在有EditText控件的AlertDialog对话框中自动弹出输入法

    我们先回顾一下创建AlertDialog的一般步骤. 一 inflate AlertDialog的布局文件   例如,其中dlg就是我们的布局文件.    View layout = LayoutIn ...

  8. 2.Abstract Factory 抽象工厂(创建型模式)之简单工厂

    简单工厂 1.只有一个工厂(具体的,没有抽象) 2.只生产一种产品(抽象的产品) 3.这种产品可以有多种具体产品类型(派生) 代码实现 class Program { static void Main ...

  9. CRT注册工具使用说明

    激活步骤如下:   1)准备工作:安装好SecureCRT软件,下载并得到该注册机.   2)保持SecureCRT软件关闭(运行的话会提示你正在运行的,关闭就好).   3)将注册机拷贝到你的CRT ...

  10. oracle创建临时表没有权限

    执行下面: grant create any table to 用户名称