The Bomberman Game

Sort by

recency

|

90 Discussions

|

  • + 1 comment

    As mentioned by others, this was a PITA question. I finally got it after carefully running it step-by-step in my own IDE. This took many days, largely due to my dissatisifaction with leaving something undone.

    Sure, interviewers might ask this question, but only if they have decided they expect to fail you before even starting.

  • + 0 comments

    c++

    void tryExplode(std::vector<string>& src, int i, int j){
        size_t rows = src.size();
        size_t columns = src[0].size();
        
        
        if(i < 0 || j < 0 || i >= rows || j >= columns){
            return;
        }
        
        src[i][j] = '.';
    
    
    }
    
    vector<string> makeFullGrid(size_t rows, size_t columns){
        std::string fullRow;
        fullRow.insert(0, columns, 'O');
        
        return {rows, fullRow};
        
    }
    
    std::vector<string> detonate(std::vector<string> const& src){
        size_t rows = src.size();
        size_t columns = src[0].size();
        
        auto dst = makeFullGrid(rows, columns);
        
        for(int i = 0; i < rows; ++i){
            for(int j = 0; j < columns; ++j){
                if(src[i][j] == 'O'){
                    tryExplode(dst, i + 1, j);
                    tryExplode(dst, i - 1, j);
                    tryExplode(dst, i, j + 1);
                    tryExplode(dst, i, j - 1);
                    tryExplode(dst, i, j);
                }
            }
        } 
        return dst;
    }
    
    
    
    vector<string> bomberMan(int n, vector<string> grid) {
    
        size_t rows = grid.size();
        size_t columns = grid[0].size();
        
        if(n <= 1){
            return grid;
        }
        
        if(n%2==0){
            return makeFullGrid(rows, columns);
        }
        
        if(n%4==3){
            return detonate(grid);
         }
         
        if(n%4==1){
            auto result = detonate(grid);
            return detonate(result);
        }
    
        throw std::runtime_error("could not solve");
    }
    
  • + 0 comments

    What the hell. Is this really representative of problems asked in interviews? It would take half the interveiw to explain the stupid rules

  • + 0 comments

    Flowing is my java 8 solution

      public static List<String> bomberMan(int n, List<String> grid) {
        // Write your code here
            List<String> holeGrid = new ArrayList<String>();
            int row = grid.size();
            int col = grid.get(0).length();
            char [][] newGrid = new char[row][col];
            
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < col; j++) {
                    newGrid[i][j] = 'O';
                }
            }
            for(char[] line: newGrid){
                String newLine = new String(line);
                holeGrid.add(newLine);
            }
            
            List<String> grid1 = nextState(grid);
            List<String> grid2 = nextState(grid1);
            List<String> grid3 = nextState(grid2);
            
            if(n == 1){
                return grid;
            }
            if(n == 3){
                return grid1;
            }
            if(n%2 == 0){
                return holeGrid;
            }
            
            int flipTime = (n-1)/2;
            if(flipTime%2 == 1){
                return grid3;
            }
            
            return grid2;
            
        }
        
        public static List<String> nextState(List<String> grid){
            List<String> nextGrid = new ArrayList<String>();
            int row = grid.size();
            int col = grid.get(0).length();
            char [][] newGrid = new char[row][col];
            int[][] directions = {{0, 0},{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
            
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < col; j++) {
                    newGrid[i][j] = 'O';
                }
            }
            
            for(int i=0; i<grid.size(); i++){
                for(int j=0; j<grid.get(0).length(); j++){
                    if(grid.get(i).charAt(j) == 'O'){
                        for(int[] direction: directions){
                            int j_new = j + direction[0];
                            int i_new = i + direction[1];
                            if(j_new<col && j_new>=0 && i_new<row && i_new>=0){
                                newGrid[i_new][j_new] = '.';
                            }
                        }
                    }
                }
            }
            for(char[] line: newGrid){
                String newLine = new String(line);
                nextGrid.add(newLine);
            }
            return nextGrid;
        }
    
  • + 0 comments
    def bomberMan(n, g):
        x = lambda g: g if n < 2 else ['O' * len(g[0]) for _ in g] if n % 2 == 0 else [''.join(['.' if g[y][x] == 'O' or (g[y][x] == '.' and 'O' in (g[max(y-1, 0)][x], g[y][max(x-1, 0)], g[y][min(x+1, len(g[0])-1)], g[min(y+1, len(g)-1)][x])) else '.' if g[y][x] == 'O' else 'O' for x in range(len(g[0]))]) for y in range(len(g))]
        return x(g) if n % 4 == 3 else x(x(g))