首页 >> 大全

Find the Spruce(dp 动态规划)

2023-12-20 大全 29 作者:考证青年

are up soon. Rick that it’s time to think about a tree. But Rick doesn’t want real trees to get hurt so he to find some in an n×m of “*” and “.”.

To find every first let’s what a in the is. A set of cells is a of k with at point (x,y) if:

All cells in the set an “*”.

For each 1≤i≤k all cells with the row x+i−1 and in range [y−i+1,y+i−1] must be a part of the set. All other cells to the set.

of and trees:

Now Rick wants to know how many his n×m . Help Rick solve this .

Input

Each test one or more test cases. The first line the of test cases t (1≤t≤10).

The first line of each test case two n and m (1≤n,m≤500) — size.

Next n lines of each test case m ci,j — . It is that ci,j is a “.” or an “*”.

It is that the sum of n⋅m over all test cases does not 5002 (∑n⋅m≤5002).

For each test case, print — the total of in the .

动态规划算法经典例题__动态规划的基本思想

Input

4
2 3
.*.
***
2 3
.*.
**.
4 5
.***.
*****
*****
*.*.*
5 7
..*.*..
.*****.
*******
.*****.
..*.*..

5
3
23
34

Note

In the first test case the first of 2 has its at point (1,2), the of 1 has its at point (1,2), the third of 1 has its at point (2,1), the of 1 has its at point (2,2), the fifth of 1 has its at point (2,3).

In the test case the first of 1 has its at point (1,2), the of 1 has its at point (2,1), the third of 1 has its at point (2,2).

其实标注也没啥用 就是让你求有多少个树(第一行1个第二行3个第三行5个。。。。。)

#include
#include
#include
#include
using namespace std;
#define ll long long
const int N=1e6+10;
int dp[550][550];
char a[550][550];
int main()
{int m;scanf("%d",&m);while(m--){memset(dp,0,sizeof(dp));int n,k,sum=0;scanf("%d %d",&n,&k);for(int i=1;i<=n;i++)scanf("%s",a[i]+1);for(int i=n;i>=1;i--){for(int j=1;j<=k;j++){if(a[i][j]=='*'){dp[i][j]=1;dp[i][j]+=min(dp[i+1][j-1],min(dp[i+1]  [j],dp[i+1][j+1]));   //因为已经清空了 所以可以从最后一行遍历sum+=dp[i][j];//  printf("###%d\n",dp[i][j]);}}}printf("%d\n",sum);}return 0;
}

关于我们

最火推荐

小编推荐

联系我们


版权声明:本站内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 88@qq.com 举报,一经查实,本站将立刻删除。备案号:桂ICP备2021009421号
Powered By Z-BlogPHP.
复制成功
微信号:
我知道了