B. Find the Spruce(递推)

2023-12-20 10:20
文章标签 find 递推 spruce

本文主要是介绍B. Find the Spruce(递推),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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.
Examples of correct and incorrect spruce trees:
在这里插入图片描述

Now Rick wants to know how many spruces his n×m matrix contains. Help Rick solve this problem.

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 5002 (∑n⋅m≤5002).

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

Example
input
4
2 3
.*.


2 3
..
.
4 5
.
.



..*
5 7
.
.*****.


.*****.
.
output
5
3
23
34
Note
In the first test case the first spruce of height 2 has its origin at point (1,2), the second spruce of height 1 has its origin at point (1,2), the third spruce of height 1 has its origin at point (2,1), the fourth spruce of height 1 has its origin at point (2,2), the fifth spruce of height 1 has its origin at point (2,3).

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

#include <bits/stdc++.h>
typedef long long ll;
const ll mod = 9999999967;
using namespace std;
namespace fastIO {inline void input(int& res) {char c = getchar();res = 0;int f = 1;while (!isdigit(c)) { f ^= c == '-'; c = getchar(); }while (isdigit(c)) { res = (res << 3) + (res << 1) + (c ^ 48);c = getchar(); }res = f ? res : -res;}inline ll qpow(ll a, ll b) {ll ans = 1, base = a;while (b) {if (b & 1) ans = (ans * base % mod +mod )%mod;base = (base * base % mod + mod)%mod;b >>= 1;}return ans;}
}
using namespace fastIO;
const int N = 1e6 + 5;int Case,n,m;
char s[505][505];
int ans[505][505];int cal(int x,int y){int res = 0;int minn;ans[x][y]=1;if(x+1>n||y<2||y+1>m) return -1;minn = 0x3f3f3f3f;for(int i=y-1;i<=y+1;i++){minn = min(minn,ans[x+1][i]);}ans[x][y] += minn;return minn+1;
}void solve(){ll res = 0;scanf("%d %d",&n,&m);for(int i=1;i<=n;i++){getchar();scanf("%s",s[i]+1);}for(int i=n;i>=1;i--){for(int j=1;j<=m;j++){if(s[i][j]=='*') {if(cal(i,j)==-1) {res++;continue;}res += cal(i,j);}}}printf("%lld\n",res);memset(ans,0,sizeof(ans));
}int main(){//init();Case=1;scanf("%d",&Case);while(Case--){solve();}return 0;
}

这篇关于B. Find the Spruce(递推)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/515677

相关文章

uva 568 Just the Facts(n!打表递推)

题意是求n!的末尾第一个不为0的数字。 不用大数,特别的处理。 代码: #include <stdio.h>const int maxn = 10000 + 1;int f[maxn];int main(){#ifdef LOCALfreopen("in.txt", "r", stdin);#endif // LOCALf[0] = 1;for (int i = 1; i <=

MongoDB学习—(6)MongoDB的find查询比较符

首先,先通过以下函数向BookList集合中插入10000条数据 function insertN(obj,n){var i=0;while(i<n){obj.insert({id:i,name:"bookNumber"+i,publishTime:i+2000})i++;}}var BookList=db.getCollection("BookList")调用函数,这样,BookList

【NodeJS】Error: Cannot find module 'ms'

转载自:http://blog.csdn.net/echo_ae/article/details/75097004 问题: Error: Cannot find module 'ms'at Function.Module._resolveFilename (module.js:469:15)at Function.Module._load (module.js:417:25)at Module

leetCode#448. Find All Numbers Disappeared in an Array

Description Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this

访问controller404:The origin server did not find a current representation for the target resource

ider build->rebuild project。Rebuild:对选定的目标(Project),进行强制性编译,不管目标是否是被修改过。由于 Rebuild 的目标只有 Project,所以 Rebuild 每次花的时间会比较长。 参考:资料

mybatis错误——java.io.IOException Could not find resource comxxxxxxMapper.xml

在学习Mybatis的时候,参考网上的教程进行简单demo的搭建,配置的没有问题,然后出现了下面的错误! Exception in thread "main" java.lang.RuntimeException: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause:

HLJUOJ1128 HDU2046(数学递推)

1128: 递推求解专题练习三 Time Limit: 1 Sec   Memory Limit: 128 MB Submit: 8   Solved: 6 [ Submit][ Status][ Web Board] Description 在2×n的一个长方形方格中,用一个1× 2的骨牌铺满方格,输入n ,输出铺放方案的总数。 例如n=3时,为2× 3方格,骨牌的铺放方案有三

Linux 中的 find 命令介绍以及使用

文章目录 Linux 中的 `find` 命令详解及使用示例`find` 命令的基本语法常用的 `find` 命令选项按文件名搜索:`-name`按文件类型搜索:`-type`按文件大小搜索:`-size`按修改时间搜索:`-mtime`按权限搜索:`-perm`按所有者搜索:`-user` 和 `-group` `find` 命令的常见操作删除找到的文件:`-exec` 和 `rm`查找并

mysql中find_in_set()函数

1.场景 假设有一个user用户表,表字段分别为:id(主键),name(姓名),age(年龄),hobby(爱好)。而一个人可能有好几个爱好,游泳啊篮球啊乒乓球啊等等。数据库里hobby字段存的是:游泳,篮球,乒乓球 而要想查所有喜欢游泳的人,就可以用find_in_set函数了 todo:贴图 2.使用 select *from user where find_in_set("游泳"

Failed to find style 'vpiCirclePageIndicatorStyle' in current theme

使用 ViewPagerIndicator 时 , 布局文件报的错 Missing styles. Is the correct theme chosen for this layout? Use the Theme combo box above the layout to choose a different layout, or fix the theme style refe