hdu Collect More Jewels
思路:
先用bfs求出入口,宝物,出口,两两之间的最短距离。
在用dfs搜索所有情况,求出从入口走到出口能获得的最大价值。
我们要解决几个问题:1、求入口到第一个取宝物的地方的最短距离
2、求第i个取宝物的地方到第i+1个取宝物的地方的最短距离
3、求第n个取宝物的地方到出口的最短距离
4、保证以上3点能在时间L内实现的情况下,取得的宝石价值最大
熟悉两种搜索的优缺点:
BFS: 对于解决最短或最少问题特别有效,而且寻找深度小,但缺点是内存耗费量大(需要开大量的数组单元用来存储状态)。
DFS:对于解决遍历求和问题有效,对于问题搜索深度小的时候处理速度迅速,然而在深度很大的情况下效率不高
#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的更多相关文章
- HDU Collect More Jewels 1044
BFS + 状态压缩 险过 这个并不是最好的算法 但是写起来比较简单 , 可以AC,但是耗时比较多 下面是代码 就不多说了 #include <cstdio> #include <c ...
- HDU 1044 Collect More Jewels(BFS+DFS)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdu.1044.Collect More Jewels(bfs + 状态压缩)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdu 1044 Collect More Jewels(bfs+状态压缩)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- Collect More Jewels(hdu1044)(BFS+DFS)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdu 1044 Collect More Jewels
题意: 一个n*m的迷宫,在t时刻后就会坍塌,问:在逃出来的前提下,能带出来多少价值的宝藏. 其中: ’*‘:代表墙壁: '.':代表道路: '@':代表起始位置: '<':代表出口: 'A'~ ...
- hdu 1044(bfs+状压)
非常经典的一类题型 没有多个出口.这里题目没有说清楚 Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- HDU 1044 BFS
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdu 1044(bfs+dfs+剪枝)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
随机推荐
- C++ virtual descructor
[代码1] C++ Code 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 ...
- IOS多线程(NSThread)
1.创建方法 使用NSThread创建线程主要有两个个方法,分别如下 NSThread* myThread = [[NSThread alloc] initWithTarget:self sele ...
- java\c程序的内存分配
JAVA 文件编译执行与虚拟机(JVM)介绍 Java 虚拟机(JVM)是可运行Java代码的假想计算机.只要根据JVM规格描述将解释器移植到特定的计算机上,就能保证经过编译的任何Java代码能够在该 ...
- frameset框架下,刷新整个页面
<a href="index.jsp" target="_parent"> index.jsp主frameset页面
- angularjs 指令(directive)详解(1)
原文地址 什么是directive?我们先来看一下官方的解释: At a high level, directives are markers on a DOM element (such as an ...
- 使yum保留下载的rpm包
[root@14LN yum]# egrep 'cachedir|keepcache' /etc/yum.conf #cachedir=/var/cache/yum/$basearch/$releas ...
- MVC学习笔记---MVC生命周期
Asp.net应用程序管道处理用户请求时特别强调"时机",对Asp.net生命周期的了解多少直接影响我们写页面和控件的效率.因此在2007年和2008年我在这个话题上各写了一篇文章 ...
- android之WakeLock机制浅析
转自:http://blog.sina.com.cn/s/blog_4ad7c2540101n2k2.html 应用程序耗电的实质,是所启用的硬件在消耗电量. 手机的耗电单元 CPU: 应用处理器( ...
- 3.Factory Method 工厂方法模式(创建型模式)
1.定义: 定义一个用于创建对象的接口,让子类决定实例化哪一个类.Factory Method使得一个类的实例化延迟到子类. 2.实现代码如下: /// <summary> /// 工厂方 ...
- 好用的PHP分页类
<?php class Page { private $total; //总记录 private $pagesize; //每页显示多少条 private $limit; ...