- Related Questions & Answers
- A Number Link Game?
- Write a C program for guessing the number game.
- Stone Game in C++
- Elimination Game in C++
- Dungeon Game in C++
- Zuma Game in C++
- 24 Game in C++
- Nim Game in C++
- A modified game of Nim in C ?
- Jump Game III in C++
- Card Flipping Game in C++
- New 21 Game in C++
- Stone Game II in C++
- Chalkboard XOR Game in C++
- Jump Game V in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

The Game − Suppose an n × n array of squares. Out of these, some of the squares are empty, some are solid, and some non-solid squares are set by integers 1, 2, 3, … .Each integer maintains or occupies exactly two different squares on the board. The task of the player is to connect the two occurrences of each integer on the board with the help of a simple path implementing horizontal and vertical movements alone. No two different paths are permitted to intersect one another. No path may include any solid square (solid squares are not permitted to appear on any path). At last, all non-solid squares must be filled by the paths.

The Algorithm − To construct a valid random puzzle with a given board size n × n, we first produce random simple mutually non-intersecting paths on the board. If a few isolated squares remain outside all the generated paths, mark these isolated squares as solid (forbidden). Next we supply the endpoints of the paths and the list of the solid squares as the puzzle.

Thus we first produce a solution, and next work out the puzzle from the solution. The paths and the solid squares divide or partition the n × n board. We implement a union-find data structure to produce this partition. The data structure is dealt with the subsets of the set of n^2 squares on the board.

Locate squares (a, b) and (c, d) randomly on the board such that −

(a, b) and (c, d) are neighbors of one another, and

neither (a, b) nor (c, d) belongs to any path produced so far. If no such pair of squares is found on the entire board, return FAILURE /* Here, (a, b) and (c, d) are the first two squares on the new path to be constructed. */

Make a union of the two union-find trees containing (a, b) and (c, d).

Repeat so long as the current path can be extended −

Rename (a, b) = (c, d).

Locate a random neighboring square (c, d) of (a, b) such that −

(c, d) does not belong to any path produced so far (including the current one)

the only neighbor (c, d) has on the partially constructed current path is (a, b).

If no such neighbor (c, d) can be found, the path cannot be extended further, so break the loop

Otherwise, make the union of the two union-find trees to which (a, b) and (c, d) belong.

Set the endpoint flags of the two squares that are at the starting and at the termination of the new path.

Return SUCCESS

Advertisements