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.
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.
'.' 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; }