ZOJ 2975Kinds of Fuwas

来源:互联网 发布:c语言左移运算符 编辑:程序博客网 时间:2024/05/21 13:55

In the year 2008, the 29th Olympic Games will be held in Beijing. This will signify the prosperity of China as well as becoming a festival for people all over the world.

The official mascots of Beijing 2008 Olympic Games are Fuwa, which are named as Beibei, Jingjing, Haunhuan, Yingying and Nini. Fuwa embodies the natural characteristics of the four most popular animals in China -- Fish, Panda, Tibetan Antelope, Swallow -- and the Olympic Flame. To popularize the official mascots of Beijing 2008 Olympic Games, some volunteers make a PC game with Fuwa.

As shown in the picture, the game has a matrix of Fuwa. The player is to find out all the rectangles whose four corners have the same kind of Fuwa. You should make a program to help the player calculate how many such rectangles exist in the Fuwa matrix.

Input

Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 50) which is the number of test cases. And it will be followed by T consecutive test cases.

The first line of each test case has two integers M and N (1 <= MN <= 250), which means the number of rows and columns of the Fuwa matrix. And then there are M lines, each has N characters, denote the matrix. The characters -- 'B' 'J' 'H' 'Y' 'N' -- each denotes one kind of Fuwa.

Output

Results should be directed to standard output. The output of each test case should be a single integer in one line, which is the number of the rectangles whose four corners have the same kind of Fuwa.

Sample Input

22 2BBBB5 6BJHYNBBHBYYHBNBYNNJNBYNNBHBYYH

Sample Output

1

8

直接考虑两行之间的合法矩形即可。

#include<map>#include<ctime>#include<cmath>    #include<queue> #include<string>#include<vector>#include<cstdio>    #include<cstring>  #include<iostream>#include<algorithm>    using namespace std;#define ms(x,y) memset(x,y,sizeof(x))    #define rep(i,j,k) for(int i=j;i<=k;i++)    #define per(i,j,k) for(int i=j;i>=k;i--)    #define loop(i,j,k) for (int i=j;i!=-1;i=k[i])    #define inone(x) scanf("%d",&x)    #define intwo(x,y) scanf("%d%d",&x,&y)    #define inthr(x,y,z) scanf("%d%d%d",&x,&y,&z)    typedef long long LL;const int low(int x) { return x&-x; }const int INF = 0x7FFFFFFF;const int mod = 1e9 + 7;const int N = 1e3 + 10;int T, n, m, ans, c[5];char s[N][N], g[N];int main(){g['B'] = 0; g['J'] = 1; g['H'] = 2; g['Y'] = 3; g['N'] = 4;for (inone(T); T--;){intwo(n, m);rep(i, 1, n) scanf("%s", s[i] + 1);ans = 0;rep(i, 1, n){rep(j, i + 1, n){ms(c, 0);rep(k, 1, m) if (s[i][k] == s[j][k]) c[g[s[i][k]]]++;rep(k, 0, 4) ans += c[k] * (c[k] - 1) / 2;}}printf("%d\n", ans);}return 0;}


0 0
原创粉丝点击