编程介的小学生 2017-11-17 16:26 采纳率: 20.5%
浏览 698
已结题

Image Recognition

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
1
3
1 1 0
1 1 0
0 0 0

Sample Output
Case 1: 5

  • 写回答

1条回答 默认 最新

  • 银河曼巴 2018-07-23 14:00
    关注

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

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

    代码:

    #include
    #include
    #include
    #include
    #define lson l,m,rt< #define rson m+1,r,rt using 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 void build(int n)
    {
    memset(sum,0,sizeof(int)*(n }
    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< else if(x>m)ret+=query(x,rson);
    return ret;
    }
    bool cmp(data a,data b)
    {
    return a.p }
    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 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 {
    if(a[i][j])update(k+s[i][j]-1,1,n,1);
    while(e {
    if(g[e].p==k)
    {
    if(g[e].x 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 for(j=1;j scanf("%d",&a[i][j]);
    memset(r,0,sizeof(r));
    memset(c,0,sizeof(c));
    for(i=1;i for(j=1;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;
    }

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?