zoukankan      html  css  js  c++  java
  • ZOJ 2975 Kinds of Fuwas(暴力+排列组合)

    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 <= MN <= 250), which means the number of rows and columns of the Fuwa matrix. And then there are M lines, each hasN 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
    

    Author: XUE, Zaiyue
    Source: The 5th Zhejiang Provincial Collegiate Programming Contest

    #include <iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    char mp[255][255];
    char fuwa[5]={'B','J','H','Y','N'};
    int t,n,m;
    long long sum;
    int main()
    {
        while(~scanf("%d",&t))
        {
        for(;t>0;t--)
        {
            scanf("%d%d",&n,&m);
            for(int i=0;i<n;i++)  scanf("%s",mp[i]);
            sum=0;
            for(int w=0;w<5;w++)
             for(int i=0;i<n-1;i++)
              for(int j=i+1;j<n;j++)
              {
               long long l=0;
               for(int k=0;k<m;k++)
               {
                  if (mp[i][k]==mp[j][k] && mp[i][k]==fuwa[w])
                        l++;
               }
               sum=sum+l*(l-1)/2;//关键是排列组合,节省时间,C(l,2);
              }
           printf("%lld
    ",sum);
        }
        }
        return 0;
    }
  • 相关阅读:
    USACO1.3.3Calf Flac
    USACO1.3.1Mixing Milk
    USACO1.3.2Barn Repair
    USACO2.1.4Healthy Holsteins
    USACO1.5.2Prime Palindromes
    USACO1.4.2The Clocks
    USACO2.1.2Ordered Fractions
    PHP关联数组教程
    你的服务器没有正确响应Token验证的解决方法
    微信公众平台消息接口开发(10)语音触发(非识别)
  • 原文地址:https://www.cnblogs.com/stepping/p/6406739.html
Copyright © 2011-2022 走看看