Codeforces Round #689 (Div. 2, based on Zed Code Competition)-B. Find the Spruce(DFS+记忆化搜索)

题目链接

Codeforces Round #689 (Div. 2, based on Zed Code Competition)-B. Find the Spruce


Description

  • Holidays are coming up really soon. Rick realized that it’s time to think about buying a traditional spruce tree. But Rick doesn’t want real trees to get hurt so he decided to find some in an n×m matrix consisting of “*” and “.”.
    To find every spruce first let’s define what a spruce in the matrix is. A set of matrix cells is called a spruce of height k with origin at point (x,y) if:
    All cells in the set contain an "".
    For each 1≤i≤k all cells with the row number x+i−1 and columns in range [y−i+1,y+i−1] must be a part of the set. All other cells cannot belong to the set.

Input

  • Each test contains one or more test cases. The first line contains the number of test cases t (1≤t≤10).
    The first line of each test case contains two integers n and m (1≤n,m≤500) — matrix size.
    Next n lines of each test case contain m characters ci,j — matrix contents. It is guaranteed that ci,j is either a “.” or an “*”.
    It is guaranteed that the sum of n⋅m over all test cases does not exceed 500^2 (∑n⋅m≤500^2).

Output

  • For each test case, print single integer — the total number of spruces in the matrix.

输入

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

输出

5
3
23
34

思路

  • 思路:实际上可以把题目意思转化为所有以"*"的点为树的顶点所能构成的云杉树的深度的和,例如样例一,第一排中间的"*“为顶点构成的树的深度为2,而第二排中间的”*"为顶点构成的树的深度为1所以ans=2+1+1+1=5
  • 方法:对每一个“*”用 dfs搜索 树的深度,因为要保证树完整所以dfs返回值为最小的树的深度+1。
  • 注意:要用记忆化搜索(二维数组dp储存dfs的值,避免重复搜索造成时间浪费),不然会超时。

AC代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

独一无二的VV

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值