Time Limit: 1000 MS Memory Limit: 131072 K
Description
Long time ago, a king occupied a vast territory.
Now there is a problem that he worried that he want to choose a largest square of his territory to build a palace.
Can you help him?
For simplicity, we use a matrix to represent the territory as follows:
0 0 0 0 0
0 1 0 1 0
1 1 1 1 0
0 1 1 0 0
0 0 1 0 0
Every single number in the matrix represents a piece of land, which is a 1*1 square
1 represents that this land has been occupied
0 represents not
Obviously the side-length of the largest square is 2
Input
The first line of the input contains a single integer t (1≤t≤5) — the number of cases.
For each case
The first line has two integers N and M representing the length and width of the matrix
Then M lines follows to describe the matrix
1≤N,M≤1000
Output
For each case output the the side-length of the largest square
Sample Input
2
5 5
0 0 0 0 0
0 1 0 1 0
1 1 0 1 0
0 1 1 0 0
0 0 1 0 0
5 5
0 0 0 0 0
0 1 0 1 0
1 1 1 1 0
0 1 1 0 0
0 0 1 0 0
Sample Output
1
2
思路:学长说,单调栈模板题。。。但是,也没他想的那么复杂。。。对于做出来的我来说,喜获水题一枚。
不废话了。步入正题。。。
按照我的思路,把图压缩在数字里(又自己造生词)。。举个例子。。。
样例二(上面的第二组样例):经过我的预处理会变成下面这副模样:
0 0 0 0 0
0 1 0 1 0
1 2 1 2 0
0 3 2 0 0
0 0 3 0 0
如此一来,是不是有了些许眉目。因为看样例,就会很快发现是(从一开始,个人习惯)(行数,列数)(3,2)(3,3)
(4,2)(4,3)构成了一个边长为二的正方形,然后通过这个观察我打的那个表,是不是发现了些许端倪。。。
就是map[4,2]>=2,map[4,3]>=2;然后看代码你就懂了。(数字化图形)。。。
代码:
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
typedef long long LL;
const int N=1000;
int mp[N+10][N+10];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m,x;
memset(mp,0,sizeof(mp));
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%d",&x);
if(x&&mp[i-1][j])
mp[i][j]=mp[i-1][j]+1;
else
mp[i][j]=x;
}
}
int maxx=0;
int k=1;
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
if(mp[i][j]>=k)
{
int p=0;
for(;mp[i][p+j]>=k; p++);
if(p>=k)
{
maxx=k;
k++;
}
}
}
}
printf("%d\n",maxx);
}
}