ACM训练——Kinds of Fuwas

题目链接:https://vjudge.net/problem/ZOJ-2975

 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.

ACM训练——Kinds of Fuwas_第1张图片

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

2
2 2
BB
BB
5 6
BJHYNB
BHBYYH
BNBYNN
JNBYNN
BHBYYH

Sample Output

1
8

 思路:如果暴力四重for循环会超时,需要用dp记录一下一些信息

#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ll long long
#define inf 0x3f3f3f3f
#define sd(a) scanf("%d",&a)
#define sdd(a,b) scanf("%d%d",&a,&b)
#define cl(a,b) memset(a,b,sizeof(a))
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define sddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define dbg() printf("aaa\n")
using namespace std;
const int maxn=300;
char g[maxn][maxn];
int dp[maxn][maxn][10];
map p;
int main() {
	int T;
    sd(T);
    p['B']=1;p['J']=2;p['H']=3;p['I']=4;p['N']=5;
    while(T--){
        int n,m;
        sdd(n,m);
        rep(i,1,n){
            scanf("%s",g[i]+1);
        }
        memset(dp,0,sizeof(dp));
        ll cnt=0;
        //预处理最后一行:
        //dp[i][j][p[g[n][i]]]表示同一行的第i列和第j列都是福娃p[g[n][i]]的个数
        for(int i=1;i=1;i--){//第i行
            for(int j=m-1;j>0;j--){//第j列
                for(int k=j+1;k<=m;k++){//第k列
                    if(g[i][j]==g[i][k]){
                        //如果true,看下面的行的第j列和第k列为颜色g[i][j]的个数
                        cnt+=dp[j][k][p[g[i][j]]];
                        dp[j][k][p[g[i][j]]]++;//统计个数加一
                    }
                }
            }
        }
        printf("%lld\n",cnt);
    }
	return 0;
}

 

你可能感兴趣的:(思维题)