您的位置:首页 > 其它

南邮 OJ 1613 Counting square

2015-08-06 10:03 309 查看


Counting square

时间限制(普通/Java) : 3000 MS/ 9000 MS 运行内存限制 : 65536 KByte

总提交 : 16 测试通过 : 11

比赛描述

There is a matrix of size R rows by C columns. Each element in the matrix is either ‘0’ or ‘1’. A square is called magic square if it meets the following three conditions.
1.The elements on the four borders are all ‘1’.
2.Inside the square (excluding the elements on the borders), the number of 1's and the number of 0's are different at most by 1.
3.The size of the square is at least 2 by 2. Now given the matrix, please tell me how many magic square are there in the matrix.

输入

The input begins with a line containing an integer T , the number of test cases. Each case begins with two integers R , C (1 ≤ R, C ≤ 300) , representing
the size of the matrix. Then R lines follow. Each contains C integers, either ‘0’ or ‘1’. The integers are separated by a single space.

输出

For each case, output the number of magic square in a single line.

样例输入

3

4 4

1 1 1 1

1 0 1 1

1 1 0 1

1 1 1 1

5 5

1 0 1 1 1

1 0 1 0 1

1 1 0 1 1

1 0 0 1 1

1 1 1 1 1

2 2

1 1

1 1

样例输出

3

2

1

题目来源

ICPC

/* AC 1625MS
#include<iostream>

#define N 301
int a

;
int s

;
// 矩形(a,b)-(c,d)之间的1的个数,矩形左上角的坐标为(a,b),右下角坐标为(c,d)。
int oneNum(int a,int b,int c,int d){
a--;
b--;
return s[c][d]-s[a][d]-s[c][b]+s[a][b];
}

int main(){
//	freopen("test.txt","r",stdin);
int T,R,C,i,j,k,count,temp,area;
scanf("%d",&T);
while(T--){
scanf("%d%d",&R,&C);
for(i=1;i<=R;i++){
for(j=1;j<=C;j++){
scanf("%d",&a[i][j]);
s[i][j] = s[i-1][j]+s[i][j-1]-s[i-1][j-1]+a[i][j];
}
}
count = 0;
for(i=1;i+1<=R;i++){
for(j=1;j+1<=C;j++){
for(k=1;i+k<=R && j+k<=C; k++){
if( oneNum(i,j,i,j+k)==k+1 &&
oneNum(i,j,i+k,j)==k+1 &&
oneNum(i+k,j,i+k,j+k)==k+1 &&
oneNum(i,j+k,i+k,j+k)==k+1){
if(k==1 || k==2){
count++;
}else{
temp = oneNum(i+1,j+1,i+k-1,j+k-1);
area = (k-1)*(k-1);
if(temp==area-temp || temp+1==area-temp || temp==area-temp+1){
count++;
}
}
}
}
}
}
printf("%d\n",count);
}
}
*/

#include<iostream>

#define N 301
int a

;
int s

;
// 矩形(a,b)-(c,d)之间的1的个数,矩形左上角的坐标为(a,b),右下角坐标为(c,d)。
int oneNum(int a,int b,int c,int d){
a--;
b--;
return s[c][d]-s[a][d]-s[c][b]+s[a][b];
}

int main(){
//	freopen("test.txt","r",stdin);
int T,R,C,i,j,k,count,temp,area;
scanf("%d",&T);
while(T--){
scanf("%d%d",&R,&C);
for(i=1;i<=R;i++){
for(j=1;j<=C;j++){
scanf("%d",&a[i][j]);
s[i][j] = s[i-1][j]+s[i][j-1]-s[i-1][j-1]+a[i][j];
}
}
count = 0;
for(i=1;i+1<=R;i++){
for(j=1;j+1<=C;j++){
for(k=1;i+k<=R && j+k<=C; k++){
if( oneNum(i,j,i,j+k)==k+1 &&
oneNum(i,j,i+k,j)==k+1 &&
oneNum(i+k,j,i+k,j+k)==k+1 &&
oneNum(i,j+k,i+k,j+k)==k+1){
if(k==1 || k==2){
count++;
}else{
temp = oneNum(i+1,j+1,i+k-1,j+k-1);
area = (k-1)*(k-1);
if(temp==area-temp || temp+1==area-temp || temp==area-temp+1){
count++;
}
}
}
}
}
}
printf("%d\n",count);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: