You are given a n × m field consisting only of periods ('.') and asterisks ('*'). Your task is to count all right triangles with two sides parallel to the square sides, whose vertices are in the centers of '*'-cells. A right triangle is a triangle in which one angle is a right angle (that is, a 90 degree angle).
The first line contains two positive integer numbers n and m (1 ≤ n, m ≤ 1000). The following n lines consist of m characters each, describing the field. Only '.' and '*' are allowed.
Output a single number — total number of square triangles in the field. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
2 2 ** *.
1
3 4 *..* .**. *.**
9
题意:给你一个n*m网格,求直角边和坐标轴平行且由*组成的直角三角形个数。
解法:暴力,直接算出每行每列的*个数,枚举每个*,贡献为(x[i]-1)*(y[i]-1)。
代码:
#include <iostream>
#include <cstdio>
using namespace std;
int x[1005],y[1005],n,m;
char map[1005][1005];
long long ans;
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%s",map[i]+1);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(map[i][j]=='*')
x[i]++,y[j]++;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(map[i][j]=='*')
ans+=(x[i]-1)*(y[j]-1);
printf("%I64d\n",ans);
}