题目:
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.
输入
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.
输出
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.
样例输入
2
2 2
BB
BB
5 6
BJHYNB
BHBYYH
BNBYNN
JNBYNN
BHBYYH
样例输出
1
8
代码:
#include <bits/stdc++.h>
using namespace std;
char ch[5]={'B','J','H','Y','N'};
char mp[255][255];
int f(char c){
for(int i=0;i<5;i++){
if(c==ch[i]) return i;
}
}
int main()
{
int t,n,m;
scanf("%d",&t);
while(t--){
int sum=0;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++){
scanf("%s",mp[i]);
}
for(int i=0;i<m;i++){
for(int j=i+1;j<m;j++){
int s[5]={0};//固定好i,j后,计算n行中每种福娃共有几行
for(int k=0;k<n;k++){
if(mp[k][i] == mp[k][j]){
s[f(mp[k][i])]++;
}
}
for(int t=0;t<5;t++){
sum+=s[t]*(s[t]-1)/2;
}
}
}
printf("%d\n",sum);
}
return 0;
}
第一想法直接暴力,硬的搜索,但是一想肯定超时。第二想法,把5种福娃分开计算,可以节约一点时间,拿vector存下每种福娃的i,j…然后又是暴力…。第三想法,肯定有其他不这么暴力的方法…存下数量拿组合数计数,不用每次找到++
今天可以直接插入代码了,昨天一定是我操作不当