ZOJ2975

Kinds of Fuwas


Time Limit: 2 Seconds      Memory Limit: 65536 KB


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

 

题意:选择一个矩形, 并且这个矩形的四个顶点都相同;问可以选择多少个这样的矩形。

题目中的数据都不大,所以我想的方法是枚举,因为满不满足条件只和矩形的4条边有关系,所以可以枚举这4条边,但是要注意优化。无脑枚举的话会TLE。

 

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#define clr(x) memset(x, 0, sizeof(x))
using namespace std;
char mp[300][300];
int vis[300][300];
int main()
{
    int T,n;
    scanf("%d",&T);
    while(T--)
    {
        clr(vis);
        clr(mp);
        int r, c;
        char mid;
        scanf("%d %d", &r, &c);
        for(int i = 0; i < r; i++)
            scanf("%s", mp[i]);
        int sum = 0;
        for(int i = 0; i < r - 1; i++)
            for(int k = 0; k < c - 1; k++)
                {
                    char mid = mp[i][k];
                    for(int j = i + 1; j < r; j++)
                    {
                        if(mp[j][k] != mid)
                        continue;
                        for(int l = k + 1; l < c; l++)
                        {
                            if(mp[i][l] == mid && mp[j][l] == mid)
                            sum++;
                        }
                    }
                }
        cout << sum << endl;
    }

    return 0;
}

 

 

 

 

 

还有一种方法是组合数学,高中可能也学过这个知识。先枚举两行,再从第一列到最后一列,看有多少个相同的。然后就可以组合成n*(n - 1)/2个矩形。

 

#include<bits/stdc++.h>

#define clr(x) memset(x, 0, sizeof(x))
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 100005;
string mp[300];
int main()
{
    int T;
    cin >> T;
    while(T--)
    {
        for(int i = 0; i < 300; i++)
            mp[i].clear();
        int row, col;
        cin >> row >> col;
        for(int i = 0; i < row; i++)
            cin >> mp[i];
        int ans = 0;
        for(int i = 0; i < row - 1; i++)
            for(int j = i + 1; j < row; j++)
            {
                int a, b, c, d, e;
                a = b = c = d = e = 0;
                for(int k = 0; k < col; k++)
                {
                    if(mp[i][k] == mp[j][k])
                    {

                        if(mp[j][k] == 'B') a++;
                        else if(mp[j][k] == 'J') b++;
                        else if(mp[j][k] == 'H') c++;
                        else if(mp[j][k] == 'Y') d++;
                        else e++;
                    }
                }
                //cout << a << b << c << d << e << endl;
                ans = ans + (a * (a - 1) + b * (b - 1) + c * (c - 1) + d * (d - 1) + e * (e - 1)) / 2;
            }
        cout << ans << endl;
    }
}

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值