21
2
Introduction
Write a solver for the Hitori puzzles using least bytes.
Challenge
Your task is write a solver for the Hitori (ひとり, the word for "alone" in Japanese; the meaning of the game name is "Leave me alone") logical puzzles. The rules are as follows:
- You are presented with a n-by-n grid of cells, each cell contains an integer between 1 and n (inclusive).
- Your goal is to make sure that no number appear more than once in each row and each column of the grid, by removing numbers from the given grid, subject to the restriction indicated in the next two rules,
- You cannot remove two numbers from two adjacent (horizontally or vertically) cells.
- The remaining numbered cells must be all connected to each other. It means that any two remaining numbered cells can be connected with a curve that is composed solely of segments connecting adjacent remaining numbers (horizonally or vertically). (Thanks to @user202729 for pointing out that this is missing)
I hope the rules are clear by now. If there is anything unclear about the rules, check the Wikipedia page.
Test Cases
The cells from which the numbers are removed are represented with 0s.
Input -> Output
4
2 2 2 4 0 2 0 4
1 4 2 3 -> 1 4 2 3
2 3 2 1 2 3 0 1
3 4 1 2 3 0 1 2
4
4 2 4 3 0 2 4 3
4 1 1 2 -> 4 1 0 2
3 1 2 1 3 0 2 1
4 3 1 3 0 3 1 0
5
1 5 3 1 2 1 5 3 0 2
5 4 1 3 4 5 0 1 3 4
3 4 3 1 5 -> 3 4 0 1 5
4 4 2 3 3 4 0 2 0 3
2 1 5 4 4 2 1 5 4 0
8
4 8 1 6 3 2 5 7 0 8 0 6 3 2 0 7
3 6 7 2 1 6 5 4 3 6 7 2 1 0 5 4
2 3 4 8 2 8 6 1 0 3 4 0 2 8 6 1
4 1 6 5 7 7 3 5 -> 4 1 0 5 7 0 3 0
7 2 3 1 8 5 1 2 7 0 3 0 8 5 1 2
3 5 6 7 3 1 8 4 0 5 6 7 0 1 8 0
6 4 2 3 5 4 7 8 6 0 2 3 5 4 7 8
8 7 1 4 2 3 5 6 8 7 1 4 0 3 0 6
9
8 6 5 6 8 1 2 2 9 8 0 5 6 0 1 2 0 9
5 6 2 4 1 7 9 8 3 5 6 2 4 1 7 9 8 3
5 8 2 5 9 9 8 2 6 0 8 0 5 0 9 0 2 0
9 5 6 6 4 3 8 4 1 9 5 6 0 4 3 8 0 1
1 1 6 3 9 9 5 6 2 -> 0 1 0 3 9 0 5 6 2
1 1 4 7 3 8 3 8 6 1 0 4 7 0 8 3 0 6
3 7 4 1 2 6 4 5 5 3 7 0 1 2 6 4 5 0
3 3 1 9 8 7 7 4 5 0 3 1 9 8 0 7 4 5
2 9 7 5 3 5 9 1 3 2 9 7 0 3 5 0 1 0
These test cases are taken from Concept Is Puzzles, PuzzleBooks, Concept Is Puzzles, Wikipedia, and Youtube, respectively.
Specs
No need to worry about exception handling.
You can assume that the input is always a valid puzzle with a unique solution and you can take advantage of this in writing your code.
This is code-golf, the lowest number of bytes wins.
4 <= n <= 9 (16 originally, changed to 9 following Stewie Griffin's suggestion, also save some trouble in IO)
You can take input and provide output through any standard form, and you are free to choose the format.
Some suggestions for the output format are (but you are not restricted to these)
- Outputting the final grid
- Outputting the grid containing all removed numbers
- Output a list of coordinates of one of the above
As usual, default loopholes apply here.
Related(inspired by this challenge): Check if All Elements In a Matrix Are Connected
My last challenge: Extension of the Game of Sevens
2I suggest that you require deterministic runtime, or require that the largest test case can be solved in no more than 1 minute (or maybe more/less). Also, you say
4 <= n <= 16
, but the largest test case is forn=9
. I suggest you either post an=16
test case, or say4 <= n <= 9
. Nice challenge by the way :) – Stewie Griffin – 2018-01-29T21:35:47.3501@StewieGriffin how about just having a separate fastest algorithm challenge? – Jonathan Allan – 2018-01-29T22:05:01.310
@StewieGriffin Tried to add a 16x16 but not yet quite ready. Changed to 9 now. – Weijun Zhou – 2018-01-29T22:35:40.970
@JonathanAllan As you wish. – Weijun Zhou – 2018-01-30T15:51:51.627
Re "I decide to make a change to see whether it would be better": It would definitely be worse. Also you should not change an already posted challenge. – user202729 – 2018-01-31T08:17:06.377
(I rolled back the challenge. If you want, you can post a separate fastest algorithm challenge. Don't worry, we allow the same challenge with different winning criteria) – user202729 – 2018-01-31T08:30:49.767
@JonathanAllan (Just FYI, I posted a Jelly solution to the connectivity challenge) – user202729 – 2018-01-31T08:33:07.373
@user202729 I didn't see your message until now. Thank you. I still don't have a good grasp of the types of puzzles ... – Weijun Zhou – 2018-02-03T01:09:41.767