Beispiel #1
0
  public void move() {
    int newRow = nextRow(row, direction);
    int newColumn = nextColumn(column, direction);
    if (maze.isValid(newRow, newColumn)) {
      maze.updateRobot(row, column, -1);

      row = newRow;
      column = newColumn;
      maze.updateRobot(row, column, +1);

      visited.add(getPosition());
      if (debug) {
        maze.print();
      }
    }
  }
Beispiel #2
0
  void bfs() {
    Item first = new Item(startRow, startCol, -1);
    queue.put(first);

    while (!queue.isEmpty()) {
      Item current = queue.get();
      if (current.row == goalRow && current.col == goalCol) {
        for (int i = 0; i < height; i++) {
          for (int j = 0; j < width; j++) {
            if (maze[i][j] == 'O') {
              maze[i][j] = ' ';
            }
          }
        }
        while (current.parent != -1) {
          maze[current.row][current.col] = 'O';
          current = queue.get(current.parent);
        }
        print();
      } else {
        if (maze[current.row + 1][current.col] == ' ') {
          maze[current.row + 1][current.col] = 'O';
          Item next = new Item(current.row + 1, current.col, queue.front - 1);
          queue.put(next);
        }
        if (maze[current.row - 1][current.col] == ' ') {
          maze[current.row - 1][current.col] = 'O';
          Item next = new Item(current.row - 1, current.col, queue.front - 1);
          queue.put(next);
        }
        if (maze[current.row][current.col + 1] == ' ') {
          maze[current.row][current.col + 1] = 'O';
          Item next = new Item(current.row, current.col + 1, queue.front - 1);
          queue.put(next);
        }
        if (maze[current.row][current.col - 1] == ' ') {
          maze[current.row][current.col - 1] = 'O';
          Item next = new Item(current.row, current.col - 1, queue.front - 1);
          queue.put(next);
        }
      }
    }
  }
Beispiel #3
0
 void dfs(int r, int c) {
   maze[r][c] = 'O';
   if (r == goalRow && c == goalCol) {
     print();
   } else {
     if (maze[r + 1][c] == ' ') {
       dfs(r + 1, c);
       maze[r + 1][c] = ' ';
     }
     if (maze[r - 1][c] == ' ') {
       dfs(r - 1, c);
       maze[r - 1][c] = ' ';
     }
     if (maze[r][c + 1] == ' ') {
       dfs(r, c + 1);
       maze[r][c + 1] = ' ';
     }
     if (maze[r][c - 1] == ' ') {
       dfs(r, c - 1);
       maze[r][c - 1] = ' ';
     }
   }
 }