There is an array of zeros and ones. It is necessary to allocate the submatrix, in the form of a square matrix of units . The algorithm of this selection is needed.

Added.

int i, j, n, m; int a[50][50], B[50][50]; srand (GetTickCount ()); puts ("\n Input n,m:"); scanf ("%d %d", &n, &m); printf ("\n Array a \n"); /*sozdanie mtarici iz 1 and 0*/ for (i = 0; i < n; i++) for (j = 0; j < m; j++) { a[i][j] = rand () % 2; printf ("%5.2d%c", a[i][j], (j == m - 1) ? '\n' : ' '); } 
  • Is it possible to find the maximum chain of units of length k ^ 2? Or the initial array is the matrix (then the units in the vector representation will go at intervals) - alexlz
  • two-dimensional array. so the chain as an option will not work. the source array is of course a matrix. and there should be no gaps between units. Put in submatrices (by definition, there are no gaps) 0 1 1 0 1 0 1 1 0 1 let there be such a matrix means square submatrix 0 0 0 0 0 is 1 1 1 1 - Sheldon20
  • The picture does not load. - Sheldon20

1 answer 1

The task is still insufficiently specified. Here is a solution that prints the coordinates of the desired matrices larger than 2x2 (including nested solutions — there will be three 2x2 matrices in the 3x3 matrix).

 #include <iostream> #include <windows.h> using namespace std; int adim (int a[50][50], int n, int m, int i, int j) { int t, c; c = 1; while (1) { // проверяем следующую границу if (i+c == n || j+c == m) goto ex; for(t = i; t <= i + c; t++) if(a[t][j+c] == 0) goto ex; for(t = j; t < j+c; t++) if(a[i+c][t] == 0) goto ex; c++; } ex: return c; } int main() { int i,j,n,m,c; int a[50][50]; srand(GetTickCount()); cerr << "Input n,m:" << endl; cin >> n >> m; cout << "Array a" << endl; /* sozdanie mtarici iz 1 and 0*/ for(i=0;i<n;i++) { for(j=0;j<m;j++) { cout << (a[i][j]=rand()%2); if (j+1 < m) cout << " "; } cout << endl; } for(i=0; i<n; i++) for(j=0; j<m; j++) if (a[i][j] && (c = adim(a, n, m, i, j)) > 1) cout << i << ' ' << j << ' ' << c << endl; return 0; } 

Well, goto aesthetes can be replaced with exceptions.

UPD c - size, height / width. In the adim function, this is a loop counter. The cells are checked (i + c, j) ... (i + c, j + c) and (i, j + c) ... (i + c, j + c) - the right and lower edges (from 1 is smaller, for a 2x2 matrix c == 1). With a successful test, c is incremented.

  • Thank you very much. I understand after the matrix, the main one displays the matrix in 3 columns. the first is i the second j and the third c (judging by the algorithm) is the coordinates of the upper left corner of the square matrix. but what is C in this case? - Sheldon20 1:51 pm
  • got it thanks again - Sheldon20
  • "aesthetes" will use goto -> return c; - IAZ
  • No, return c; - not for "aesthetes". But I do it right :) - alexlz 1:58 pm