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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值