ACM: 简单DP 动态规划题 toj 1547

来源:互联网 发布:淘宝网左轮吉他直播 编辑:程序博客网 时间:2024/05/22 06:09

Kinds of Fuwas

描述

 

In the year 2008, the 29thOlympic Games will be held in Beijing. This will signify theprosperity of China as well as becoming a festival for people allover 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 popularanimals in China -- Fish, Panda, Tibetan Antelope, Swallow -- andthe Olympic Flame. To popularize the official mascots of Beijing2008 Olympic Games, some volunteers make a PC game with Fuwa.

ACM: <wbr>简单DP <wbr>动态规划题 <wbr>toj <wbr>1547

As shown in the picture, the game has a matrix of Fuwa. Theplayer is to find out all the rectangles whose four corners havethe same kind of Fuwa. You should make a program to help the playercalculate how many such rectangles exist in the Fuwa matrix.

输入

 

Standard input willcontain multiple test cases. The first line of the input is asingle integer T (1 <= T<= 50) which is the number of test cases. And itwill be followed by T consecutive test cases.

The first line of each test case has two integers M andN (1 <= M, N <=250), which means the number of rows and columns of the Fuwamatrix. And then there are M lines, each has Ncharacters, denote the matrix. The characters -- 'B' 'J' 'H' 'Y''N' -- each denotes one kind of Fuwa.

输出

Resultsshould be directed to standard output. The output of each test caseshould be a single integer in one line, which is the number of therectangles whose four corners have the same kind ofFuwa.

样例输入

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

 

样例输出

1
8

 

题意:  给你一个矩阵,找出子矩阵中四个角的fuwa是相同的个数.

 

解题思路:

               1. 控制行的范围即可. 列的可以用排列组合计算出结果 Ca2: 从a中选取2个组合.

 

代码:

#include<cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 255
int n,m;
char g[MAX][MAX];
inline intca2(int a)
{
 return a*(a-1) / 2;
}
intmain()
{
 int i, j, k;
// freopen("input.txt","r",stdin);
 int caseNum;
 scanf("%d",&caseNum);
 while(caseNum--)
 {
  scanf("%d%d",&n, &m);
  for(i = 0; i <n; ++i)
  {
   getchar();
   for(j = 0; j< m; ++j)
    scanf("%c",&g[i][j]);
  }
  int result =0;
  for(i = 0; i <n; ++i)
  {
   for(j = i+1;j < n; ++j)
   {
    intbb = 0, jj = 0, hh = 0, yy = 0, nn = 0;
    for(k= 0; k < m; ++k)
    {
     if(g[i][k]== g[j][k])
     {
      if(g[i][k]== 'B') bb++;
      elseif(g[i][k] == 'J') jj++;
      elseif(g[i][k] == 'H') hh++;
      elseif(g[i][k] == 'Y') yy++;
      elseif(g[i][k] == 'N') nn++;
     }
    }
    result+= ca2(bb) + ca2(jj) + ca2(hh) + ca2(yy) + ca2(nn);
   }
  }
  printf("%d\n",result);
 }
 return 0;
}

 

0 0
原创粉丝点击