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 ...
随机推荐
- ios block循环引用问题
ios开发中,开了ARC模式,系统自动管理内存,如果程序中用到了block就要注意循环引用带来的内存泄露问题了 这几天遇到一个问题,正常页面dismiss的时候是要调用dealloc方法的,但是我的程 ...
- linux下 文件IO 相关
linux下操作文件或设备,需要一个文件描述符 file descriptor,fd 来引用.fd是一个非负整数,实际上是一个索引值,指向文件的记录表,对文件的操作都需要fd.默认的几个:标准输入流 ...
- opencv中的meanshift图像切割
Meanshift(均值漂移)是一种在一组数据的密度分布中寻找局部极值的稳定的方法.Meanshift不仅能够用于图像滤波,视频跟踪,还能够用于图像切割. 通过给出一组多维数据点,其维数是(x,y,r ...
- 在HTML中如何隐藏某段文字具体该怎么实现
<p style="display:none;"> 需要隐藏的文字....... </p> <div style="display:none ...
- Json for Java API学习
首先声明:本文来个非常多网友的博客,我通过參考了他们的博客,大致的了解了一些项目中经常使用的Json in java 类和方法,以及关于json的个人理解 个人对json的一些简单理解 在近期的学习中 ...
- Selenium 验证picklist是可被正确选中且是有序的(动态数组赋值)
原代码: <select id="edit-submitted-im-interesting-in" class="form-select required&quo ...
- FZU2179(数位dp)
传送门:Chriswho 题意:求区间[1,n]内能整除自己本身各位数字的数的个数. 分析:这题跟CF 55D Beautiful numbers一样的,一个数能被它的所有非零数位整除,则能被它们的最 ...
- hdu4612(双连通缩点+树的直径)
传送门:Warm up 题意:询问如何加一条边,使得剩下的桥的数目最少,输出数目. 分析:tarjan缩点后,重新建图得到一棵树,树上所有边都为桥,那么找出树的直径两个端点连上,必定减少的桥数量最多, ...
- Shell脚本检查memcache进程并自己主动重新启动
修正版: #!/bin/sh #check memcache process and restart if down mm_bin="/usr/local/bin/memcached&quo ...
- android中Sensor 工作流程
JAVA 程序 我们使用 sensor 接口一般只要注册一下 SensorListener 像下面这样 ************************************************ ...