hdu 1044(bfs+dfs+剪枝)
Collect More Jewels
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6739 Accepted Submission(s): 1564
is written in the Book of The Lady: After the Creation, the cruel god
Moloch rebelled against the authority of Marduk the Creator.Moloch stole
from Marduk the most powerful of all the artifacts of the gods, the
Amulet of Yendor, and he hid it in the dark cavities of Gehennom, the
Under World, where he now lurks, and bides his time.
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.
input will contain multiple test cases. The first line of the input is a
single integer T (1 <= T <= 10) which is the number of test
cases. T test cases follow, each preceded by a single blank line.
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.
should be directed to standard output. Start each case with "Case #:"
on a single line, where # is the case number starting from 1. Two
consecutive cases should be separated by a single blank line. No blank
line should be produced after the last test case.
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 <algorithm>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
const int INF = ;
int n,m,limit,num;
int dis[][]; ///记录两点之间的距离
int jw[];
struct Node{
int x,y,step,geshu;
}s;
char graph[][];
bool vis[][];
int dir[][]={{,},{-,},{,},{,-}};
bool check(int x,int y){
if(x<||x>n||y<||y>m||vis[x][y]||graph[x][y]=='*') return false;
return true;
}
void bfs(Node s,int k){
queue<Node> q;
vis[s.x][s.y] = true;
s.geshu = ;
q.push(s);
while(!q.empty()){
Node now = q.front();
q.pop();
if(now.geshu==num+) return;
for(int i=;i<;i++){
Node next;
next.x = now.x+dir[i][];
next.y = now.y+dir[i][];
if(!check(next.x,next.y)) continue;
char c = graph[next.x][next.y];
next.step = now.step+;
next.geshu = now.geshu+;
if(c=='.') next.geshu-=;
if(c=='@'){
dis[k][] = next.step;
}
else if(c>='A'&&c<='J') {
dis[k][c-'A'+] = next.step;
}else if(c=='<'){
dis[k][num+] = next.step;
}
vis[next.x][next.y] = true;
q.push(next);
}
}
}
bool vis1[];
int MAX = ,sum;
void dfs(int u,int step,int ans){
if(step>limit || MAX == sum) return ; ///必须要加剪枝
if(u==num+){
MAX = max(MAX,ans);
return;
}
for(int i=;i<=num+;i++){
if(!vis1[i]){
vis1[i] = true;
dfs(i,step+dis[u][i],ans+jw[i]);
vis1[i] = false;
}
}
}
int main(){
int tcase;
scanf("%d",&tcase);
int kk = ;
while(tcase--){
for(int i=;i<;i++){
for(int j=;j<;j++){
dis[i][j] = (i==j)?:INF;
}
}
scanf("%d%d%d%d",&m,&n,&limit,&num);
sum = ;
for(int i=;i<=num;i++){
scanf("%d",&jw[i]);
sum+=jw[i];
}
jw[num+] = jw[] = ;
for(int i=;i<=n;i++){
scanf("%s",graph[i]+);
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(graph[i][j]=='@') {
memset(vis,false,sizeof(vis));
s.x = i,s.y = j,s.step=;
bfs(s,);
}
if(graph[i][j]=='<'){
memset(vis,false,sizeof(vis));
s.x = i,s.y = j,s.step=;
bfs(s,num+);
}
if(graph[i][j]>='A'&&graph[i][j]<='J'){
memset(vis,false,sizeof(vis));
s.x = i,s.y = j,s.step=;
bfs(s,graph[i][j]-'A'+);
}
}
}
printf("Case %d:\n",kk++);
if(dis[][num+]>limit){
printf("Impossible\n");
if(tcase) printf("\n");
continue;
}
memset(vis1,false,sizeof(vis1));
MAX = ;
vis1[] = true;
dfs(,,);
printf("The best score is %d.\n",MAX);
if(tcase) printf("\n");
}
}
hdu 1044(bfs+dfs+剪枝)的更多相关文章
- hdu - 1072(dfs剪枝或bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1072 思路:深搜每一个节点,并且进行剪枝,记录每一步上一次的s1,s2:如果之前走过的时间小于这一次, ...
- hdu 1044 BFS(压缩图)+DFS
题意: 给你起点,终点,图上有墙有路还有宝物,问你在规定时间内能否能到终点,如果能问最多能捡到多少宝物. 思路: 看完这个题目果断 BFS+三维的mark ...
- hdu 1983(BFS+DFS) 怪盗Kid
http://acm.hdu.edu.cn/showproblem.php?pid=1983 首先,题目要求出口和入口不能封闭,那么,只要把出口或入口的周围全给封闭了那盗贼肯定无法成功偷盗,出口或入口 ...
- hdu 1175(BFS&DFS) 连连看
题目在这里:http://acm.hdu.edu.cn/showproblem.php?pid=1175 大家都很熟悉的连连看,原理基本就是这个,典型的搜索.这里用的是广搜.深搜的在下面 与普通的搜索 ...
- UVA-11882 bfs + dfs + 剪枝
假设当前已经到达(x,y),用bfs判断一下还可以到达的点有maxd个,如果maxd加上当前已经经过的长度小于当前答案的长度就退出,如果相同,就将bfs搜索到的点从大到小排序,如果连最大序列都无法大于 ...
- HDU 1175 连连看 (DFS+剪枝)
<题目链接> 题目大意:在一个棋盘上给定一个起点和终点,判断这两点是否能通过连线连起来,规定这个连线不能穿过其它的棋子,并且连线转弯不能超过2次. 解题分析:就是DFS从起点开始搜索,只不 ...
- 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 1145(Sticks) DFS剪枝
Sticks Problem Description George took sticks of the same length and cut them randomly until all par ...
随机推荐
- 【简单算法】17.字符串转整数(atoi)
题目: 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即为 ...
- 剑桥offer系列(1~10)
1.题目描述 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 思路:从左下开始, ...
- Linux之SSL安全套接字20160704
使用SSL前,先有 基本的TCP套接字连接.见demo代码 SSL_library_init();//在使用OpenSSL 之前,必须进行相应的协议初始化工作 OpenSSL_add_all_algo ...
- qt4+vs2010 环境搭建
1.安装开发所需的软件: vs2010(包括VS2010SP1dvd1,Visual_Assist_X_10.9.2062.0_Crack等) QT: qt-win-opensource-4.8.5- ...
- logrotate配置和使用
logrotate是linux自带的日志管理工具.服务器如果不对日志进行滚动操作,单个日志文件的增长速度极快,不利于日志查找和问题定位.而logrotate能够自动完成日志的截断.压缩和滚动操作. 安 ...
- centos7-每天定时备份 mysql数据库
centos7-每天定时备份 mysql数据库 第一步:编写数据库备份脚本database_mysql_shell.sh #!/bin/bash DATE=`date +%Y%m%d%H%M` #ev ...
- 用pip命令安装Python第三方库
一.准备工作 1. 安装pip (1)下载 pip下载地址:https://pypi.python.org/pypi/pip#downloads (2)安装 下载后解压,控制台下进入解压后的目录,运行 ...
- 【C++ STL】Queue
1.定义 class queue<>实作为一个queue(也成为FIFO,先进先出).可以使用push()将任意数量的元素置入queue中,也可以使用pop()将元素以其插入顺序从容器中移 ...
- wcf 服务创建,配置,测试
一.WCF创建: 常规的创建WCF服务是通过SOAP传输的,很多网站开发人员想放弃使用XML而使用JSON,这个时候可以参照:http://www.cnblogs.com/zhili/p/WCFRes ...
- js_判断当前页面是否有网络和网络连接超时
2018-04-12 方法一:通过navigator.onLine属性判断,返回true为有联网状态,false为断网状态. //方法一 if(navigator.onLine) { console. ...