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.

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<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#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<char ,int > 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<m;i++){
            for(int j=i+1;j<=m;j++){
                if(g[n][i]==g[n][j]){
                    dp[i][j][p[g[n][i]]]++;
                }
            }
        }
        //從右下角遍歷到左上角
        for(int i=n-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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章