Problem Description
A few days ago, Tom was tired of all the PC-games, so he went back to some old FC-games. "Hudson's Adventure Island" was his favorite which he had played thousands of times. But to his disappointed, the more he played, the more he
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.
 
Input
The input consists of multiple test cases. Each test case starts with two positive integers m, n. Then follows m lines, each line contain n integers.
 
Output
For each case, if the hero can get the exit, find and print the maximum points of energy he left. Or print “you loss!”
 
Sample Input
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
 
Sample Output
4
0
you loss!
Hint
The 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.
 
Author
imems
 
Source

思路:先用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&#39;s Adventure Island(BFS+如压力DP)的更多相关文章

  1. Hdu 3341 Lost&#39;s revenge (ac+自己主动机dp+hash)

    标题效果: 举个很多种DNA弦,每个字符串值值至1.最后,一个长字符串.要安排你最后一次另一个字符串,使其没事子值和最大. IDEAS: 首先easy我们的想法是想搜索的!管她3721..直接一个字符 ...

  2. HDU - 3341 Lost&#39;s revenge(AC自己主动机+DP)

    Description Lost and AekdyCoin are friends. They always play "number game"(A boring game b ...

  3. HDU 1312 Red and Black --- 入门搜索 BFS解法

    HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...

  4. HDU 3247 Resource Archiver (AC自动机+BFS+状压DP)

    题意:给定 n 个文本串,m个病毒串,文本串重叠部分可以合并,但合并后不能含有病毒串,问所有文本串合并后最短多长. 析:先把所有的文本串和病毒都插入到AC自动机上,不过标记不一样,可以给病毒标记-1, ...

  5. HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)

    题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP ...

  6. HDU 1565&1569 方格取数系列(状压DP或者最大流)

    方格取数(2) Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  7. UVA10269 Adventure of Super Mario(Floyd+DP)

    UVA10269 Adventure of Super Mario(Floyd+DP) After rescuing the beautiful princess, Super Mario needs ...

  8. HDU 1430 魔板(康托展开+BFS+预处理)

    魔板 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  9. HDU 5025:Saving Tang Monk(BFS + 状压)

    http://acm.hdu.edu.cn/showproblem.php?pid=5025 Saving Tang Monk Problem Description   <Journey to ...

随机推荐

  1. 一切皆为 JavaScript

    JavaScript起源于Netscape公司的LiveScript语言,这是一种基于对象和事件驱动的client脚本语言.最初的设计是为了检验HTML表单输入的正确性. 早些年,JavaScript ...

  2. centos下chm阅读器

    xchm kchmviewer chmsee

  3. Setup SSH and SVN on Windows Server

    cygwin: install sshd, cygrunsrv http://lifehacker.com/205090/geek-to-live--set-up-a-personal-home-ss ...

  4. NET Core RC2

    .NET Core RC2/RTM 明确了时间表 .NET Core 经过了将近2年的开发,去年12月份发布的RC1版本,明确来说那只是一个beta版本,自从RC1发布以来,看到github里的RC2 ...

  5. hdu 1070Milk

    地址链接:http://acm.hdu.edu.cn/showproblem.php?pid=1070 题意:多看几遍,学着静下来心去看英文题 代码: #include<bits/stdc++. ...

  6. WM_SYSCOMMAND消息命令整理 good

    注意:1. 使用WM_SYSCOMMAND时,鼠标的一些消息可能会受到影响,比如不能响应MouseUp事件,可以在窗口中捕获WM_SYSCOMMAND消息,并判断消息的CommandType来判断消息 ...

  7. linux内核基础(系统调用,简明)

    内核基础(系统调用) 在说系统调用之前.先来说说内核是怎么和我们交互的.或者说是怎么和我们产生交集的. 首先,内核是用来控制硬件的仅仅有内核才干直接控制硬件,所以说内核非常重要,假设内核被控制那么电脑 ...

  8. HDU1035深度搜索

    /* HDU1035 意甲冠军: 给定一个字符矩阵,N S W E分别代表向上,下,剩下,进 模拟搜索,推断: 若能走出字符矩阵.则Yes,输出步数 若走不出矩阵,那么必然有圈存在,必然在矩阵中存在一 ...

  9. POI数据下载器

    偶尔用点儿POI数据,所以写了一个下载器.用到的东西还真不少. 功能点 +编写翻页脚本 +CSharp与JS交互 +POI数据转换json +CSharp的json序列化类 +CSharp读写json ...

  10. SCU 3132(博弈)

    传送门:windy和水星 -- 水星游戏 1 题意:在一张由 n*m 的格子组成的棋盘上放着 k 个骑士每个骑士的位置为(xi,yi),表示第xi行,第yi列骑士如果当前位置为(x,y),一步可以走的 ...