Carnival Slots dfs+dp

Carnival Slots

Time limit 1500 ms
Memory limit 262144 kB

You go to the carnival and come across a nice little game. The carnival worker shows you the setup of the game, which can be represented as a 2-dimensional grid g with r rows and c columns. You are given the opportunity to change around the grid and maximize your score before the worker drops several balls into each column.

A cell in the ith row (from top) and the jth column (from left) is denoted by (i, j), and can be one of three different types of cells:

1.’.’; a ball that enters this cell will go to cell (i + 1, j).
2.’’; a ball that enters this cell will go to cell (i + 1, j + 1).
3.’/’; a ball that enters this cell will go to cell (i + 1, j - 1).
You may change a cell of type 2 and 3 to any of the three types, and you can change as many cells as you want. Cells of type 1 can’t be changed.

Under the grid is aligned c buckets, where the ith bucket is below the ith column.

Each of the c buckets contains a score. For every ball that falls into a bucket, the score on that bucket is added to your total score and that ball stops. A ball that doesn’t fall into a bucket gets a score of 0.

You are given how many balls the worker will drop into each column. Balls are dropped one after the another such that no two balls will collide. After making the changes, what is the maximum score you can achieve?

Your only condition for the grid g is that it’s not allowed to have two adjacent cells in one row such that the left one is ‘’ and the right one is ‘/’.

Input
The first line of input contains a single integer T (1 ≤ T ≤ 5300), the number of test cases.

The first line of each test case contains two space-separated integers r and c (1 ≤ r, c ≤ 500), the dimensions of the grid.

The following line contains c space-separated integers b1, b2, …, bc (0 ≤ bi ≤ 108), where bi is the number of balls dropped into the ith column.

Each of the following r lines contains c characters, representing the grid. Each character is either ‘.’, ‘’, or ‘/’.

The last line of each test case contains c space-separated integers s1, s2, …, sc (0 ≤ si ≤ 108), where si is the score added after one ball drops into the ith bucket.

The sum of r × c over all test cases doesn’t exceed 4 × 106.

Output
For each test case, print on a single line the maximum score possible.

Example

Input
2
3 3
1 2 1
…/
./.

10 5 20
1 2
100000000 100000000

1 100000000

Output

70
10000000100000000

题目来源:2018 ACM-ICPC, Syrian Collegiate Programming Contest

题目vj上的题源

题意:在第一行丢球,经过路程后,到达所能到达的最大底部数值,使合起来的数值最大。一开始就给出个初步的网格,后面遇到特殊符号可以改符号来进行遍历。

解题思路:遍历每个点,遇到分叉路口若满足条件,没有溢出便分别遍历,这里使用dp的原因是用于节省时间,没加dp的话会导致超时。

#include
using namespace std;
typedef long long ll;
char a[505][505];
ll in[505];
ll out[505];
ll dp[505][505];
int r,c;
ll dfs(int x,int y)
{
    if(x==r+1)return out[y];
    ll ans=0;
    if(dp[x][y]!=-1)return dp[x][y];
    ans=dfs(x+1,y);
    if(y-1>=1&&a[x][y]!='.')ans=max(ans,dfs(x+1,y-1));
    if(y+1<=c&&a[x][y]!='.')ans=max(ans,dfs(x+1,y+1));
    return dp[x][y]=ans;
}
int main()
{
    freopen("balls.in","r",stdin);
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&r,&c);
        for(int i=1; i<=c; i++)scanf("%d",&in[i]);
        for(int i=1; i<=r; i++)scanf("%s",a[i]+1);
        for(int i=1; i<=c; i++)scanf("%d",&out[i]);
        ll ans=0;
        memset(dp,-1,sizeof dp);
        for(int i=1; i<=c; i++)ans+=dfs(1,i)*in[i];
        printf("%lld\n",ans);
    }
    return 0;
}

你可能感兴趣的:(dp,dfs,C++)