Advertisement
a53

cntSQ_80puncte

a53
Jan 6th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. #include <fstream>
  2. #define NMAX 1000
  3. using namespace std;
  4. ifstream fin("cntsq.in");
  5. ofstream fout("cntsq.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 TOTAL=0,NR;
  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>=0;--i)
  30. {
  31. for(int j=M-1;j>=0;--j)
  32. {
  33. int small=getMin(hor[i][j],ver[i][j]);
  34. NR=0;
  35. while(small>0)
  36. {
  37. if(ver[i][j-small+1]>=small&&
  38. hor[i-small+1][j]>=small)
  39. {
  40. ++NR;
  41. }
  42. --small;
  43. }
  44. TOTAL+=NR;
  45. }
  46. }
  47. return TOTAL;
  48. }
  49.  
  50. int main()
  51. {
  52. int i,j;
  53. char ch;
  54. fin>>n>>m;
  55. fin.get();
  56. for(i=0; i<n;++i)
  57. for(j=0;j<m;++j)
  58. {
  59. fin>>ch;
  60. if(ch!='0')
  61. mat[i][j]=1;
  62. }
  63. fout<<findSubSquare(mat,n,m);
  64. return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement