题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2975
题 意:找出所给矩阵中四个角相同的子矩阵的个数。
思 路:找出第i列与第j列字母相同的行数,再用排列组合从中选2个,由此计算所有的i-j列中满足条件的个数。
代码如下:
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <sstream>
#include <algorithm>
using namespace std;
typedef long long LL;
char v[255][255];
int ans, n, m;
int vis[255][255];
void slove( char ch )
{
memset( vis, 0, sizeof(vis) );
for(int i = 1; i <= n; i ++ )
{
for( int j = 1; j <= m; j ++ )
if( v[i][j] == ch )
{
int p;
for( p = j+1; p <= m; p ++ )
if( v[i][p] == ch )
{
vis[j][p]++;
}
}
}
for( int i = 1; i < m; i ++ )
{
for( int j = i+1; j <= m; j ++ )
if( vis[i][j] )
ans += vis[i][j]*(vis[i][j]-1)/2;
}
}
int main()
{
int T;
scanf ( "%d", &T );
while( T-- )
{
scanf ( "%d %d", &n, &m );
for( int i = 1; i <= n; i ++ )
{
for( int j = 1; j <= m; j ++ )
scanf ( " %c", &v[i][j] );
}
ans = 0;
slove( 'B' );
slove( 'J' );
slove( 'H' );
slove( 'Y' );
slove( 'N' );
printf("%d\n",ans);
}
return 0;
}