Problem of 8 Neighbours of an element in a 2 | Dimension of Array ; Count of 1 s ; Counting all neighbouring 1 s ; Comparing the number of neighbouring 1 s with given ranges ; Copying changes to the main matrix ; Driver code ; Function call to calculate the resultant matrix after ' K ' iterations . ; Printing Result
N = 4 NEW_LINE def predictMatrix ( arr , range1a , range1b , range0a , range0b , K , b ) : NEW_LINE INDENT c = 0 NEW_LINE while ( K ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT c = 0 NEW_LINE if ( i > 0 and arr [ i - 1 ] [ j ] == 1 ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT if ( j > 0 and arr [ i ] [ j - 1 ] == 1 ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT if ( i > 0 and j > 0 and arr [ i - 1 ] [ j - 1 ] == 1 ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT if ( i < N - 1 and arr [ i + 1 ] [ j ] == 1 ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT if ( j < N - 1 and arr [ i ] [ j + 1 ] == 1 ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT if ( i < N - 1 and j < N - 1 and arr [ i + 1 ] [ j + 1 ] == 1 ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT if ( i < N - 1 and j > 0 and arr [ i + 1 ] [ j - 1 ] == 1 ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT if ( i > 0 and j < N - 1 and arr [ i - 1 ] [ j + 1 ] == 1 ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT if ( arr [ i ] [ j ] == 1 ) : NEW_LINE INDENT if ( c >= range1a and c <= range1b ) : NEW_LINE INDENT b [ i ] [ j ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT b [ i ] [ j ] = 0 NEW_LINE DEDENT DEDENT if ( arr [ i ] [ j ] == 0 ) : NEW_LINE INDENT if ( c >= range0a and c <= range0b ) : NEW_LINE INDENT b [ i ] [ j ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT b [ i ] [ j ] = 0 NEW_LINE DEDENT DEDENT DEDENT DEDENT K -= 1 NEW_LINE for k in range ( N ) : NEW_LINE INDENT for m in range ( N ) : NEW_LINE INDENT arr [ k ] [ m ] = b [ k ] [ m ] NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ [ 0 , 0 , 0 , 0 ] , [ 0 , 1 , 1 , 0 ] , [ 0 , 0 , 1 , 0 ] , [ 0 , 1 , 0 , 1 ] ] NEW_LINE range1a = 2 NEW_LINE range1b = 2 NEW_LINE range0a = 2 NEW_LINE range0b = 3 NEW_LINE K = 3 NEW_LINE b = [ [ 0 for x in range ( N ) ] for y in range ( N ) ] NEW_LINE predictMatrix ( arr , range1a , range1b , range0a , range0b , K , b ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT print ( ) NEW_LINE for j in range ( N ) : NEW_LINE INDENT print ( b [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT