题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2975
给出n*m的矩阵,要求算出四个角类型相同的小矩形的数目
使用暴力枚举任意两列,然后枚举每一行,统计每行的两列相同的个数,然后使用组合数求解,时间复杂度250^3.
#include<iostream>
#include<cstdio>
#include<set>
#include<string>
#include<string.h>
#include<cstring>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<cctype>
#include<algorithm>
#include<sstream>
#define mt(a) memset(a,0,sizeof a)
#define fl(a,b,c) fill(a,b,c)
#define inf 1000000000+7
using namespace std;
typedef long long ll;
char graph[300][300];
ll g[5];
int main()
{
int T;
cin >> T;
while (T--)
{
int n, m;;
ll ans = 0;
memset(graph, 0, sizeof graph);
memset(g, 0, sizeof g);
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++)
scanf("%s", graph[i]);
for (int i = 0; i < m; i++)
{
for (int j = i + 1; j < m; j++)
{
memset(g, 0, sizeof g);
for (int k = 0; k < n; k++)
{
if (graph[k][i] == graph[k][j])
{
if (graph[k][i] == 'B')g[0]++;
else if (graph[k][i] == 'J')g[1]++;
else if (graph[k][i] == 'H')g[2]++;
else if (graph[k][i] == 'Y')g[3]++;
else if (graph[k][i] == 'N')g[4]++;
}
}
for (int k = 0; k < 5; k++)
{
if(g[k])ans += ((g[k] * (g[k] - 1)) / 2);
}
}
}
printf("%lld\n", ans);
}
return 0;
}
本文介绍了一种解决矩阵问题的算法,即计算矩阵四个角类型相同的小矩形数量。通过暴力枚举两列并统计每行两列相同元素的数量,最后利用组合数学求解。此算法的时间复杂度为O(n^3)。
&spm=1001.2101.3001.5002&articleId=50699597&d=1&t=3&u=fd3ae389e35d4146b34a80ceefefccee)
875

被折叠的 条评论
为什么被折叠?



