Kinds of Fuwas(暴力+组合数学)

来源:互联网 发布:iphone剪辑音乐软件 编辑:程序博客网 时间:2024/06/01 22:36
K - Kinds of Fuwas
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu
Submit Status Practice ZOJ 2975

Description

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

18
思路:
   这题我一开始想到是直接暴力,没想到两行之间先求出来是否组成一个相同的列,所以要多复杂有多复杂。想着想着,发现了上面提到的思路,之后就是要将多个相同列进行组合矩形了。这里我想到了杨辉三角求组合的东西,因为自己写组合的东西太麻烦了。之后暴力找两行就行了。
AC代码:
#include<iostream>#include<algorithm>#include<cstring>#include<string>#include<cstdio>#include<map>#include<vector>using namespace std;#define T 1000typedef long long ll;int C[T][T];void paly_table(){int i,j;for(i=0;i<=300;++i){C[i][0] = 1;C[i][i] = 1;}for(i=2;i<=300;++i){for(j=1;j<7;++j){C[i][j] = C[i-1][j-1]+C[i-1][j];}}}int num1[T][T];int row,col;int solve(){int i,j,k,cnt=0,c1,c2,c3,c4,c5;for(i=0;i<row-1;++i){/*c1 = c2 =c3 =c4 = c5 = 0;*/for(j=i+1;j<row;++j){c1 = c2 =c3 =c4 = c5 = 0;for(k=0;k<col;++k){if(num1[i][k]==num1[j][k]){if(num1[i][k]==1){    c1++;}else if(num1[i][k]==2){c2++;}else if(num1[i][k]==3){c3++;}else if(num1[i][k]==4){c4++;}else {c5++;}}}if(c1>1)cnt+=C[c1][2]; if(c2>1)cnt+=C[c2][2];if(c3>1)cnt+=C[c3][2];if(c4>1)cnt+=C[c4][2];if(c5>1)cnt+=C[c5][2];}}return cnt;}int main(){#ifdef zscfreopen("input.txt","r",stdin);#endifint n,i,j;char c;paly_table();scanf("%d",&n);while(n--){scanf("%d%d",&row,&col);for(i=0;i<row;++i){for(j=0;j<col;++j){scanf("\n%c",&c);if(c=='B'){num1[i][j] = 1;}else if(c=='J'){num1[i][j] = 2;}else if(c=='H'){num1[i][j] = 3;}else if(c=='Y'){num1[i][j] = 4;}else{num1[i][j] =5;}}}printf("%d\n",solve());}return 0;}
学长高端代码(看了之后内心崩溃了大哭):

1 0
原创粉丝点击