0

So let's say we have this matrix:

0 0 0 0 0 0 0 0 
0 1 1 1 0 0 0 0
0 1 1 1 0 0 0 0 
0 1 1 1 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 1 1 1 0
0 0 0 0 1 1 1 0

And we need those outputs:

Submatrix 1:

1 1 1
1 1 1
1 1 1

Submatrix 2:

1 1 1
1 1 1

The matrix size can vary, so can the submatrix sizes but they will have either square or rectangle shape. And is only 0 and 1.

How can i do that? Is there any algorithm to do that?

for (i=0; i<m;i++){
    for (j=2; j<n; j++){
        if ( A[i+1,j]==1 && A[i,j+1]==1 || A[i+1,j]==1 && A[i,j-1]==1 || A[i-1,j]==1 && A[i,j+1]==1 || A[i-1,j]==1 && A[i,j-1]==1 || A[i-1,j]==1 && A[i+1,j]==1 && A[i,j-1]==1 && A[i,j+1]==1 )
           B[i,j]=A[i,j];


        }
}

This is the first ideea that comes to my mind, but i don't know how to stop after i'm done with the first block of ones, and in the end the matrix B is the same as matrix A. Also i don't know how to set the submatrix size to be exactly the size that it needs to be. For now i'm only trying to break the big matrix in 2. Should i allocate the submatrix dynamically?

  • What is the maximum and minimum allowed sub matrix size? Is size=(1,1) allowed? Is the input only 0 and 1? Is the matrix always quadratic? – RoQuOTriX Nov 25 '19 at 09:46
  • @RoQuOTriX You should read more carefully, these information are already provided. – Fareanor Nov 25 '19 at 09:48
  • The sentence "And is only 0 and 1" has no semantic content. What is only 0 and 1? You refer to sizes of sub matrices, but dont answer my question if a 1-element matrix is allowed, too. And every matrix has the form of a square or a rectangle, so this information doesn't help at all – RoQuOTriX Nov 25 '19 at 09:51
  • @RoQuOTriX I'll translate for you: The matrix size can be anything, same thing for the submatrices sizes. The matrices shapes can be quadratic or rectangular. The whole matrix contains only `0` (for empty spaces) and `1` (for submatrices to catch). – Fareanor Nov 25 '19 at 09:54
  • 0 and 1 are the values allowed in the matrix as you can see in the example. And yes, the matrix size can vary ( bigger or smaller ) meaning it can go up to something like 2000x2000 or 10x10, even 1x1 if you want but 1x1 is nonsense because the submatrix is the matrix itself, right? – myname123321 Nov 25 '19 at 09:55
  • @Fareanor could you please provide an example for a non-quadratic/non-rectangular matrix-shape? And what is an "empty space" in a matrix? It is not about not understanding the question at all.I understand what he wants, but the wording is bad – RoQuOTriX Nov 25 '19 at 09:56
  • @myname123321 He was asking _1x1_ for submatrix size not for the upper matrix :) – Fareanor Nov 25 '19 at 09:56
  • @RoQuOTriX What a bad faith, we know that the english is bad, but it is still easy to understand. If you are unpleased, why don't you propose an edit ? Or at least go on your way. No need to be that aggressive, it does not help anyone :) – Fareanor Nov 25 '19 at 09:59
  • @RoQuOTriX I'm sorry, english is not my first language, but the examples should be pretty straight forward. An algorithm should work for any case not just for the example that i provided. I tried to work on that problem but my knowledge is limited. I'm not that good when it comes to algorithms. – myname123321 Nov 25 '19 at 10:55
  • What have you tried? What are the problems you are facing? – mfnx Nov 25 '19 at 11:02
  • 1
    And also, just for fun: how would you treat this (more than 1 solution)? 1 0 0 \newline 1 1 1 \newline 1 1 1 \newline – mfnx Nov 25 '19 at 11:02
  • @MFnx I tried to take the matrix element by element and see if it has neighbours which are aswell 1 but this just duplicates my matrix. I can't find the solution to stop after let's say the first submatrix. Your example can be done with an array of arrays because you can't have a matrix that has 1 element on first row, and 3 on the next two rows ... right? – myname123321 Nov 25 '19 at 11:26
  • 2
    But, regardless of C++ and coding... just with a paper and a pencil. What would the solution of 1 0 0 \newline 1 1 1 \newline 1 1 1 \newline be? – mfnx Nov 25 '19 at 11:29
  • @MFnx Check neighbours of current element. If one of them is 1 then our current element is a part of our block of ones, right? – myname123321 Nov 25 '19 at 11:32
  • Neighbour to the right? Neighbour downwards? Left, upwards? Let's say you mean to the right. Then you would have 1 submatrix of 1, and another submatrices of 2 times 3 ones. Ok, well, give it a try! If you encounter problems, ask a good question here. https://stackoverflow.com/help/how-to-ask – mfnx Nov 25 '19 at 11:38
  • @MFnx should i ask a new question providing my code or should i edit this one? – myname123321 Nov 25 '19 at 11:44
  • As you please. Editing is just fine imo. – mfnx Nov 25 '19 at 12:02
  • Just for my clarifying: The 2nd sub-matrix (3 x 2) can be found in the exposed matrix sample 3 times? – Scheff's Cat Nov 25 '19 at 12:05
  • @myname123321 The question of MFnx is very important. If you have 1 0 0 \newline 1 1 1 \newline 1 1 1, will you consider you have a (1x1) matrix with a (2x3) matrix below ? Or will you consider you have a (3x1) matrix to the left and a (2x2) matrix ? More clearly, can a submatrix be right next to another one ? – Fareanor Nov 25 '19 at 12:55
  • Sorry for being late. I forgot to mention that a case like 1 0 0 \newline 1 1 1 \newline 1 1 1 should be ignored, because inputs like this would not be tested. – myname123321 Nov 29 '19 at 06:47

0 Answers0