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 ...
随机推荐
- POJ1087 A Plug for UNIX 【最大流】
A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13855 Accepted: 4635 ...
- 实时人脸检测 (Real-Time Face Detection)
源地址:http://blog.sina.com.cn/s/blog_79b67dfe0102uzra.html 最近需要用到人脸检测,于是找了篇引用广泛的论文实现了一下:Robust Real-Ti ...
- 关于索引删除的策略IndexDeletionPolicy
关于索引删除的策略IndexDeletionPolicy . public IndexWriter(Directory d, Analyzer a, boolean create) ...
- 高效合并两个有序数组(Merge Sorted Array)
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: Y ...
- Opencv246+vs2012生成不依赖编译环境的exe文件
我们都知道,vs2012编译项目有两个版本号:Debug和Release,这里我们在Release下生成exe文件,为什么要在Release以下生成呢,原因是你在Debug模式下生成的exe须要vs2 ...
- PowerDesigner中SQL文件、数据库表反向生成PDM
1 反向生成PDM 1) 创建一个空的PDM模型(选择相应的DBMS): 2) 选择[Database]--[Update Model from Database ...
- Python 收集Twitter时间序列数据
CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-7-18 @author: guaguastd @name: c ...
- [C++/CLI编程宝典][2]什么是C++/CLI语言
对于什么是C++/CLI,我们首先能够简单的将其名字划分为两部分来理解,第一,C++,我们熟悉的眼下被广泛使用的面向对象的ISO国际标准的高级语言,也称为ISOC++,我们这里以后均称其为ISOC++ ...
- SlidingMenu开源项目滑动界面的实现总结
先上图 须要准备的是先得在GitHub上下载ActionBarSherlock-master.zip,和SlidingMenu-master.zip这两个开源文件,然后解压这两个包,SlidingMe ...
- 如何通过shell脚本操作MongoDB
通过shell脚本对MongoDB进行自动化操作 运行写好的 ./show.sh 脚本 发现能够建立mongo链接 #!/bin/sh mongo WordPress --eval "sho ...