HOJ 2133&POJ 2964 Tourist(动态规划)
Tourist
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1503 Accepted: 617
Description
A lazy tourist wants to visit as many interesting locations in a city as possible without going one step further than necessary. Starting from his hotel, located in the north-west corner of city, he intends to take a walk to the south-east corner of the city and then walk back. When walking to the south-east corner, he will only walk east or south, and when walking back to the north-west corner, he will only walk north or west. After studying the city map he realizes that the task is not so simple because some areas are blocked. Therefore he has kindly asked you to write a program to solve his problem.
Given the city map (a 2D grid) where the interesting locations and blocked areas are marked, determine the maximum number of interesting locations he can visit. Locations visited twice are only counted once.
Input
The first line in the input contains the number of test cases (at most 20). Then follow the cases. Each case starts with a line containing two integers, W and H (2 ≤ W, H ≤ 100), the width and the height of the city map. Then follow H lines, each containing a string with W characters with the following meaning:
‘.’ Walkable area
‘*’ Interesting location (also walkable area)
‘#’ Blocked area
You may assume that the upper-left corner (start and end point) and lower-right corner (turning point) are walkable, and that a walkable path of length H + W - 2 exists between them.
Output
For each test case, output a line containing a single integer: the maximum number of interesting locations the lazy tourist can visit.
Sample Input
2
9 7
*……..
…..**#.
..*…#
..####*#.
..#.*#.
…#**…
*……..
5 5
...
*###.
..*
.###*
...
Sample Output
7
8
经常写这种在一个矩阵里从左上角走到右下角,只能向下和向右走,这种是一个水DP。这道题目就是这类题目的升级类型。就是走到右下角还要返回左上角,以前走过的景点返回时走过都不算。我一开始天真的以为先求左上角到右下角的DP,再把图变一下,求右下角到左上角的DP,这样第二个样例就过不了。所以就要换一种方式,去的和来的不能分开DP,只能将他们和在一起才能得到正确的解。看了题解,状态是DP[i][j][k],表示到第i条斜线,去的路的横坐标是j,来的路的横坐标是k。还有的解法是i是第几步。
可以看出,如果一道题目的解法是动态规划,那么一定有其相应的状态转移方程,不能局限于题目的给的条件,
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
using namespace std;
int dp[205][105][105];
int n,m;
int t;
char a[105][105];
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&m,&n);
for(int i=0;i<n;i++)
scanf("%s",a[i]);
memset(dp,0,sizeof(dp));
for(int i=1;i<=m+n-1;i++)
{
for(int j=0;j<n;j++)
{
for(int k=0;k<n;k++)
{
int y1=(i-1)-j;
if(y1<0)
continue;
if(y1>=m)
continue;
int y2=(i-1)-k;
if(y2<0)
continue;
if(y2>=m)
continue;
if(a[j][y1]=='#'||a[k][y2]=='#')
{dp[i][j][k]=0;continue;}
int ans=-2;
if(j-1>=0)
ans=max(ans,dp[i-1][j-1][k]);
if(k-1>=0)
ans=max(ans,dp[i-1][j][k-1]);
if(k-1>=0&&j-1>=0)
ans=max(ans,dp[i-1][j-1][k-1]);
ans=max(ans,dp[i-1][j][k]);
if(ans==-1)
{dp[i][j][k]=-1;continue;}
else
dp[i][j][k]=ans;
if(a[j][y1]=='*'&&y1!=y2)
dp[i][j][k]++;
if(a[k][y2]=='*'&&y1!=y2)
dp[i][j][k]++;
if(a[k][y2]=='*'&&a[j][y1]=='*'&&y1==y2)
dp[i][j][k]++;
}
}
}
printf("%d\n",dp[m+n-1][n-1][n-1]);
}
return 0;
}
HOJ 2133&POJ 2964 Tourist(动态规划)的更多相关文章
- HOJ 2124 &POJ 2663Tri Tiling(动态规划)
Tri Tiling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9016 Accepted: 4684 Descriptio ...
- poj 3783 Balls 动态规划 100层楼投鸡蛋问题
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098409.html 题目链接:poj 3783 Balls 动态规划 100层楼投鸡蛋问题 ...
- HOJ 2148&POJ 2680(DP递推,加大数运算)
Computer Transformation Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4561 Accepted: 17 ...
- poj 2229 一道动态规划思维题
http://poj.org/problem?id=2229 先把题目连接发上.题目的意思就是: 把n拆分为2的幂相加的形式,问有多少种拆分方法. 看了大佬的完全背包代码很久都没懂,就照着网上的写了动 ...
- [POJ 2063] Investment (动态规划)
题目链接:http://poj.org/problem?id=2063 题意:银行每年提供d种债券,每种债券需要付出p[i]块钱,然后一年的收入是v[i],到期后我们把本金+收入取出来作为下一年度本金 ...
- [POJ 2923] Relocation (动态规划 状态压缩)
题目链接:http://poj.org/problem?id=2923 题目的大概意思是,有两辆车a和b,a车的最大承重为A,b车的最大承重为B.有n个家具需要从一个地方搬运到另一个地方,两辆车同时开 ...
- POJ 1088 滑雪 -- 动态规划
题目地址:http://poj.org/problem?id=1088 Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当 ...
- poj 1159 Palindrome - 动态规划
A palindrome is a symmetrical string, that is, a string read identically from left to right as well ...
- poj 2385【动态规划】
poj 2385 Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14007 Accepte ...
随机推荐
- PHP计算两个绝对路径的相对路径
用PHP计算两个绝对路径的相对路径,该如何求呢? 先根据分隔符切割,然后查找相同 异同点,然后开始有相同点,从相同点结束为止开始拼接剩余部分,没有的话,到达根路径拼接整体. 截图如下: 代码如下: & ...
- 【代码审计】EasySNS_V1.6远程图片本地化导致Getshell
0x00 环境准备 EasySNS官网:http://www.imzaker.com 网站源码版本:EasySNS极简社区V1.60 程序源码下载:http://es.imzaker.com/inde ...
- 一句话木马:ASP篇
ASP一句话木马收集: <%eval request("chopper")%> <%execute request("chopper")%&g ...
- error: pathspec 'master' did not match any file(s) known to git.
问题描述: 在远程服务器上新建裸仓库git --bare init : git clone裸仓库到本地: 本地新建并切换分支xccdev,git checkout -b xccdev: 从xccde ...
- python --help查询python相关命令
C:\Users\lenovo>python --help usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ... O ...
- 安装.NET Framework 3.5
https://www.microsoft.com/zh-CN/download/details.aspx?id=22 https://docs.microsoft.com/zh-cn/dotnet/ ...
- 第二十篇:不为客户连接创建子进程的并发回射服务器(poll实现)
前言 在上文中,我使用select函数实现了不为客户连接创建子进程的并发回射服务器( 点此进入 ).但其中有个细节确实有点麻烦,那就是还得设置一个client数组用来标记select监听描述符集中被设 ...
- make: Warning: File `Makefile' has modification time 17 s in the future
linux下,make makefile文件的时候报警告: make: Warning: File `Makefile' has modification time 17 s in the futur ...
- 《转》Python学习(13)-Python的字符编码
转自 http://www.cnblogs.com/BeginMan/p/3166363.html 一.字符编码中ASCII.Unicode和UTF-8的区别 点击阅读:http://www.cnbl ...
- 【架构师之路】 LVS+Keepalived实现高可用负载均衡
一.原理 1.概要介绍 如果将TCP/IP划分为5层,则Keepalived就是一个类似于3~5层交换机制的软件,具有3~5层交换功能,其主要作用是检测web服务器的状态, ...