POJ2964--Tourist双重动态规划

本文介绍了一种解决懒游客问题的算法实现,该问题旨在寻找从城市左上角到右下角并返回的路径,使得经过有趣地点的数量最大化,且每个地点仅被访问一次。通过动态规划方法,定义状态转移方程,最终求得最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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[k][i][j]表示走了k步,第一个路径到第i行,第二个路径到第j行、
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 218
#define max(a,b)	a>b?a:b;
#define min(a,b)	a>b?b:a;
#define inf 0x3f3f3f3f
int dp[maxn][maxn][maxn];
char map[maxn][maxn];
int main()
{
	//freopen("in.txt","r",stdin);
	int t;
	scanf("%d",&t);
	while(t--)
	{
		memset(map,'#',sizeof(map));
		int r,c;
		scanf("%d%d",&c,&r);
		for(int i = 0;i < r;i++)
			scanf("%s",map[i]);
		int len = r+c-2;
		for(int i = 0;i <= len;i++)
			for(int j = 0;j < r;j++)
				for(int k = 0;k < r;k++)
					dp[i][j][k] = -inf;
		dp[0][0][0] = (map[0][0] == '*'?1:0);
		for(int i = 0;i <= c;i++)	map[r][i] = '#';
		for(int i = 0;i <= r;i++)	map[i][c] = '#';
		for(int k = 0;k < len;k++)
		{
			for(int i = 0;i <= k;i++)
			{
				if(i >= r)	break;
				if(k - i >= c)	continue;
				for(int j = 0;j <= k;j++)
				{
					if(j >= r)	break;
					if(k - j >= c)	continue;
					if(dp[k][i][j] < 0 || map[i][k-i] == '#' || map[j][k-j] == '#')	continue;
					if(map[i+1][k-i] != '#' && map[j+1][k-j] != '#')
					{
						int key = dp[k][i][j]+(map[i+1][k-i]=='*')+(map[j+1][k-j]=='*');
						if(i == j && map[i+1][k-i] == '*')	key--;//有钱				
						dp[k+1][i+1][j+1] = max(dp[k+1][i+1][j+1],key);
					}
					if(map[i+1][k-i] != '#' && map[j][k+1-j] != '#')
					{
						int key = dp[k][i][j]+(map[i+1][k-i]=='*')+(map[j][k+1-j]=='*');
						if(i+1 == j && map[i+1][k-i] == '*')	key--;
						dp[k+1][i+1][j] = max(dp[k+1][i+1][j],key);
					}
					if(map[i][k+1-i] != '#' && map[j+1][k-j] != '#')
					{
						int key = dp[k][i][j]+(map[i][k+1-i]=='*')+(map[j+1][k-j]=='*');
						if(i == j+1 && map[i][k+1-i] == '*')	key--;
						dp[k+1][i][j+1] = max(dp[k+1][i][j+1],key);
					}
					if(map[i][k+1-i] != '#' && map[j][k+1-j] != '#')
					{
						int key = dp[k][i][j]+(map[i][k+1-i]=='*')+(map[j][k+1-j]=='*');
						if(i == j && map[i][k+1-i] == '*')	key--;			
						dp[k+1][i][j] = max(dp[k+1][i][j],key);
					}
					
				}
			}
		}
		if(dp[r+c-2][r-1][r-1] < 0)	dp[r+c-2][r-1][r-1] = 0;
		printf("%d\n",dp[r+c-2][r-1][r-1]);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值