ACM: 简单DP 动态规划题 toj 1547
2016-05-19 23:22
197 查看
Kinds of Fuwas
描述
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.
![](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
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.
输入
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.
输出
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.
样例输入
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 int
ca2(int a)
{
return a*(a-1) / 2;
}
int
main()
{
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)
{
int
bb = 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++;
else
if(g[i][k] == 'J') jj++;
else
if(g[i][k] == 'H') hh++;
else
if(g[i][k] == 'Y') yy++;
else
if(g[i][k] == 'N') nn++;
}
}
result
+= ca2(bb) + ca2(jj) + ca2(hh) + ca2(yy) + ca2(nn);
}
}
printf("%d\n",result);
}
return 0;
}
相关文章推荐
- ACM: 动态规划题 toj 1511
- ACM: 动态规划题 toj 1545
- 2
- 人工智能60周年前沿趋势交流会【转】
- Java编码浅析(注意区分三个概念)(转)
- Jmeter(5)属性和变量
- 2012年广东ACM省赛赛后总结
- ACM: 浙江省赛题目 22:00 ~ 3:00 (…
- ACM: 无向图的强连通分量 图论题 p…
- ACM: 最近公共祖先问题 图论题
- ACM: 广搜 图论题 poj 1708
- ACM: 闭包问题 图论题 (书上练习)
- ACM: 图论题 poj 1161 (把图重建成…
- ACM: 图论题 poj 1135
- ACM: 简单 动态规划题 toj 1509 (…
- Largest prime 尤拉托斯展那筛选法
- ACM: 动态规划题 toj 1508
- ACM: 训练题 动态规划题 (蛮有意思…
- ACM: 动态规划体 toj 1408
- ACM: 背包问题 动态规划题 toj 133…