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 ...
随机推荐
- Vue语法笔记
Vue.js 的核心是一个允许采用简洁的模板语法来声明式的将数据渲染进 DOM: 事件监听:v-on 指令绑定一个事件监听器 缩写[@] v-on:click 用户输入,绑定数据:v-model ...
- bzoj4552: [Tjoi2016&Heoi2016]排序(二分+线段树)
又是久违的1A哇... 好喵喵的题!二分a[p],把大于mid的数改为1,小于等于mid的数改为0,变成01串后就可以用线段树进行那一连串排序了,排序后如果p的位置上的数为0,说明答案比mid小,如果 ...
- bzoj2064: 分裂(集合DP)
......咸鱼了将近一个月,因为沉迷ingress作业越来越多一直没时间搞OI呜呜呜 题目大意:有一个初始集合(n个元素)和一个目标集合(m个元素)(1<=n,m<=10),两个操作 ...
- HTML5 canvas 创意:飞翔的凤凰
当我看到这件作品的时候,我表示非常喜欢.这个作品的产生不仅仅需要编程和算法,作者肯定是个充满了艺术细胞的人.倘若有什么canvas艺术作品比赛的话,我想它就是获奖的那个. 先观赏下演示吧.注意,要看到 ...
- Apple Tree POJ - 3321 dfs序列构造树状数组(好题)
There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. ...
- 手脱ASProtect v1.2(无Stolen Code)
1.载入PEID ASProtect v1.2 2.载入OD > 01C04200 push 跑跑赛道.0042C001 ; //入口处 C3 retn AA stos byte ptr es: ...
- AndroidManifest Ambiguity方案原理及代码
1简述 前段时间在bluebox的一份android安全pdf中看到一个AndroidManifest Ambiguity方案.该方案基于android系统解析AXML的一个特点:android在解析 ...
- [C#] 小记 new 和 override 关键字
C#要想实现函数的override,要求和C++一样,父类的函数必须用virtual关键字注明,随后在子类中用override关键字表明重写的函数. 子类同名函数定义时,如果什么都不写,或者使用new ...
- Spring实战第一部分总结
Spring实战第一部分总结 第一章 综述 1. DI依赖注入让相互协作的组件保持松散耦合,而 ...
- 51Nod 1092 回文字符串 | 最长公共子序列变形
求字符串和其逆的最长公共子序列,需要添加的字符数就为长度-最长公共子序列长 #include "stdio.h" #include "string.h" #de ...