hdu 1044(bfs+状压)
非常经典的一类题型
没有多个出口。这里题目没有说清楚
Collect More Jewels
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4684 Accepted Submission(s): 983
Your goddess The Lady seeks to possess the Amulet, and with it to gain deserved ascendance over the other gods.
You, a newly trained Rambler, have been heralded from birth as the instrument of The Lady. You are destined to recover the Amulet for your deity, or die in the attempt. Your hour of destiny has come. For the sake of us all: Go bravely with The Lady!
If you have ever played the computer game NETHACK, you must be familiar with the quotes above. If you have never heard of it, do not worry. You will learn it (and love it) soon.
In this problem, you, the adventurer, are in a dangerous dungeon. You are informed that the dungeon is going to collapse. You must find the exit stairs within given time. However, you do not want to leave the dungeon empty handed. There are lots of rare jewels in the dungeon. Try collecting some of them before you leave. Some of the jewels are cheaper and some are more expensive. So you will try your best to maximize your collection, more importantly, leave the dungeon in time.
The first line of each test case contains four integers W (1 <= W <= 50), H (1 <= H <= 50), L (1 <= L <= 1,000,000) and M (1 <= M <= 10). The dungeon is a rectangle area W block wide and H block high. L is the time limit, by which you need to reach the exit. You can move to one of the adjacent blocks up, down, left and right in each time unit, as long as the target block is inside the dungeon and is not a wall. Time starts at 1 when the game begins. M is the number of jewels in the dungeon. Jewels will be collected once the adventurer is in that block. This does not cost extra time.
The next line contains M integers,which are the values of the jewels.
The next H lines will contain W characters each. They represent the dungeon map in the following notation:
> [*] marks a wall, into which you can not move;
> [.] marks an empty space, into which you can move;
> [@] marks the initial position of the adventurer;
> [<] marks the exit stairs;
> [A] - [J] marks the jewels.
If the adventurer can make it to the exit stairs in the time limit, print the sentence "The best score is S.", where S is the maximum value of the jewels he can collect along the way; otherwise print the word "Impossible" on a single line.
4 4 2 2
100 200
****
*@A*
*B<*
****
4 4 1 2
100 200
****
*@A*
*B<*
****
12 5 13 2
100 200
************
*B.........*
*.********.*
*@...A....<*
************
The best score is 200.
Case 2:
Impossible
Case 3:
The best score is 300.
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <string>
#include <queue>
#include <stdlib.h>
using namespace std; struct node
{
int x,y,cnt;
}que[]; int n,m,t,tj;
int cost[];
int dis[][];
char g[][];
int sid,tid;
int mlink[][];
int qf,qd;
int up[]={,,-,};
int rl[]={,,,-};
int dp[][][]; void bfs(int sx,int sy)
{
int mark[][];
memset(mark,,sizeof(mark));
qf=qd=;
node finode;
mark[sx][sy]=;
finode.x=sx; finode.y=sy; finode.cnt=;
que[qf++]=finode;
while(qf>qd)
{
node cur=que[qd++];
for(int i=;i<;i++)
{
int tx,ty;
tx=cur.x+up[i];
ty=cur.y+rl[i];
if( (tx>=&&tx<n)&&(ty>=&&ty<m) && g[tx][ty]!='*'&& mark[tx][ty]==)
{
mark[tx][ty]=;
node nwnode;
nwnode.cnt=cur.cnt+;
nwnode.x=tx;nwnode.y=ty;
que[qf++]=nwnode;
if(mlink[tx][ty]!=-)
{
dis[ mlink[tx][ty] ][ mlink[sx][sy] ]=nwnode.cnt;
dis[ mlink[sx][sy] ][ mlink[tx][ty] ]=nwnode.cnt;
}
}
}
}
} int main()
{
int T;
int tt=;
scanf("%d",&T);
int flag=;
while(T--)
{
if(flag) printf("\n");
flag=;
scanf("%d%d%d%d",&m,&n,&t,&tj);
for(int i=;i<tj;i++)
scanf("%d",cost+i);
for(int i=;i<n;i++)
{
scanf("%s",g[i]);
}
memset(dis,-,sizeof(dis));
memset(mlink,-,sizeof(mlink));
int id=;
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
if(g[i][j]!='.'&&g[i][j]!='*')
{
if(g[i][j]=='@')
mlink[i][j]=;
else if( g[i][j]=='<')
{
id++;
mlink[i][j]=tj+;
}
else mlink[i][j]=g[i][j]-'A'+;
}
}
if(id>=)
{
for(int i=;i<;i++)
printf("%d\n",cost[i]); }
for(int i=;i<n;i++)
for(int j=;j<m;j++)
if(g[i][j]!='.'&&g[i][j]!='<'&&g[i][j]!='*')
{
bfs(i,j);
}
////////////
memset(dp,-,sizeof(dp));
dp[][][]=; for(int i=;i<=tj;i++)
{
for(int j=;j<=tj;j++)
{
for(int k=; k<(<<(tj+)) ;k++)
{
if(dp[i-][j][k]!=- && dp[i-][j][k] < t )
{
for(int p=;p<=tj;p++)
{
if( (k&(<<p)) == &&dis[j][p]!=-)
{
if(dp[i][p][(k|(<<p))]==-) dp[i][p][k|(<<p)] = dp[i-][j][k]+dis[j][p];
else dp[i][p][(k|(<<p))] = min(dp[i][p][(k|(<<p))],dp[i-][j][k]+dis[j][p]);
}
}
}
}
}
}
int ans=-;
for(int i=;i<=tj;i++)
for(int j=;j<=tj;j++)
for(int k=;k<(<<(tj+));k++)
if(dp[i][j][k]!=-&&dis[j][tj+]!=-)
if(dp[i][j][k]+dis[j][tj+]<=t)
{
int tmp=;
for(int p=;p<=tj;p++)
if( ((<<p)&k)!= )
tmp+=cost[p-];
ans=max(ans,tmp);
} printf("Case %d:\n",tt++);
if(ans==-) printf("Impossible\n");
else printf("The best score is %d.\n",ans);
}
return ;
}
hdu 1044(bfs+状压)的更多相关文章
- hdu 2209 bfs+状压
http://acm.hdu.edu.cn/showproblem.php?pid=2209 不知为啥有种直觉.会出状压+搜索的题,刷几道先 简单的BFS.状压表示牌的状态, //#pragma co ...
- hdu 5025 bfs+状压
http://acm.hdu.edu.cn/showproblem.php?pid=5025 N*N矩阵 M个钥匙 K起点,T终点,S点需多花费1点且只需要一次,1-9表示9把钥匙,只有当前有I号钥匙 ...
- hdu 1429 bfs+状压
题意:这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁的门,钥匙藏在地牢另外的某些地方.刚开始 Ignatius被关在(sx,sy)的位置,离开地牢的门 ...
- HDU 4771 BFS + 状压
Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- C - 小明系列故事――捉迷藏 HDU - 4528 bfs +状压 旅游-- 最短路+状压
C - 小明系列故事――捉迷藏 HDU - 4528 这个题目看了一下题解,感觉没有很难,应该是可以自己敲出来的,感觉自己好蠢... 这个是一个bfs 用bfs就很好写了,首先可以预处理出大明和二明能 ...
- hdu 5094 Maze (BFS+状压)
题意: n*m的迷宫.多多要从(1,1)到达(n,m).每移动一步消耗1秒.有P种钥匙. 有K个门或墙.给出K个信息:x1,y1,x2,y2,gi 含义是(x1,y1)与(x2,y2)之间有gi ...
- hdu 4771 Stealing Harry Potter's Precious (BFS+状压)
题意: n*m的迷宫,有一些格能走("."),有一些格不能走("#").起始点为"@". 有K个物体.(K<=4),每个物体都是放在& ...
- 孤岛营救问题 (BFS+状压)
https://loj.ac/problem/6121 BFS + 状压 写过就好想,注意细节debug #include <bits/stdc++.h> #define read rea ...
- HDU 5025:Saving Tang Monk(BFS + 状压)
http://acm.hdu.edu.cn/showproblem.php?pid=5025 Saving Tang Monk Problem Description <Journey to ...
随机推荐
- ASP.NET MVC 从IHttp到页面输出
MVCHandler应该算是MVC真正开始的地方.MVCHandler实现了IHttpHandler接口,ProcessRequest便是方法入口. MVCHandler : IHttpHandler ...
- Extjs利用vtype验证表单
Ext.create('Ext.form.Panel', { title: '表单验证', renderTo: Ext.getBody(), frame ...
- MAC OS下使用Xcode进行GLSL编程的配置过程
整理自之前使用的163博客原创文章. GLSL项目中需要使用GLEW库,因此先要安装GLEW库和在Xcode中配置GLEW.要使GLEW在Xcode中被正确链接,又需要通过MacPorts来安装GLE ...
- DevOps:怎么实现源代码注释和系统文档的自动化更新?
[编者按]计算机软件传统定义为:软件是计算机系统中与硬件相依存的另一部分,软件包括程序.数据及其相关文档的完整集合.然而在时下的开发中,文档的合规性往往被忽视的干干净净.本文由 Todd Waits ...
- setTimeout延时0毫秒的作用和问题
一 作用 http://www.cnblogs.com/xieex/archive/2008/07/11/1241151.html 经常看到setTimeout延时0ms的javascript代码,感 ...
- 详解 $_SERVER 函数中QUERY_STRING和REQUEST_URI区别
详解 $_SERVER 函数中QUERY_STRING和REQUEST_URI区别 http://blog.sina.com.cn/s/blog_686999de0100jgda.html 实例: ...
- PHP开发入行真功夫 三扬科技
前言与目录 PHP开发入行真功夫 前言 PHP开发入行真功夫 目录 第2章 基本语法 2.1.1 判断闰年程序 2.1.2 我们现在能做的…… 2.2.1 PHP的语言概貌 2.2.2 为我们的程 ...
- (4)用opengl读入off文件生成可执行文件把模型显示出来(未完待续)
·找了好几个程序,好像都达不到我的要求,去教程里看看吧! 在往上抛出了这问题,好几天才有人回答,我已经找到程序了 正好的他的分析对我分解程序很有用 这是一个难度比较高的 首先你要分析.off文件结构, ...
- nodejs的require模块及路径
在nodejs中,模块大概可以分为核心模块和文件模块. 核心模块是被编译成二进制代码,引用的时候只需require表示符即可,如(require('net')). 文件模块,则是指js文件.json文 ...
- 用eclipse创建maven项目
Maven是基于项目对象模型(POM),也可以进行模块化开发.并且是个强大的管理工具.本经验用eclipse来创建maven项目 步骤: 1.下载并正确安装eclipse 2.在eclipse上成功安 ...