Zoj problem set-2975 kinds of fuwas

Source: Internet
Author: User
Kinds of fuwas

Time limit:1 second Memory limit:32768 KB

In the year 2008, the 29th Olympus IC 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 Olympus 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 Olympus flame. to popularize the official mascots of Beijing 2008 Olympus 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 shoshould 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 integerT(1 <=T<= 50) which is the number of test cases. And it will be followedTConsecutive test cases.

The first line of each test case has two integersMAndN(1 <=M,N<= 250), which means the number of rows and columns of the Fuwa matrix. And then there areMLines, each hasNCharacters, denote the matrix. The characters -- 'B' J ''h'' y ''n' -- each denotes one kind of Fuwa.

Output

Results shocould be directed to standard output. The output of each test case shocould 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 6 bjhynbbhbyyhbnbynnjnbynnbhbyyh

Sample output

 
18

Author:Xue, zaiyue
Source:The 5th Zhejiang Provincial Collegiate Programming Contest

Source code:

 

 
# Include < Iostream >
# Include < Vector >

Using Namespace STD;
Int Main ()
{
Int Cases; CIN > Cases;
While (Cases -- )
{
Int X, Y; CIN > X > Y;
Vector < Vector < Char > > Martrix (x, Vector < Char > (Y ));
For ( Int IX = 0 ; Ix < X; ix ++ )
For ( Int Iy = 0 ; Iy < Y; Iy ++ )
CIN > Martrix [ix] [Iy];
Int Fumas = 0 ;
For ( Int I = 0 ; I < X; I ++ )
{
For ( Int II = I + 1 ; II < X; II ++ )
{
Int B0 = 0 , J0 = 0 , H0 = 0 , Y0 = 0 , N0 = 0 ;
For ( Int K = 0 ; K < Y; k ++ )
{
If (Martrix [I] [k] = Martrix [II] [k])
{
Switch (Martrix [I] [k])
{
Case ' B ' :
B0 ++ ;
Break ;
Case ' J ' :
J0 ++ ;
Break ;
Case ' H ' :
H0 ++ ;
Break ;
Case ' Y ' :
Y0 ++ ;
Break ;
Case ' N ' :
N0 ++ ;
Break ;
Default :
Break ;
}
}
}
Fumas + = (B0 * (B0 - 1 ) / 2 + J0 * (J0 - 1 ) / 2 + H0 * (H0 - 1 ) / 2 + Y0 * (Y0 - 1 ) / 2 + N0 * (N0 - 1 ) / 2 );
}
}
Cout < Fumas < Endl;
}
}

You can also get the correct answer for this question by searching, but it will be TLE.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.