http://acm.hdu.edu.cn/showproblem.php?pid=1044

代码题,没什么好说的,先预处理出两点间距离,然后dfs搜一下找最大值

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
const int INF=0xfffffff;
int n,m,L,M,ans,sum;
int dis[][];
char mp[][]; struct node{
int x,y,step,v;
}kk[]; int dx[]={,-,,};
int dy[]={,,,-};
int vis[][]; int bfs(node s,node t){
memset(vis,,sizeof(vis));
queue <node> q;
s.step=;
q.push(s);
vis[s.x][s.y]=;
while(!q.empty()){
node u=q.front();
if(u.x==t.x && u.y==t.y)
return u.step;
q.pop();
for(int i=;i<;i++){
int xx=u.x+dx[i];
int yy=u.y+dy[i];
if(xx< || xx>=n || yy< || yy>=m)continue;
if(mp[xx][yy]=='*')continue;
if(!vis[xx][yy]){
vis[xx][yy]=;
node next=u;
next.step++;next.x=xx;next.y=yy;
if(next.step<=L)q.push(next);
}
}
}
return INF;
} int VIS[]; void dfs(int now,int t,int v){
if(ans==sum)return;
if(t>L)return;
if(now==M+){
ans=max(ans,v);
return;
}
for(int i=;i<=M+;i++){
if(VIS[i])continue;
VIS[i]=;
dfs(i,t+dis[now][i],v+kk[i].v);
VIS[i]=;
}
} int V[]; int main(){
int T;
scanf("%d",&T);
int first=;
for(int cas=;cas<=T;cas++){
scanf("%d%d%d%d",&m,&n,&L,&M);
sum=ans=;
for(int i=;i<=M;i++){
scanf("%d",&V[i]);
sum+=V[i];
}
for(int i=;i<n;i++)
scanf("%s",mp[i]);
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(mp[i][j]=='@'){
kk[].x=i;kk[].y=j;kk[].v=;
}
else if(mp[i][j]=='<'){
kk[M+].x=i;kk[M+].y=j;kk[M+].v=;
}
else if(mp[i][j]>='A' && mp[i][j]<='J'){
kk[mp[i][j]-'A'+].x=i;kk[mp[i][j]-'A'+].y=j;kk[mp[i][j]-'A'+].v=V[mp[i][j]-'A'+];
}
}
}
memset(dis,-,sizeof(dis));
for(int i=;i<=M+;i++){
for(int j=;j<=M+;j++){
if(i==j)dis[i][j]=;
else{
if(dis[i][j]==-){
dis[i][j]=bfs(kk[i],kk[j]);
dis[j][i]=dis[i][j];
}
}
}
}
memset(VIS,,sizeof(VIS));
VIS[]=;
dfs(,,);
if(first)first=;
else putchar('\n');
printf("Case %d:\n",cas);
if(dis[][M+]==INF)puts("Impossible");
else printf("The best score is %d.\n",ans);
}
return ;
}

HDU 1044的更多相关文章

  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(bfs+状压)

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

  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. 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. hdu 1044 Collect More Jewels

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

  8. hdu 1044 BFS(压缩图)+DFS

    题意:              给你起点,终点,图上有墙有路还有宝物,问你在规定时间内能否能到终点,如果能问最多能捡到多少宝物. 思路:           看完这个题目果断 BFS+三维的mark ...

  9. HDU Collect More Jewels 1044

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

随机推荐

  1. java代码抓取网页邮箱

    实现思路:1.使用java.net.URL对象,绑定网络上某一个网页的地址2.通过java.net.URL对象的openConnection()方法获得一个HttpConnection对象3.通过Ht ...

  2. C语言指针(一)

    一.指针 定义指针变量 指针指向的数据类型 *指针变量名称; 例: int *p; *作用: 1.在定义变量的时候 * 是一个类型说明符,说明定义的这个变量是一个指针变量 2.在不是定义变量的时候 * ...

  3. MyEclipse生成WAR包并在Tomcat下部署发布[转]

      从来没有想过web项目还能打包的,但是有要求,就不得不去实现,在网上找了一下,发现挺简单的. 首先是使用MyEclipse将web项目打包,如下图所示. 右键选中项目,选择export. 然后选择 ...

  4. 如何给一个网卡配置多个虚拟ip

    1.执行命令 ifconfig etho: 192.168.1.101 netmask 255.255.255.0 up 2.要想永久保存,则将刚刚那行代码写入/etc/rc.local  (开机都会 ...

  5. 绑定本地Service并与之通信-----之一

    import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os. ...

  6. Jquery操作复选框(CheckBox)的取值赋值实现代码

    赋值 复选框 CheckBox 遍历 取值  1. 获取单个checkbox选中项(三种写法): $("input:checkbox:checked").val() 或者 $(&q ...

  7. 导航栏视图设置 tabbleView 是设置总背景图

    //导航栏视图设置 tabbleView 是设置总背景图 //默认的时白色半透明(有点灰的感觉), UIBarStyleBlack,UIBarStyleBlackTranslucent ,UIBarS ...

  8. 2015.11.16JQuery 隐藏,显示按钮.

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

  9. 如何在android项目中引用project作为类库引用

    前言: 在我们开发项目的时候,存在很多多个项目共有一个资源.逻辑代码的情况,这种情况一般我们采用在开发项目中导入别的项目作为引用的类库.资源等. 操作: 1.  新建一个android项目common ...

  10. Windows7下U盘安装Ubuntu14.04双系统

    1.准备工作 (1)下载Ubuntu14.04系统镜像文件,Ultraiso,EasyBcd,分区助手 Ubuntu14.04地址:http://www.ubuntu.com/download/des ...