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.
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;}
- hdu 4331 Image Recognition(DP+线段树)2012 Multi-University Training Contest 4
- 2012 Multi-University Training Contest 4-1001 hdu4331 Image Recognition
- hdu 4336 Card Collector 2012 Multi-University Training Contest 4
- hdu 5791 2016 Multi-University Training Contest 5(dp)
- hdu 6170 dp 2017 Multi-University Training Contest
- 2012 Multi-University Training Contest 4
- hdu 4348 To the moon(线段树成段更新) 2012 Multi-University Training Contest 5
- HDU 3974 Assign the task 2011 Multi-University Training Contest 14 - Host by FZU 线段树
- 2014 Multi-University Training Contest 3 1007 && HDU 4893 Wow! Such Sequence ! (线段树)
- hdu 4967 Handling the Past 线段树 2014 Multi-University Training Contest 9-1008
- hdu 4973 A simple simulation problem 线段树 2014 Multi-University Training Contest 10-1003
- HDU 5316 Magician 2015 Multi-University Training Contest 3 1001 线段树
- hdu 5726 GCD(2016 Multi-University Training Contest 1线段树)
- HDU 5861 Road (线段树) 2016 Multi-University Training Contest 10
- hdu 5861Road(2016 Multi-University Training Contest 10——线段树+扫描线)
- hdu 6070 Dirt Ratio(二分+线段树)(2017 Multi-University Training Contest
- 2012 Multi-University Training Contest
- 【线段树】2015 Multi-University Training Contest 1 1002
- C#学习笔记
- Java定时任务ScheduledThreadPoolExecutor
- 判断一文件是不是字符设备文件
- c写了个通用链表带了例子。
- HDU 敌兵布阵 树状数组
- hdu 4331 Image Recognition(DP+线段树)2012 Multi-University Training Contest 4
- echo colorful text in shell.
- 价值观与审美
- XNA旋转:矩阵,四元数和欧拉角向量 .
- 同步和异步的区别
- bundle通信----类访问
- 30个创意出色的非常实用的jquery框架插件-(视觉大背景,瀑布流效果)
- 客户端链接RAC报错ORA-12545 的处理
- 【SQL Server学习笔记】索引调优、统计信息、查询调整、资源调控