CSCI3136辅导、辅导c/c++编程设计
Haskell CSCI3136 Ripple Effect
Problem Description
Ripple Effect or Hakyuu is a logic puzzle somewhat similar to Sudoku. The puzzle consists of a rectangular grid divided into regions called rooms or cages. I’ll go with rooms. Some of the cells are already filled with numbers. Most cells are empty.
Just as with Sudoku, the goal is to fill the empty cells with numbers so that a number of rules are satisfied. The rules of Ripple Effect are as follows:
Room Rule: Each room of size s must contain every number between 1 and s exactly once.
Cross Rule: If a cell c contains some value v, then another cell c in the same row or column can also contain value v only if the distance between c and c is more than v. For example, if c is at position (r, c) and stores the value 3, then none of the cells at positions (r - 3, c), (r - 2, c), (r - 1, c), (r + 1, c) (r + 2, c) (r + 3, c) (r, c - 3) (r, c - 2) (r, c - 1) (r, c + 1) (r, c + 2) (r, c + 3) can store the value 3. The cell at position (r + 4, c), for example, can store the value 3 because its distance to c is 3.
For the puzzle in Figure 1, the only solution that satisfies these conditions is the following.
Your Project
Implement, in Haskell of course, a command line tool ripple-effect that takes two arguments. The first one is the name of a file from which to load a Ripple Effect puzzle. The second is the name of a file to which to write the solution. This program should do the following:
Read the input file and print an error message if it is not readable.
Try to solve the puzzle. If there is no solution, it should print This puzzle has no solution.
If there is a solution, it should write the solution to the output file.
Optionally, you may choose to print the solution to the screen in human-readable format, as the puzzle viewer does, described under Test Data and Tools below.
Input and Output Formats
Input Format
For a puzzle with r rows and c columns, the input file consists of 2r + 1 lines.
The first r lines describe the rooms. Each of these lines contains c numbers separated by spaces. In other words, these lines form an r c grid corresponding to the cells of the puzzle. The number corresponding to each cell identifies the room that the cell belongs to. Two cells with the same number belong to the same room. Two cells with different numbers belong to different rooms.
The (r + 1)st line is always empty, to separate the lines describing the rooms from the lines describing the initial cell values.
The last r lines describe the cell values. Each line contains c entries separated by spaces. So, once again, these lines form an r c grid corresponding to the cells of the puzzle. If a cell is initially empty, then its corresponding value is a dash (-). If the cell is non-empty, then its corresponding value is the number the cell contains.
As an example, the format for the example puzzle in Figure 1 of this document is
Notes
In this example, the rooms are numbered by the order of their top-left cells. You should not rely on this being always the case (and I don’t see why it would help).
Most puzzles provided as test inputs (see Test Data and Tools below) have only rooms of size less than 10. Thus, all numbers in the puzzle can be represented as one-digit numbers. Neither your parser for puzzle files nor your solver should rely on this because there are some input files with rooms of size 10 or greater, and with some prefilled values in these rooms equal to 10 or greater.
Output Format
For a puzzle with r rows and c columns, the output file should consist of r lines storing c numbers each. The numbers on each line should be separated by spaces. Thus, these lines form an r c grid of numbers corresponding to the cells of the puzzle. The value corresponding to each puzzle cell should be the value stored in this puzzle cell in the solution. For example, the solution in Figure 3 is represented by the following output file content:
Requirements
Your program must satisfy the following requirements:
It must deal with incorrect command-line arguments gracefully. That is, if too few or too many command line arguments are provided, your program should print a usage message, and must not crash.
USAGE: ripple-effect