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 2Input
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≤1000Output
For each case output the the side-length of the largest squareSample 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 0Sample Output
1 2
想到了DP,却没看出规律,dp确实是弱项,以后刷题要有倾斜
判断此点左边,左上,上是否都为1,如果为1,则此点等于其中最小值+1
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<string>
#include<algorithm>
#include<stdlib.h>
#include<queue>
#include<map>
#include<vector>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define LL long long int
using namespace std;
int dp[1005][1005];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m,ans=0;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
scanf("%d",&dp[i][j]);
}
}
for(int i=1;i<n;i++)
{
for(int j=1;j<m;j++)
{
if(dp[i][j])
dp[i][j]=min(dp[i-1][j],min(dp[i-1][j-1],dp[i][j-1]))+1;
ans=max(ans,dp[i][j]);
}
}
printf("%d\n",ans);
}
}