hdu 4331 Image Recognition(DP+线段树)2012 Multi-University Training Contest 4

来源:互联网 发布:查看系统版本 linux 编辑:程序博客网 时间:2024/06/01 12:06

Image Recognition

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 377    Accepted Submission(s): 137


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.
 

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
131 1 01 1 00 0 0
 

Sample Output
Case 1: 5
 

Source
2012 Multi-University Training Contest 4
 

Recommend
zhoujiaqi2010
 

题目:http://acm.hdu.edu.cn/showproblem.php?pid=4331

题意:给你一个n*n的0 1 矩阵,要求满足边上都是1的正方形矩阵的个数

分析:先DP求出每个点能到达的四个方向的最长距离,然后就是枚举每一条对角线进行统计,统计的时候必须用线段树什么的,然后比赛的时候一直都想不出怎样来统计,想了个错误的做法,看了题解才想明白,估计是太久没写线段树了(貌似暴力统计就过了,肿么能这样呢= =)

代码:

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;const int mm=1111;struct data{    int p,x;}g[mm*2];int a[mm][mm],s[mm][mm],t[mm][mm],r[mm][mm],c[mm][mm];int sum[mm<<2];void build(int n){    memset(sum,0,sizeof(int)*(n<<2));}void update(int x,int l,int r,int rt){    ++sum[rt];    if(l==r)return;    int m=(l+r)>>1;    if(x<=m)update(x,lson);    else update(x,rson);}int query(int x,int l,int r,int rt){    if(l==r)return sum[rt];    int m=(l+r)>>1,ret=0;    if(x<=m)ret+=query(x,lson)+sum[rt<<1|1];    else if(x>m)ret+=query(x,rson);    return ret;}bool cmp(data a,data b){    return a.p<b.p||(a.p==b.p&&a.x<b.x);}int solve(int i0,int j0,int n){    int i,j,e=0,k,m=0,ret=0;    for(k=1,i=i0,j=j0;k<=n;++k,++i,++j)        if(a[i][j])        {            g[m].p=k-t[i][j],g[m++].x=-k;            g[m].p=k,g[m++].x=k;        }    sort(g,g+m,cmp);    build(n);    for(k=1,i=i0,j=j0;k<=n;++k,++i,++j)        {            if(a[i][j])update(k+s[i][j]-1,1,n,1);            while(e<m&&g[e].p<=k)            {                if(g[e].p==k)                {                    if(g[e].x<0)ret-=query(-g[e].x,1,n,1);                    else ret+=query(g[e].x,1,n,1);                }                ++e;            }        }    return ret;}int main(){    freopen("a.in","r",stdin);    freopen("a.out","w",stdout);    int i,j,k,n,T,ans,cs=0;    scanf("%d",&T);    while(T--)    {        scanf("%d",&n);        for(i=1;i<=n;++i)            for(j=1;j<=n;++j)                scanf("%d",&a[i][j]);        memset(r,0,sizeof(r));        memset(c,0,sizeof(c));        for(i=1;i<=n;++i)            for(j=1;j<=n;++j)            if(a[i][j])            {                r[i][j]=r[i-1][j]+1;                c[i][j]=c[i][j-1]+1;                t[i][j]=min(r[i][j],c[i][j]);            }        memset(r,0,sizeof(r));        memset(c,0,sizeof(c));        for(i=n;i>0;--i)            for(j=n;j>0;--j)            if(a[i][j])            {                r[i][j]=r[i+1][j]+1;                c[i][j]=c[i][j+1]+1;                s[i][j]=min(r[i][j],c[i][j]);            }        ans=0;        for(i=n;i>1;--i)            ans+=solve(i,1,n-i+1);        for(j=1;j<=n;++j)            ans+=solve(1,j,n-j+1);        printf("Case %d: %d\n",++cs,ans);    }    return 0;}