HDU-3502-Huson's Adventure Island(BFS+如压力DP)
lost. Tom soon gave up the game.

Now, he invents an easier game on the base of “Hudson's Adventure Island”. He wants to know whether the new game is easy enough. So he came to you for help.
To simplify the problem, you can assume there is matrix-map contains m(0 < m < 256) rows, n(0 < n < 256) columns, an entrance on the top-left corner (0, 0), an exit on the bottom-right corner (m-1, n-1). Each entry of the matrix contains a integer k.The range
of k is defined below:
a)k = 0,it is a free space one can go through.
b)k = -1,it is an obstacle one can’t go through.
c)0 < k < 10000,it is a fruit one can go through and gain k points of energy.
d)k >= 0 at the entrance point and the end point.
At the begin, the hero has 0 points of energy and he can go four directions (up, down, left, right). Each move he makes cost 1 point of energy. No energy no move. If he can’t make a move or get to the exit, he loses the game. The number of fruit is 17 at most.
4 4
8 0 0 0
-1 -1 -1 0
-1 -1 -1 0
0 6 0 0
3 3
4 0 0
0 0 0
0 0 0
4 4
5 0 0 0
0 -1 -1 0
0 -1 -1 0
0 0 0 0
4
0
you loss!HintThe hero can pass the exit. When he gets the exit point, he can choose to get out to finish the game or move on to gain more points of energy.
思路:先用BFS 求出各个水果和终点之间的距离,再用状态压缩DP求解。
#include <cstdio>
#include <queue>
#define INF 999999999
#define max(A,B)(A>B?A:B)
using namespace std; struct S{
int x,y;
}fruit[20],t; int n,m,cnt,ans,mp[256][256],dis[20][20],cost[256][256],nxt[4][2]={{1,0},{0,1},{-1,0},{0,-1}},mx[1<<18][20],val[20]; void bfs(S t)
{
int i,j,nx,ny; for(i=0;i<n;i++) for(j=0;j<m;j++) cost[i][j]=INF; queue<S>que; cost[t.x][t.y]=0; que.push(t); while(!que.empty())
{
t=que.front(); for(i=0;i<4;i++)
{
nx=t.x+nxt[i][0];
ny=t.y+nxt[i][1]; if(nx>=0 && nx<n && ny>=0 && ny<m && mp[nx][ny]!=-1 && cost[nx][ny]==INF)
{
cost[nx][ny]=cost[t.x][t.y]+1; t.x=nx;
t.y=ny; que.push(t);
} t=que.front();
} que.pop();
}
} int main()
{
int i,j; while(~scanf("%d%d",&n,&m))
{
cnt=0; for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&mp[i][j]); if(mp[i][j]>0)
{
fruit[cnt].x=i;
fruit[cnt].y=j; val[cnt++]=mp[i][j];
}
}
} if(n==1 && m==1)
{
if(mp[0][0]>=0) printf("%d\n",mp[0][0]);
else printf("you loss!\n"); continue;
} if(mp[0][0]<=0)
{
printf("you loss!\n"); continue;
} if(mp[n-1][m-1]==-1)
{
printf("you loss!\n"); continue;
} fruit[cnt].x=n-1;//终点也放进去
fruit[cnt].y=m-1; for(i=0;i<=cnt;i++)
{
bfs(fruit[i]); for(j=0;j<=cnt;j++) dis[i][j]=cost[fruit[j].x][fruit[j].y];//各个水果和终点之间的距离 } queue<S>que; for(i=1;i<(1<<cnt);i++) for(j=0;j<cnt;j++) mx[i][j]=-1; int v; t.x=1;
t.y=0; mx[t.x][t.y]=val[0]; que.push(t); while(!que.empty())
{
t=que.front(); for(i=1;i<cnt;i++)
{
if(!(t.x & (1<<i)))
{
v=mx[t.x][t.y]-dis[t.y][i]; if(v>=0)
{
t.x=(t.x | (1<<i));
t.y=i; if(v+val[i]>mx[t.x][i])
{
mx[t.x][i]=v+val[i];
que.push(t);
} t=que.front();
}
}
} que.pop();
} ans=-1; for(i=1;i<(1<<cnt);i++) for(j=0;j<cnt;j++) ans=max(ans,mx[i][j]-dis[j][cnt]); if(ans>=0) printf("%d\n",ans);
else puts("you loss!");
}
}
HDU-3502-Huson's Adventure Island(BFS+如压力DP)的更多相关文章
- Hdu 3341 Lost's revenge (ac+自己主动机dp+hash)
标题效果: 举个很多种DNA弦,每个字符串值值至1.最后,一个长字符串.要安排你最后一次另一个字符串,使其没事子值和最大. IDEAS: 首先easy我们的想法是想搜索的!管她3721..直接一个字符 ...
- HDU - 3341 Lost's revenge(AC自己主动机+DP)
Description Lost and AekdyCoin are friends. They always play "number game"(A boring game b ...
- HDU 1312 Red and Black --- 入门搜索 BFS解法
HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...
- HDU 3247 Resource Archiver (AC自动机+BFS+状压DP)
题意:给定 n 个文本串,m个病毒串,文本串重叠部分可以合并,但合并后不能含有病毒串,问所有文本串合并后最短多长. 析:先把所有的文本串和病毒都插入到AC自动机上,不过标记不一样,可以给病毒标记-1, ...
- HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)
题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP ...
- HDU 1565&1569 方格取数系列(状压DP或者最大流)
方格取数(2) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- UVA10269 Adventure of Super Mario(Floyd+DP)
UVA10269 Adventure of Super Mario(Floyd+DP) After rescuing the beautiful princess, Super Mario needs ...
- HDU 1430 魔板(康托展开+BFS+预处理)
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- HDU 5025:Saving Tang Monk(BFS + 状压)
http://acm.hdu.edu.cn/showproblem.php?pid=5025 Saving Tang Monk Problem Description <Journey to ...
随机推荐
- Swift - 故事板storyboard的用法
故事板(UIStoryboard)可以很方便的进行界面的设计,下面总结了常用的几个操作方法: 1,初始场景 选中View Controller,在属性面板里勾选Is Initial View Cont ...
- CodeForces Round #191 (327C) - Magic Five 等比数列求和的快速幂取模
很久以前做过此类问题..就因为太久了..这题想了很久想不出..卡在推出等比的求和公式,有除法运算,无法快速幂取模... 看到了 http://blog.csdn.net/yangshuolll/art ...
- 《大数据互联网大规模数据挖掘与分布式处理》阅读笔记(四)-----WEB广告
作者: 沈慧 目前,许多WEB应用通过广告而维持生计,从在线广告中获益最多的是搜索应用,“adwords”模型就是一种用于搜索查询和广告匹配的模型.这一章介绍了在线广告的相关问题.在线算法.Adwor ...
- hdu2647解题报告
题意:有个工厂的老板给工人发奖金,每人基础都是888,工人们有自己的想法,如:a 工人想要比 b 工人的奖金高,老板想要使花的钱最少 那么就可以 给b 888,给a 889 ,但是如果在此基础上,b也 ...
- POJ1291-并查集/dfs
并查集 题意:找出给定的这些话中是否有冲突.若没有则最多有多少句是对的. /* 思路:如果第x句说y是对的,则x,y必定是一起的,x+n,y+n是一起的:反之x,y+n//y,x+n是一起的. 利用并 ...
- jQuery选择
1.基本的选择 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY29tZW9uc3RvbmU=/font/5a6L5L2T/fontsize/400/fil ...
- Struts2他们拦截器实例定义—登陆权限验证
版本号:struts2.1.6 这种情况下实现功能:用户需要指定username登陆,进入相应的页面运行成功登陆作战,否则,它返回到着陆的登录页面,当直接进入操作页面(登陆访问页面后的能力)如果不同意 ...
- delphi 自我删除和线程池(1000行代码,需要仔细研究)
unit Unit4; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- WM_PARENTNOTIFY的作用(不完全)
VCL里源码如下: procedure TWinControl.WMParentNotify(var Message: TWMParentNotify); begin with Message do ...
- uva796(求桥数目)
传送门:Critical Links 题意:给出一个无向图,按顺序输出桥. 分析:模板题,求出桥后排个序输出. #include <cstdio> #include <cstring ...