You could start by scanning the map into a 2-dimensional array. Then, make a list of all places where a brigde might occur – in other words, places where two islands appear on the same row or same column, with no other island between them. It may be helpful to think of multiple bridges between the same pair of islands as a single bridge with multiple "planks". The puzzle then becomes a Constraint Satisfaction Problem where the Variables are the bridges, and the Value of each variable is the number of planks in that bridge, which could be 0, 1, 2 or 3.
The file scan_print_map.py
in the tools
directory provides code for scanning the map into a 2D numpy array.
The file cryptarith.py
in the
code
directory cantains code for solving cryptarithmetic puzzles with a basic backtracking search. This might help you to get started with a basic search (which you could then improve on).
Your program will be tested on a variety of inputs ranging from small to large. There will be no more than 40 rows, 40 columns, 500 islands and 800 bridges. The maximum runtime will be two minutes (120 seconds) for each input.
Your program should read the input (map) from stdin
, and print the solution to stout
.
bridgen
generates a map and prints it to stdout
.
bridgecheck
takes the name of the file with the map as a command-line argument, reads the (putative) solution from stdin
,
and prints its output to stdout
.
On a Unix system, you could test your code like this:
./bridgen 10 20 > s1.in ./hashi < s1.in > s1.out ./bridgecheck s1.in < s1.outNote: You do not need to include
bridgen
or bridgecheck
in your submission. They are just provided as tools, to help you test your code.
No, you can assume the input will be in the correct format, and that at least one solution exists.