| Author: | Course Instructor | Time limit: | 10 sec | |
| Input / output: | interactive | Memory limit: | 512 Mb |
Fill a Sudoku of box size k: a grid of n × n cells, n = k2, split into n boxes of k × k cells. Every row, every column and every box must contain each of the numbers 1,…,n exactly once. Some cells are given.
You do not have to search for the answer yourself. The judge owns a SAT solver (an oracle) and lets your program use it: you send a formula in conjunctive normal form, the oracle says whether it is satisfiable and, in MODEL mode, returns a satisfying assignment. Translate the puzzle into a formula, ask the oracle, and translate its answer back into a filled grid. The large tests are out of reach of hand-written backtracking within the time limit; a correct encoding makes them easy for the oracle.
This is an interactive problem. After reading the test, your program prints commands (tokens separated by whitespace; a command may span several lines):
a l1 l2 ... 0 adds the clause (l1 ∨ l2 ∨ …). Variables are positive integers up to 5 ⋅ 106, literal − v is the negation of v; you choose the numbering.s a1 a2 ... 0 solves the formula of all clauses added so far under the temporary assumptions a1,a2,… (literals forced true for this query only; the list may be empty: s 0). Flush the output, then read the reply:
SAT; in MODEL mode it is followed by a line of V characters 0/1, where V is the largest variable used so far: the i-th character is the value of variable i;UNSAT; if the query had assumptions, it is followed by a line listing the assumptions used by the refutation, ending with 0;UNKNOWN: the query exceeded C conflicts.ans followed by the filled grid (n lines of n integers) ends the dialogue. Flush the output.In DECISION mode the oracle answers only SAT or UNSAT, without a model, and you must still output a filled grid. Hint: forcing one more literal that keeps the formula satisfiable keeps a solution.
Clauses stay in the oracle between queries: you may add clauses after a reply and ask again.
Example (fragment, MODEL mode): two clauses over variables 1 and 2, one query, the model says x1 = 0, x2 = 1.
you: a 1 2 0 you: a -1 0 you: s 0 oracle: SAT oracle: 01 you: ans ...
Line 1: the mode (MODEL or DECISION), the conflict budget of one query C and the maximum number of queries Q. Line 2: k. Then n lines of n integers, the grid; 0 marks an empty cell.
The commands described above, ending with ans and the filled grid. Any valid filling that keeps the given cells is accepted.
3 ≤ k ≤ 6; every test has a solution.
At most Q queries per test and at most 3 ⋅ 107 literals in all clauses together.
Groups: warm-up (n ≤ 16, MODEL), large (n = 25,36, MODEL), decision (n ≤ 16, DECISION), decision large (n = 25, DECISION).