Connected Cells in a Grid

  • + 0 comments

    My Swift solution:

    var maxConnectedCells: Int = 0
    var connectedCells: Int = 0
    var grid: [[Cell]] = []
    var visited: [Cell] = []
     
    class Cell {
        var filled: Bool = false
        var i: Int = 0
        var j: Int = 0
    }
    
    func connectedCell(matrix: [[Int]]) -> Int {
        createCells(matrix: matrix)
    
        for row in grid {
    
            connectedCells = 0
    
        innerLoop: for cell in row {
                if !cell.filled { break innerLoop }
                if cell.filled && !visited.contains(where: { $0.i == cell.i && $0.j == cell.j }) {
                    markCurrentCell(cell: cell)
                }
            }
        }
    
        return maxConnectedCells
    }
    
    private func markCurrentCell(cell: Cell) {
        visited.append(cell)
        connectedCells += 1
        let largest = max(maxConnectedCells, connectedCells)
        maxConnectedCells = largest
    
        exploreAround(i: cell.i, j: cell.j)
    }
    
    private func exploreAround(i: Int, j: Int) {
        let directions = [(-1, -1), (0, -1), (-1, 1), (1, 1), (0, 1), (-1, 0), (1, -1), (1, 0)]
    
        for direction in directions {
            let newI = i + direction.0
            let newJ = j + direction.1
    
            if (newI < 0) || (newI >= grid.count) {
                continue
            }
    
            if (newJ < 0) || (newJ >= grid[newI].count) {
                continue
            }
    
            let cell = grid[newI][newJ]
    
            guard cell.filled && !visited.contains(where: { $0.i == cell.i && $0.j == cell.j }) else {
                continue
            }
    
            visited.append(cell)
            connectedCells += 1
            let largest = max(maxConnectedCells, connectedCells)
            maxConnectedCells = largest
    
            exploreMore(cell: cell)
        }
    }
    
    private func exploreMore(cell: Cell) {
        let directions = [(-1, -1), (0, -1), (-1, 1), (1, 1), (0, 1), (-1, 0), (1, -1), (1, 0)]
    
        for direction in directions {
            let newI = cell.i + direction.0
            let newJ = cell.j + direction.1
    
            if (newI < 0) || (newI >= grid.count) {
                continue
            }
    
            if (newJ < 0) || (newJ >= grid[newI].count) {
                continue
            }
    
            let cell = grid[newI][newJ]
    
            guard cell.filled && !visited.contains(where: { $0.i == cell.i && $0.j == cell.j }) else {
                continue
            }
    
            visited.append(cell)
            connectedCells += 1
            let largest = max(maxConnectedCells, connectedCells)
            maxConnectedCells = largest
        
            exploreMore(cell: cell)
        }
    }
    
    private func createCells(matrix: [[Int]]) {
        var collumns: [[Cell]] = []
        for (i, _) in matrix.enumerated() {
            var row: [Cell] = []
            for (j, _) in matrix[i].enumerated() {
                let cell = Cell()
                cell.filled = matrix[i][j] == 1 ? true : false
                cell.i = i
                cell.j = j
                row.append(cell)
            }
            collumns.append(row)
        }
        grid = collumns
    }