Hdu4331 Image Recognition

Source: Internet
Author: User
Image Recognition

Time Limit: 6000/3000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 964 accepted submission (s): 371


Problem descriptionnow there is an image recognition problem for you. now you are given an image which is a n * n matrix and there are only 0 s and 1 s in the matrix. and we are interested in the squares in whose four edges there is no 0 s. so it's your task to find how many such
Squares in the image.


Inputthe 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.


Outputfor 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 the image.


Sample Input

131 1 01 1 00 0 0
 


Sample output

Case 1: 5
 


Source2012 multi-university training contest 4


Recommendzhoujiaqi2010 should be a line segment tree. I didn't expect it to be violent.

#include <iostream>#include <stdio.h>#include <string.h>using namespace std;#define MAXN 1005int up[MAXN][MAXN];int lleft[MAXN][MAXN];int map[MAXN][MAXN];int fmin(int a,int b){    if(a<b)    return a;    return b;}int main(){    int n,tcase,i,j,t=1;    int ans;    scanf("%d",&tcase);    while(tcase--)    {        scanf("%d",&n);        ans=0;        memset(lleft,0,sizeof(lleft));        memset(up,0,sizeof(up));        for(i=1;i<=n;i++)            for(j=1;j<=n;j++)            {                scanf("%d",&map[i][j]);                if(map[i][j])                {                    ans++;                    lleft[i][j]=lleft[i][j-1]+1;                    up[i][j]=up[i-1][j]+1;                }                else                {                    lleft[i][j]=0;                    up[i][j]=0;                }            }        for(i=1;i<=n;i++)        {            for(j=1;j<=n;j++)            {                              int k=fmin(up[i][j],lleft[i][j]);                if(k<=1)                continue;                int temp;                for(temp=2;temp<=k;temp++)                {                    if(up[i][j-temp+1]>=temp&&lleft[i-temp+1][j]>=temp)                    {                        ans++;                                       }                }            }                  }        printf("Case %d: %d\n",t++,ans);    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.