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 <= M, N <= 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
2
2 2
BB
BB
5 6
BJHYNB
BHBYYH
BNBYNN
JNBYNN
BHBYYH
Sample Output
1
8
思路:按竖行找,遇到竖行相同的就开始横向找,直到找到横向相同的字符,且与该字符同竖向的字符相同,暴力累加。
AC代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<queue>
using namespace std;
int main()
{
int t,i,j,k;
scanf("%d",&t);
char a[300][300];
while(t--)
{
int m,n;
scanf("%d%d",&m,&n);
getchar();
for(i=0;i<m;i++)
{
gets(a[i]);
}
int s=0;
int p=0;
for(i=0;i<m;i++)
{
for(j=i+1;j<m;j++)
{
for(k=0;k<n;k++)
{
if(a[i][k]==a[j][k])
{
for(p=k+1;p<n;p++)
{
if(a[i][p]==a[j][p]&&a[i][k]==a[i][p])
{
s++;
}
}
}
}
}
}
printf("%d\n",s);
}
}
题目传送门:Kinds of Fuwas(点击可交题)