views:

314

answers:

4

Hello!

I am trying to make a game where a player have to find his way from Start to End on the Game Board. Game board

As you see this Game Board contains a bunch of red circular obstacles. To win the game the player has to remove a minimum amount of obstacles. So my question is, how do I programatically find out the minimum amount of obstacles to remove, to free a path? A free path would be considered the space between, circles not overlapping and not touching.

So what I really need is the minimum amount of circles to remove, I don't need the actual path. Is there an easy way to do this?

And to supplement understanding of this game board, the circles each have the same radius, and it is restricted by the black lines.

Also it is not necessary to move in a straight line.

A: 

One option is to first remove those circles with the most numbers of overlap or touches. After each one you remove, check if its a solution, if not continue removing.

var circle;
circle = findMostOverlapCircle();
while(circle != null) {
    circle.remove();
    circle = findMostOverlapCircle();
}
Guilherme Oenning
I think that removing circles with a minimum number of overlaps would yield a better solution. Try applying your solution on the example he supplied.
Alin Purcaru
+10  A: 

It is a graph theory maximum flow problem.

Suppose that every circle is a node in the graph. Additionally introduce 2 special nodes: TOP and BOTTOM. Connect circles with these nodes if they intersect with TOP/BOTTOM side. Connect nodes corresponding to circles with each other if the circles intersect.

Now you need to find a minimum cut in this graph, having TOP as source and BOTTOM as sink or vice versa. You can use Max-flow_min-cut_theorem to solve it. What it states is that Minimum-cut problem is equivallent to Max-flow problem. You can find details on how to solve Max-Flow problem on TopCoder.

As we can go through each node only once, we should convert the nodes into a directed edge of capacity one with in-node and out-node for each circle. The max-flow algorithm will solve the problem on the resulting graph and take into account the fact that we are removing circles rather than connections between circles. It is always a better decision for this problem to remove a node in a graph rather than edge, as we can always remove any edge by removing a node. Removing a node additionally can result in removing more than one edge.

By the way, a similar problem can be found on Uva Online Judge. It a good idea to try solve this task on the judge, then you will be sure that your solution is correct.

Leonid
@Leonid You're right. I removed my answer.
Alin Purcaru
But circles are not nodes. They can overlap in unmodelable ways. Consider a playfield so covered with circles that every point in the field has at least two circles on top of it. How would you model such mess with a graph?
Dialecticus
If the circle is intersecting with another circle, or lies within it then there is a bi-directional edge between nodes corresponding to those circles in a graph. If there are 2 huge circles covering all field they will be both connected to TOP and BOTTOM nodes, as well as interconnected themselves. We would have to remove both circles as they are both connected to sink and source in the first place (i.e. maximum flow in graph is 2 so as the answer). I'd suggest you to read the article on TopCoder and try to deduce given problem to `min-cut-problem` yourself.
Leonid
OK, but one more thing. The theorem talks about cutting the edges, not removing the nodes. Shouldn't we then model circles as edges in the graph?
Dialecticus
That is a good point. But the nodes in the graph can be replaced with edges of capacity 1 as well. A notion of *note capacity* can be applied here as well - i.e. we're not going though the same node more than once.
Leonid
I think this is the correct answer to this problem, I will try to apply this algorithm to the problem I have.
Cheesebaron
+2  A: 

For a graph translation, something like this might work.

Make a wall(the blue lines) between two circles if they overlap. Don't forget to add in the top and bottom border as well. This creates a couple of regions. These will be all the nodes of the graph.

Next we have to find the edges, the cost of going from one node to another. Take two neighbour regions (sharing a wall.) Then by brute force, or what ever clever method you come up with, determine the cost of moving from this region to the other. If you remove a circle, that means, you remove all the walls that go to that circle. If this makes the two regions into one region, the cost of the edge is 1. If you need to remove two circles(all the walls they have) to combine the two regions, the cost is 2. Etc.

Some of the edges(green) are drawn. We have to go from the start region, to the end region. You now got a everyday weighted graph.

I think this can be improved a lot, but I leave that as an exercise =)

In this case minimum is 3.

Warning, picture is drawn by hand, I did forget a few walls, edges and regions. For illustration purposes only. alt text

Ishtar
That's just silly.
Alin Purcaru
What's so silly about it? Circles are edges, regions are nodes. Makes perfect sense to me.
Ishtar
The walls are silly. You don't have a constant cost for crossing them.
Alin Purcaru
I should clarify it a bit, I guess. I never do say there is a constant cost for crossing the walls (blue lines). However "Each green line has a cost, how many circles I need to remove to get there"
Ishtar
Then the green lines are silly :). There is an infinite number of possibilities to pick those lines.
Alin Purcaru
@Alin - Hope it is more clear now. There are not infinite number of green lines, you only need to look at two regions that share at least one wall. I've forgotten 3 green lines in the drawing (small triangle in bottom,middle)
Ishtar
I think you're missing a blue line at the top-left region, near (slightly to the right of) the big green line interconnection; and missing many others at the bottom-right corner.
Denilson Sá
@Denilson Sá - You are right, sorry. (Miss a few in the center as well) I could edit them back in, but it just creates more and more lines, I wanted to use the picture the illustrate the way of turning it into a graph. A computer would be better finding all the regions.
Ishtar
That's okay. I just happened to look at one of the missing lines first, and I was quite confused by that. :)
Denilson Sá
+7  A: 

In trying to visualize what Leonid wrote, I made the following diagram.

alt text

Svante
+1 for nice picture!
Leonid
Awesome illustration Svante!
Cheesebaron