题面
原题链接:CodeForces - 629A
Door’s family is going celebrate Famil Doors’s birthday party. They love Famil Door so they are planning to make his birthday cake weird!
The cake is a n × n square consisting of equal squares with side length 1. Each square is either empty or consists of a single chocolate. They bought the cake and randomly started to put the chocolates on the cake. The value of Famil Door’s happiness will be equal to the number of pairs of cells with chocolates that are in the same row or in the same column of the cake. Famil Doors’s family is wondering what is the amount of happiness of Famil going to be?
Please, note that any pair can be counted no more than once, as two different cells can’t share both the same row and the same column.
Input
In the first line of the input, you are given a single integer n (1 ≤ n ≤ 100) — the length of the side of the cake.
Then follow n lines, each containing n characters. Empty cells are denoted with ‘.’, while cells that contain chocolates are denoted by ‘C’.
Output
Print the value of Famil Door’s happiness, i.e. the number of pairs of chocolate pieces that share the same row or the same column.
Examples
Input
3
.CC
C…
C.C
Output
4
Input
4
CC…
C…C
.CC.
.CC.
Output
9
Note
If we number rows from top to bottom and columns from left to right, then, pieces that share the same row in the first sample are:
(1, 2) and (1, 3)
(3, 1) and (3, 3)
Pieces that share the same column are:
(2, 1) and (3, 1)
(1, 3) and (3, 3)
题意
给你一个矩形蛋糕,C代表巧克力,“.”表示空。每一行每一列的巧克力的对数 代表幸福值,问题目中所给数据的幸福值是多少
思路
记录每一行每一列的巧克力(C)的个数x,然后求组合数 c(x,2)的值,并将所有组合数相加。
代码
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <vector>
using namespace std;
char s[202][202];
int h[202020], l[202020];
int main()
{
int n;
cin >> n;
memset(h, 0, sizeof(h));
memset(l, 0, sizeof(l));
for (int i = 0; i < n;i++)
cin >> s[i];
for (int i = 0; i < n;i++)
{
for (int j = 0; j < n;j++)
{
if(s[i][j]=='C')
h[i]++;//记录每一行的个数
if(s[j][i]=='C')
l[i]++;//记录第j列每行的个数
}
}
int sum = 0;
for (int i = 0; i < n;i++)//求组合数
{
sum += (h[i] * (h[i] - 1)) / 2;
sum += (l[i] * (l[i] - 1)) / 2;
}
cout << sum << endl;
}