Advertisement
a53

LSQ

a53
Jan 3rd, 2019
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define NMAX 1000
  3. using namespace std;
  4. ifstream fin("lsq.in");
  5. ofstream fout("lsq.out");
  6. int n,m,mat[NMAX][NMAX];
  7.  
  8. int getMin(int x,int y)
  9. { return (x<y)?x:y; }
  10.  
  11. int findSubSquare(int mat[NMAX][NMAX],int N,int M)
  12. {
  13. int maxv=0;
  14. int hor[N][M],ver[N][M];
  15. hor[0][0]=ver[0][0]=(mat[0][0] == 1);
  16. for(int i=0;i<N;++i)
  17. {
  18. for(int j=0;j<M;++j)
  19. {
  20. if(mat[i][j]==0)
  21. ver[i][j]=hor[i][j]=0;
  22. else
  23. {
  24. hor[i][j]=(j==0)?1:hor[i][j-1]+1;
  25. ver[i][j]=(i==0)?1:ver[i-1][j]+1;
  26. }
  27. }
  28. }
  29. for(int i=N-1;i>=1;--i)
  30. {
  31. for(int j=M-1;j>=1;--j)
  32. {
  33. int small=getMin(hor[i][j],ver[i][j]);
  34. while (small>maxv)
  35. {
  36. if (ver[i][j-small+1]>=small&&
  37. hor[i-small+1][j]>=small)
  38. {
  39. maxv=small;
  40. }
  41. --small;
  42. }
  43. }
  44. }
  45. return maxv;
  46. }
  47.  
  48. int main()
  49. {
  50. int i,j;
  51. char ch;
  52. fin>>n>>m;
  53. fin.get();
  54. for(i=0; i<n;++i)
  55. for(j=0;j<m;++j)
  56. {
  57. fin>>ch;
  58. if(ch!='0')
  59. mat[i][j]=1;
  60. }
  61. fout<<findSubSquare(mat,n,m);
  62. return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement