Image Recognition
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 935 Accepted Submission(s): 356
Problem Description
Now there is an image recognition problem for you. Now you are given an image which is a N * N matrix and there are only 0s and 1s in the matrix. And we are interested in the squares in whose four edges there is no 0s. So it’s your task to find how many such squares in the image.
Input
The first line of the input contains an integer T (1<=T<=10) which means the number of test cases.
For each test cases, the first line is one integer N (1<=N<=1000) which is the size of the image. Then there are N lines and each line has N integers each of which is either 0 or 1.
For each test cases, the first line is one integer N (1<=N<=1000) which is the size of the image. Then there are N lines and each line has N integers each of which is either 0 or 1.
Output
For each test case, please output a line which is "Case X: Y", X means the number of the test case and Y means the number of the squares we are interested in in the image.
Sample Input
1 3 1 1 0 1 1 0 0 0 0
Sample Output
Case 1: 5
Source
#include<stdio.h>
#include<string.h>
#define MAXN 1005
int map[MAXN][MAXN],left[MAXN][MAXN],right[MAXN][MAXN],up[MAXN][MAXN],down[MAXN][MAXN],lu[MAXN][MAXN],rd[MAXN][MAXN];
int min(int a,int b)
{
return a<b?a:b;
}
int main()
{
int i,j,k,n,t,cas=1,ans;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
ans=0;
//memset(left,0,sizeof(left));
//memset(right,0,sizeof(right));
//memset(up,0,sizeof(up));
//memset(down,0,sizeof(down));
for(i=0;i<n+2;i++)
for(j=0;j<n+2;j++)
left[i][j]=0;
for(i=0;i<n+2;i++)
for(j=0;j<n+2;j++)
right[i][j]=0;
for(i=0;i<n+2;i++)
for(j=0;j<n+2;j++)
up[i][j]=0;
for(i=0;i<n+2;i++)
for(j=0;j<n+2;j++)
down[i][j]=0;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&map[i][j]);
if(map[i][j])
{
ans++;
left[i][j]=left[i][j-1]+1;
up[i][j]=up[i-1][j]+1;
}
lu[i][j]=min(left[i][j],up[i][j]);
}
for(i=n;i>=1;i--)
for(j=n;j>=1;j--)
{
if(map[i][j])
{
right[i][j]=right[i][j+1]+1;
down[i][j]=down[i+1][j]+1;
}
rd[i][j]=min(right[i][j],down[i][j]);
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(map[i][j])//少了这个剪枝可不行
{
for(k=2;k<=rd[i][j];k++)//边长为一的我们输入时已经统计过了
{
if(lu[i+k-1][j+k-1]>=k)
ans++;
}
}
}
}
printf("Case %d: %d\n",cas++,ans);
}
return 0;
}
优化过的
#include<stdio.h>
#include<string.h>
#define MAXN 1005
int map[MAXN][MAXN],left[MAXN][MAXN],right[MAXN][MAXN],up[MAXN][MAXN],down[MAXN][MAXN],lu[MAXN][MAXN],rd[MAXN][MAXN];
int min(int a,int b)
{
return a<b?a:b;
}
int main()
{
int i,j,k,n,t,cas=1,ans,h,c;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
ans=0;
c=0;
h=0;
memset(left,0,sizeof(left));
memset(right,0,sizeof(right));
memset(up,0,sizeof(up));
memset(down,0,sizeof(down));
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&map[i][j]);
if(map[i][j])
{
ans++;
left[i][j]=left[i][j-1]+1;
up[i][j]=up[i-1][j]+1;
}
lu[i][j]=min(left[i][j],up[i][j]);
}
for(i=n;i>=1;i--)
for(j=n;j>=1;j--)
{
if(map[i][j])
{
right[i][j]=right[i][j+1]+1;
down[i][j]=down[i+1][j]+1;
}
rd[i][j]=min(right[i][j],down[i][j]);
if(rd[i][j]>1)//我把那些能组成大于等于2的正方形的点都存了下来
{
left[h][c]=i;//没办法啊,题目内存控制严
up[h][c++]=j;
if(c==1000)
{
c=0;
h++;
}
}
}
/*for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(map[i][j])//少了这个剪枝可不行
{
for(k=2;k<=rd[i][j];k++)//边长为一的我们输入时已经统计过了
{
if(lu[i+k-1][j+k-1]>=k)
ans++;
}
}
}
}*/
for(i=0;i<h;i++)
for(j=0;j<1000;j++)
{
for(k=2;k<=rd[left[i][j]][up[i][j]];k++)//边长为一的我们输入时已经统计过了
{
if(lu[left[i][j]+k-1][up[i][j]+k-1]>=k)
ans++;
}
}
for(j=0;j<c;j++)
{
for(k=2;k<=rd[left[i][j]][up[i][j]];k++)//边长为一的我们输入时已经统计过了
{
if(lu[left[i][j]+k-1][up[i][j]+k-1]>=k)
ans++;
}
}
printf("Case %d: %d\n",cas++,ans);
}
return 0;
}