Permutations with BFS: Solving Haunted House Contest

In summary, the conversation discusses the problem of finding the shortest path through a haunted house filled with candies. The original solution used the greedy approach, but it was not entirely correct as it did not guarantee the shortest path. The expert suggests using backtracking, particularly depth first search, to generate all possible permutations of paths through the candies. The expert also mentions the use of checks to ensure the validity of the paths. The conversation also includes a sample code in Java that solves the problem using breadth first search, but may not work for all maps.
  • #1
Panphobia
435
13
http://dwite.ca/questions/haunted_house.html
So this is from a contest I wrote a while back, I took the greedy approach for my solution, and I did get 5/5, but my solution was not totally correct because the shortest path was not guaranteed, if a candy was 2 spaces before the start, and a candy was 1 space in front, my algorithm would go to the candy 1 space in front, then to the one 2 spaces back, and then carry on. But to finish this algorithm correctly you would need to figure out all permutations of paths through the candies. So how would I even start to do that? It has been bugging me for a while now.
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
It's not entirely clear how you attempted to solve it before because you didn't post any sample code, but the type of algorithm used to generate all permutations is called backtracking. (http://en.wikipedia.org/wiki/Backtracking) Maybe you already knew that.

The basic idea is you have a function that takes a partial path, and extends it by one step. Then it calls itself recursively with the new path, once for each possible next step (think of a depth first search). You'll have to add in checks to make sure the path you are considering is valid, but that shouldn't be too difficult.
 
  • #3
So if I went about it with a breadth first search it wouldn't work correct? I can see how a dfs would solve if yes.
 
  • #4
Breadth first should do it too, but at far as I know the algorithm is usually depth first. I think it has to do with memory optimization. You only need one path in memory at one time in the depth first approach, which can be helpful if the graph is "wide".

I could be mistaken, but breadth first and depth first should be the same if you are trying to find all paths.
 
  • #5
This is my code in java that works for almost all maps except for something like this
*.B***
Code:
import java.io.*;
import java.util.*;

public class CandyBFS {

static int n, candy;
static int minsteps, maxcandy;
static int totCandies = 0;
static boolean[][] is;

public static void main(String[] args) throws FileNotFoundException {
    Scanner s = new Scanner(new File("C:\\Users\\Daniel\\Desktop\\Java\\CandyBFS\\DATA5.txt"));
    while (s.hasNext()) {
        n = Integer.parseInt(s.nextLine().trim());
        char[][] maze = new char[n][n];
        is = new boolean[n][n];
        int xStart = 0;
        int yStart = 0;
        for (int y = 0; y < n; ++y) {
            String text = s.nextLine().trim();
            for (int x = 0; x < n; ++x) {

                maze[x][y] = text.charAt(x);
                if (maze[x][y] == 'B') {
                    xStart = x;
                    yStart = y;
                }
            }
        }
        candy = 0;
        int tot = 0;
        int y = 0, x = 0;
        x = xStart;
        y = yStart;
        for (int j = 0; j < n; ++j) {
            for (int i = 0; i < n; ++i) {
                is[i][j] = false;
            }
        }
        while (true) {
            char[][] grid = new char[n][n];
            for (int j = 0; j < n; ++j) {
                for (int i = 0; i < n; ++i) {
                    grid[i][j] = maze[i][j];
                }
            }
            int lol[] = BFS(grid, x, y);
            if (lol[0] == -1) {
                break;
            }
            y = lol[2];
            x = lol[1];
            tot += lol[0];
        }
        System.out.println(candy + " " + tot);
    }
}

public static int[] BFS(char[][] maze, int xStart, int yStart) {
    Queue<int[]> queue = new LinkedList<int[]>();
    int start[] = {xStart, yStart, 0, 0};
    queue.add(start);
    while (queue.peek() != null) {
        int[] array = queue.poll();
        int x = array[0];
        int y = array[1];
        if (x < 0 || y < 0 || y > n - 1 || x > n - 1) {
            continue;
        }
        if (maze[x][y] == '#') {
            continue;
        }
        if (maze[x][y] == '*' && !is[x][y]) {
            is[x][y] = true;
            candy++;
            int sta[] = {array[2], x, y};
            return sta;

        }
        if (maze[x][y] >= 'a' && maze[x][y] <= 'f') {
            if (candy < maze[x][y] - 'a' + 1) {
                continue;
            }
        }
        int[][] points = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
        for (int i = 0; i < 4; ++i) {
            int sta[] = {x + points[i][0], y + points[i][1], array[2] + 1};
            queue.add(sta);
        }
        maze[x][y] = '#';
    }
    int sta[] = {-1};
    return sta;
}
}
 
Last edited:

1. How does BFS help in solving the Haunted House Contest?

BFS (Breadth-First Search) is a graph traversal algorithm that systematically explores all the neighboring nodes of a given starting node. In the context of the Haunted House Contest, BFS helps in finding all possible permutations of the contestants' positions by exploring all the possible paths through the haunted house, starting from the given starting position. This allows for an efficient and thorough search of all possible solutions.

2. What is the time complexity of using BFS to solve the Haunted House Contest?

The time complexity of BFS is O(V + E), where V is the number of vertices (rooms in the haunted house) and E is the number of edges (connections between rooms). In the context of the Haunted House Contest, this would mean that the time complexity is dependent on the size of the haunted house and the number of connections between rooms. Generally, BFS is considered to be a relatively efficient algorithm for solving this type of problem.

3. Can the Haunted House Contest be solved using other algorithms besides BFS?

Yes, there are other graph traversal algorithms that could be used to solve the Haunted House Contest, such as Depth-First Search or Dijkstra's algorithm. However, BFS is particularly well-suited for this problem because it guarantees to find the shortest path between the starting position and the exit, which is the main objective of the contest.

4. How does the starting position affect the outcome of the Haunted House Contest?

The starting position is crucial in determining the outcome of the Haunted House Contest as it determines which paths will be explored by the BFS algorithm. Different starting positions may lead to different permutations of the contestants' positions and ultimately, different solutions to the contest. It is important to carefully consider the starting position before beginning the contest.

5. Are there any limitations to using BFS to solve the Haunted House Contest?

While BFS is generally an efficient algorithm for solving the Haunted House Contest, it may not be the best choice for very large or complex haunted houses. In these cases, the time and memory required to execute BFS may become significant. Additionally, BFS may not be suitable for certain types of mazes or layouts of the haunted house, as it relies on a systematic exploration of all possible paths.

Similar threads

  • Programming and Computer Science
Replies
2
Views
625
  • Programming and Computer Science
Replies
1
Views
5K
  • Programming and Computer Science
Replies
29
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Linear and Abstract Algebra
Replies
5
Views
1K
  • Special and General Relativity
Replies
13
Views
1K
  • Mechanical Engineering
Replies
15
Views
814
  • Quantum Physics
Replies
2
Views
1K
Replies
25
Views
2K
Back
Top