Count cells in a grid from which maximum number of cells can be reached by K vertical or horizontal jumps | Function to count the number of cells in the grid such that maximum cell is reachable with a jump of K ; Maximum reachable rows from the current row ; Stores the count of cell that are reachable from the current row ; Count of reachable rows ; Update the maximum value ; Add it to the count ; Maximum reachable columns from the current column ; Stores the count of cell that are reachable from the current column ; Count of rechable columns ; Update the maximum value ; Add it to the count ; Return the total count of cells ; Driver Code
def countCells ( n , m , s ) : NEW_LINE INDENT mx1 = - 1 NEW_LINE cont1 = 0 NEW_LINE i = 0 NEW_LINE while ( i < s and i < n ) : NEW_LINE INDENT aux = ( n - ( i + 1 ) ) // s + 1 NEW_LINE if ( aux > mx1 ) : NEW_LINE INDENT mx1 = cont1 = aux NEW_LINE DEDENT elif ( aux == mx1 ) : NEW_LINE INDENT cont1 += aux NEW_LINE DEDENT i += 1 NEW_LINE DEDENT mx2 = - 1 NEW_LINE cont2 = 0 NEW_LINE i = 0 NEW_LINE while ( i < s and i < m ) : NEW_LINE INDENT aux = ( m - ( i + 1 ) ) // s + 1 NEW_LINE if ( aux > mx2 ) : NEW_LINE INDENT mx2 = cont2 = aux NEW_LINE DEDENT elif ( aux == mx2 ) : NEW_LINE INDENT cont2 += aux NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return cont1 * cont2 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE M = 5 NEW_LINE K = 2 NEW_LINE print ( countCells ( N , M , K ) ) NEW_LINE DEDENT