开发者

Connect the walls algorithm

I have to figure out an algorithm, which finds a solution for the Connect the Walls game.

In Connect the Wall we've got rectangle shape board divided into equal, square cells. In each cell we can put a diagonal wall (directed from left upper corner to right bottom or from left bottom corner to right upper). We have to put walls in all the empty spaces according to the following rules:

  1. Each cell must contain a wall
  2. The walls must not form closed loop (we can't "brick up")
  3. The number of walls connecting to a pillar must match the number written on the pillar
  4. Additionally some cells might contain a wall which we can't move.

Connect the walls online game

Please help.

EDIT:

Ok. I've managed to write simplifying algorithm in C. I need som开发者_StackOverflow中文版eone to check me. It works for now like this: Steps

Moreover, I have a question about the last brute-force step. Can I pick a random cell for start? (maybe there is faster way) The brute-force algorithm as I understand it:

  1. Choose the first cell and mark it / or \
  2. Check if it meets pillars criteria and if there are no cycles
  3. If everything's ok go to the empty neighbour cell, if not back up and change wall direction in previous cell (if we didn't do it before)
  4. Loop until we won't have empty cells on our board. Is that right?

And one more issue concerning fast cycle check. I've figured out that I could use disjoint sets and connected components of graph features. So...We keep connected components of graph in find-union structure. If a new recently added edge (wall) connects points from the same connected component - it's a cycle.


Fun game. Sort of like mine-sweeper.

Are you interested in only a way to get the solution? In that case, it is a simple tree-search. Sort of like solving a maze. Backtracking and stuff. Easy to prune subtrees since the restrictions are quite tight.

A quick way? Look for all "2" pillars on the border. Fill them all first. Also fill the "1" pillars at corners. Mark all walls connecting to "0" pillars as impossible, but mark all the other directions (because all those squares must be marked).

Then for each pillar that has the requisit numbers of walls, mark all other connecting walls to that pillar as impossible, mark all other directions. Then mark all the other walls connected to "3" pillars with one impossible wall. The mark all the other walls connected to "2" pillars with two impossible walls. Then "1" pillars. Loop.

After simplifying your board like this, you'll have to do a normal tree search on the remaining unspecified walls. Mark one potential wall, then loop through the above again to eliminate possibilities.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜