Kaggle Ministral 3 (3B) Reinforcement Learning Sudoku Game
To run this, press "Runtime" and press "Run all" on a free Tesla T4 Google Colab instance!
To install Unsloth on your local device, follow our guide. This notebook is licensed LGPL-3.0.
You will learn how to do data prep, how to train, how to run the model, & how to save it
Goal: Make Ministral solve Sudoku puzzles with Reinforcement Learning
Our goal is to make Ministral learn to solve Sudoku puzzles using reinforcement learning (GRPO). The model will devise a strategy to fill in empty cells, and we'll reward it for correct placements and completing valid puzzles.
News
Train MoEs - DeepSeek, GLM, Qwen and gpt-oss 12x faster with 35% less VRAM. Blog
You can now train embedding models 1.8-3.3x faster with 20% less VRAM. Blog
Ultra Long-Context Reinforcement Learning is here with 7x more context windows! Blog
3x faster LLM training with 30% less VRAM and 500K context. 3x faster • 500K Context
New in Reinforcement Learning: FP8 RL • Vision RL • Standby • gpt-oss RL
Visit our docs for all our model uploads and notebooks.
Installation
Unsloth
🦥 Unsloth: Will patch your computer to enable 2x faster free finetuning. ERROR 12-02 03:15:55 [fa_utils.py:64] Cannot use FA version 2 is not supported due to FA2 is only supported on devices with compute capability >= 8 🦥 Unsloth Zoo will now patch everything to make training faster!==((====))== Unsloth 2025.11.6: Fast Ministral3 patching. Transformers: 5.0.0.dev0. vLLM: 0.11.2. \\ /| Tesla T4. Num GPUs = 1. Max memory: 14.741 GB. Platform: Linux. O^O/ \_/ \ Torch: 2.9.0+cu126. CUDA: 7.5. CUDA Toolkit: 12.6. Triton: 3.2.0 \ / Bfloat16 = FALSE. FA [Xformers = 0.0.33.post1. FA2 = False] "-____-" Free license: http://github.com/unslothai/unsloth Unsloth: Fast downloading is enabled - ignore downloading bars which are red colored! Unsloth: QLoRA and full finetuning all not selected. Switching to 16bit LoRA.
model.safetensors.index.json: 0%| | 0.00/45.6k [00:00<?, ?B/s]
Downloading (incomplete total...): 0.00B [00:00, ?B/s]
Fetching 2 files: 0%| | 0/2 [00:00<?, ?it/s]
Loading weights: 0%| | 0/458 [00:00<?, ?it/s]
generation_config.json: 0%| | 0.00/131 [00:00<?, ?B/s]
processor_config.json: 0%| | 0.00/189 [00:00<?, ?B/s]
chat_template.jinja: 0%| | 0.00/7.50k [00:00<?, ?B/s]
preprocessor_config.json: 0%| | 0.00/728 [00:00<?, ?B/s]
tokenizer_config.json: 0%| | 0.00/206k [00:00<?, ?B/s]
tokenizer.json: 0%| | 0.00/17.1M [00:00<?, ?B/s]
special_tokens_map.json: 0%| | 0.00/21.4k [00:00<?, ?B/s]
To do efficient RL, we will use LoRA, which allows us to only add 1 to 5% of extra weights to the model for finetuning purposes. This allows us to save memory usage by over 60%, and yet it retains good accuracy.
Unsloth: Making `model.base_model.model.model.vision_tower.transformer` require gradients
Sudoku Game Implementation
We use GPT-5 to create a clean Sudoku solver environment. The strategy outputs "row,col,value" to fill cells.
Test the Sudoku environment:
Initial puzzle: ┌───────┬───────┬───────┐ │ 4 . 8 │ . 5 2 │ 6 3 . │ │ . 9 3 │ 4 6 7 │ . . 1 │ │ 6 1 2 │ . 9 8 │ 4 . . │ ├───────┼───────┼───────┤ │ 1 . 4 │ . . . │ 7 9 5 │ │ 3 . 9 │ 7 1 . │ 8 2 6 │ │ 7 8 . │ 5 . 9 │ 1 . 3 │ ├───────┼───────┼───────┤ │ 2 4 . │ 9 7 . │ . 6 . │ │ 8 3 5 │ 6 4 . │ . 7 . │ │ . . 7 │ 2 . . │ . 1 4 │ └───────┴───────┴───────┘ State: ongoing, Moves: 0
SudokuGame(difficulty=30, seed=42)
Try making some moves:
After placing 7 at (1,0): ┌───────┬───────┬───────┐ │ 4 7 8 │ . 5 2 │ 6 3 . │ │ . 9 3 │ 4 6 7 │ . . 1 │ │ 6 1 2 │ . 9 8 │ 4 . . │ ├───────┼───────┼───────┤ │ 1 . 4 │ . . . │ 7 9 5 │ │ 3 . 9 │ 7 1 . │ 8 2 6 │ │ 7 8 . │ 5 . 9 │ 1 . 3 │ ├───────┼───────┼───────┤ │ 2 4 . │ 9 7 . │ . 6 . │ │ 8 3 5 │ 6 4 . │ . 7 . │ │ . . 7 │ 2 . . │ . 1 4 │ └───────┴───────┴───────┘ State: ongoing, Moves: 1
If we do some other action that's not part of the action space, we will get an error, and the game will not accept anymore actions.
RL Environment Setup
Execute strategies with time limits to prevent infinite loops.
To allow longer strategies for Reinforcement Learning, we shall allow a 10 second timer.
Test with a simple strategy:
Moves: 1, State: failed
┌───────┬───────┬───────┐ │ 4 7 8 │ . 5 2 │ 6 3 . │ │ . 9 3 │ 4 6 7 │ . . 1 │ │ 6 1 2 │ . 9 8 │ 4 . . │ ├───────┼───────┼───────┤ │ 1 . 4 │ . . . │ 7 9 5 │ │ 3 . 9 │ 7 1 . │ 8 2 6 │ │ 7 8 . │ 5 . 9 │ 1 . 3 │ ├───────┼───────┼───────┤ │ 2 4 . │ 9 7 . │ . 6 . │ │ 8 3 5 │ 6 4 . │ . 7 . │ │ . . 7 │ 2 . . │ . 1 4 │ └───────┴───────┴───────┘
Code Execution
To execute and create a new Python function, we first have to check if the function does not call other global variables or cheat. This is called countering reward hacking since we don't want the function to cheat.
For example the below piece of code is fine, since it only imports Python level functions. We use check_python_modules:
Safe Python code? True
{'stdlib': [], 'non_stdlib': [], 'relative_imports': 0}
For the below piece of code, since we import numpy, we should not allow the execution:
Safe Python code? False
{'stdlib': [], 'non_stdlib': ['numpy'], 'relative_imports': 0}
Data & RL task setup
Create the prompt that instructs the model to generate a Sudoku solving strategy. You can customize this to some other task for another RL task.
Create a Sudoku solving strategy using only native Python built-in functions without any import statements.
You are given two lists of lists (9x9 grids):
- board: current state (0 means empty)
- initial: starting puzzle (0 means was empty, numbers are fixed)
Return a tuple (row, col, number) for the next move.
- row: 0-8 (row index)
- col: 0-8 (column index)
- number: 1-9 (digit to place)
Only place numbers in cells that are BOTH empty in initial AND empty in board (initial[row][col] == 0 AND board[row][col] == 0)
Use Sudoku rules: no duplicates in rows, columns, or 3x3 boxes.
Output your function in backticks:
```python
def strategy(board, initial):
# Your logic here
return (row, col, number)
```
All helper functions must be inside def strategy. Output only the function.
First, let's prompt the model without RL and see how it goes:
==================================================
BASE MODEL OUTPUT (before RL training):
==================================================
<s><s>[SYSTEM_PROMPT]You are Ministral-3-3B-Instruct-2512, a Large Language Model (LLM) created by Mistral AI, a French startup headquartered in Paris.
You power an AI assistant called Le Chat.
Your knowledge base was last updated on 2023-10-01.
The current date is {today}.
When you're not sure about some information or when the user's request requires up-to-date or specific data, you must use the available tools to fetch the information. Do not hesitate to use tools whenever they can provide a more accurate or complete response. If no relevant tools are available, then clearly state that you don't have the information and avoid making up anything.
If the user's question is not clear, ambiguous, or does not provide enough context for you to accurately answer the question, you do not try to answer it right away and you rather ask the user to clarify their request (e.g. "What are some good restaurants around me?" => "Where are you?" or "When is the next flight to Tokyo" => "Where do you travel from?").
You are always very attentive to dates, in particular you try to resolve dates (e.g. "yesterday" is {yesterday}) and when asked about information at specific dates, you discard information that is at another date.
You follow these instructions in all languages, and always respond to the user in the language they use or request.
Next sections describe the capabilities that you have.
# WEB BROWSING INSTRUCTIONS
You cannot perform any web search or access internet to open URLs, links etc. If it seems like the user is expecting you to do so, you clarify the situation and ask the user to copy paste the text directly in the chat.
# MULTI-MODAL INSTRUCTIONS
You have the ability to read images, but you cannot generate images. You also cannot transcribe audio files or videos.
You cannot read nor transcribe audio files or videos.
# TOOL CALLING INSTRUCTIONS
You may have access to tools that you can use to fetch information or perform actions. You must use these tools in the following situations:
1. When the request requires up-to-date information.
2. When the request requires specific data that you do not have in your knowledge base.
3. When the request involves actions that you cannot perform without tools.
Always prioritize using tools to provide the most accurate and helpful response. If tools are not available, inform the user that you cannot perform the requested action at the moment.[/SYSTEM_PROMPT][INST]Create a Sudoku solving strategy using only native Python built-in functions without any import statements.
You are given two lists of lists (9x9 grids):
- board: current state (0 means empty)
- initial: starting puzzle (0 means was empty, numbers are fixed)
Return a tuple (row, col, number) for the next move.
- row: 0-8 (row index)
- col: 0-8 (column index)
- number: 1-9 (digit to place)
Only place numbers in cells that are BOTH empty in initial AND empty in board (initial[row][col] == 0 AND board[row][col] == 0)
Use Sudoku rules: no duplicates in rows, columns, or 3x3 boxes.
Output your function in backticks:
```python
def strategy(board, initial):
# Your logic here
return (row, col, number)
```
All helper functions must be inside def strategy. Output only the function.[/INST]Here's a Sudoku-solving strategy using only native Python built-in functions:
```python
def strategy(board, initial):
def is_valid(row, col, num):
# Check row
if num in board[row]:
return False
# Check column
if any(board[i][col] == num for i in range(9)):
return False
# Check 3x3 box
box_row, box_col = 3 * (row // 3), 3 * (col // 3)
if any(board[i][j] == num for i in range(box_row, box_row + 3)
for j in range(box_col, box_col + 3)):
return False
return True
def find_empty():
for row in range(9):
for col in range(9):
if initial[row][col] == 0 and board[row][col] == 0:
for num in range(1, 10):
if is_valid(row, col, num):
return (row, col, num)
return None
return find_empty()
```</s> Reward functions
We now design a extract_function function which simply extracts the function wrapped in 3 back ticks.
And 3 reward functions:
function_workswhich rewards the model if the strategy is a valid Python function.no_cheatingwhich checks if the function imported other modules, and if it did, we penalize it.strategy_succeedswhich checks if the game strategy actually succeeds in attaining Sudoku after running the auto-generated strategy.
Reward 1: Function Works
Checks if the generated code is valid Python and can be executed.
Reward 2: No Cheating
Penalizes functions that import external libraries.
Reward 3: Strategy Succeeds
Rewards strategies that successfully solve Sudoku puzzles.
Dataset Preparation
Create the training dataset.
Maximum prompt length: 3245
Dataset sample:
{'prompt': [{'content': 'Create a Sudoku solving strategy using only native Python built-in functions without any import statements.\nYou are given two lists of lists (9x9 grids):\n- board: current state (0 means empty)\n- initial: starting puzzle (0 means was empty, numbers are fixed)\n\nReturn a tuple (row, col, number) for the next move.\n- row: 0-8 (row index)\n- col: 0-8 (column index)\n- number: 1-9 (digit to place)\n\nOnly place numbers in cells that are BOTH empty in initial AND empty in board (initial[row][col] == 0 AND board[row][col] == 0)\nUse Sudoku rules: no duplicates in rows, columns, or 3x3 boxes.\nOutput your function in backticks:\n```python\ndef strategy(board, initial):\n # Your logic here\n return (row, col, number)\n```\nAll helper functions must be inside def strategy. Output only the function.', 'role': 'user'}], 'answer': 0} Train the model
Now set up GRPO Trainer and all configurations! We also support GSPO, GAPO, Dr GRPO and more! Go the Unsloth Reinforcement Learning Docs for more options.
warmup_ratio is deprecated and will be removed in v5.2. Use `warmup_steps` instead.
Unsloth: We now expect `per_device_train_batch_size` * `gradient_accumulation_steps` * `world_size` to be a multiple of `num_generations`. We will change the batch size of 1 to the `num_generations` of 4
And let's run the trainer! If you scroll up, you'll see a table of rewards. The goal is to see the reward column increase!
You might have to wait 150 to 200 steps for any action. You'll probably get low reward for the first 100 steps. Please be patient!
| Step | Training Loss | reward | reward_std | completion_length | kl |
|---|---|---|---|---|---|
| 1 | 0.000000 | 0.125000 | 0.000000 | 200.000000 | 0.000000 |
| 2 | 0.000000 | 0.072375 | 0.248112 | 200.000000 | 0.000000 |
| 3 | 0.000000 | -0.079000 | 0.163776 | 182.500000 | 0.000005 |
And let's train the model!
NOTE A T4 free GPU might take 5 minutes for one generation sadly since it's an old GPU - A100 or H100 will be much faster!
The model is already on multiple devices. Skipping the move to device specified in `args`.
==((====))== Unsloth - 2x faster free finetuning | Num GPUs used = 1
\\ /| Num examples = 1,000 | Num Epochs = 1 | Total steps = 200
O^O/ \_/ \ Batch size per device = 4 | Gradient accumulation steps = 1
\ / Data Parallel GPUs = 1 | Total batch size (4 x 1 x 1) = 4
"-____-" Trainable parameters = 67,502,080 of 3,916,592,128 (1.72% trained)
`generation_config` default values have been modified to match model-specific defaults: {'max_length': 262144}. If this is not desired, please set these values explicitly.
Streaming output truncated to the last 5000 lines. ┌───────┬───────┬───────┐ │ 2 3 1 │ 4 5 6 │ 7 9 8 │ │ 8 7 9 │ 1 3 2 │ 4 6 5 │ │ 6 5 4 │ 8 9 7 │ 1 3 2 │ ├───────┼───────┼───────┤ │ 3 4 5 │ 2 8 1 │ 9 . 6 │ │ 7 6 8 │ 3 4 9 │ 5 2 1 │ │ 1 9 2 │ 6 . 5 │ 3 7 4 │ ├───────┼───────┼───────┤ │ 5 2 7 │ 9 1 8 │ 6 4 3 │ │ 9 1 3 │ 5 6 4 │ 8 . 7 │ │ 4 8 6 │ . 7 3 │ 2 1 9 │ └───────┴───────┴───────┘ Valid moves: 31, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 4 6 1 │ . 3 2 │ 8 5 9 │ │ 3 5 7 │ 4 9 8 │ 1 2 6 │ │ 9 8 2 │ 5 1 7 │ . 4 3 │ ├───────┼───────┼───────┤ │ 2 3 5 │ 7 4 1 │ 9 6 8 │ │ 1 9 6 │ 8 2 3 │ 5 7 4 │ │ 8 7 4 │ 6 5 9 │ 3 1 2 │ ├───────┼───────┼───────┤ │ 5 2 3 │ 9 6 . │ 4 8 1 │ │ 6 4 8 │ 3 7 5 │ 2 9 . │ │ 7 1 9 │ 2 8 4 │ 6 3 5 │ └───────┴───────┴───────┘ Valid moves: 31, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 4 6 1 │ . 3 2 │ 8 5 9 │ │ 3 5 7 │ 4 9 8 │ 1 2 6 │ │ 9 8 2 │ 5 1 7 │ . 4 3 │ ├───────┼───────┼───────┤ │ 2 3 5 │ 7 4 1 │ 9 6 8 │ │ 1 9 6 │ 8 2 3 │ 5 7 4 │ │ 8 7 4 │ 6 5 9 │ 3 1 2 │ ├───────┼───────┼───────┤ │ 5 2 3 │ 9 6 . │ 4 8 1 │ │ 6 4 8 │ 3 7 5 │ 2 9 . │ │ 7 1 9 │ 2 8 4 │ 6 3 5 │ └───────┴───────┴───────┘ Valid moves: 31, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 4 6 1 │ . 3 2 │ 8 5 9 │ │ 3 5 7 │ 4 9 8 │ 1 2 6 │ │ 9 8 2 │ 5 1 7 │ . 4 3 │ ├───────┼───────┼───────┤ │ 2 3 5 │ 7 4 1 │ 9 6 8 │ │ 1 9 6 │ 8 2 3 │ 5 7 4 │ │ 8 7 4 │ 6 5 9 │ 3 1 2 │ ├───────┼───────┼───────┤ │ 5 2 3 │ 9 6 . │ 4 8 1 │ │ 6 4 8 │ 3 7 5 │ 2 9 . │ │ 7 1 9 │ 2 8 4 │ 6 3 5 │ └───────┴───────┴───────┘ Valid moves: 31, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 4 6 1 │ . 3 2 │ 8 5 9 │ │ 3 5 7 │ 4 9 8 │ 1 2 6 │ │ 9 8 2 │ 5 1 7 │ . 4 3 │ ├───────┼───────┼───────┤ │ 2 3 5 │ 7 4 1 │ 9 6 8 │ │ 1 9 6 │ 8 2 3 │ 5 7 4 │ │ 8 7 4 │ 6 5 9 │ 3 1 2 │ ├───────┼───────┼───────┤ │ 5 2 3 │ 9 6 . │ 4 8 1 │ │ 6 4 8 │ 3 7 5 │ 2 9 . │ │ 7 1 9 │ 2 8 4 │ 6 3 5 │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, -1) # Backtrack: no solution found for this backtrack step ============================================================ Valid moves: 31, Final state: failed Final board: ┌───────┬───────┬───────┐ │ 1 3 7 │ 2 . 4 │ 6 5 9 │ │ 6 2 . │ 1 5 3 │ 7 8 4 │ │ 4 5 9 │ 6 7 8 │ 2 3 1 │ ├───────┼───────┼───────┤ │ 2 6 5 │ 8 4 . │ 9 1 7 │ │ 3 9 4 │ 7 6 1 │ 8 2 5 │ │ 7 1 8 │ 9 2 5 │ 3 4 6 │ ├───────┼───────┼───────┤ │ 5 7 6 │ 4 8 2 │ 1 9 3 │ │ 9 4 2 │ 3 1 7 │ 5 6 8 │ │ 8 . 1 │ 5 9 6 │ 4 7 2 │ └───────┴───────┴───────┘ Valid moves: 31, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 1 3 7 │ 2 . 4 │ 6 5 9 │ │ 6 2 . │ 1 5 3 │ 7 8 4 │ │ 4 5 9 │ 6 7 8 │ 2 3 1 │ ├───────┼───────┼───────┤ │ 2 6 5 │ 8 4 . │ 9 1 7 │ │ 3 9 4 │ 7 6 1 │ 8 2 5 │ │ 7 1 8 │ 9 2 5 │ 3 4 6 │ ├───────┼───────┼───────┤ │ 5 7 6 │ 4 8 2 │ 1 9 3 │ │ 9 4 2 │ 3 1 7 │ 5 6 8 │ │ 8 . 1 │ 5 9 6 │ 4 7 2 │ └───────┴───────┴───────┘ Valid moves: 31, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 1 3 7 │ 2 . 4 │ 6 5 9 │ │ 6 2 . │ 1 5 3 │ 7 8 4 │ │ 4 5 9 │ 6 7 8 │ 2 3 1 │ ├───────┼───────┼───────┤ │ 2 6 5 │ 8 4 . │ 9 1 7 │ │ 3 9 4 │ 7 6 1 │ 8 2 5 │ │ 7 1 8 │ 9 2 5 │ 3 4 6 │ ├───────┼───────┼───────┤ │ 5 7 6 │ 4 8 2 │ 1 9 3 │ │ 9 4 2 │ 3 1 7 │ 5 6 8 │ │ 8 . 1 │ 5 9 6 │ 4 7 2 │ └───────┴───────┴───────┘ Valid moves: 31, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 1 3 7 │ 2 . 4 │ 6 5 9 │ │ 6 2 . │ 1 5 3 │ 7 8 4 │ │ 4 5 9 │ 6 7 8 │ 2 3 1 │ ├───────┼───────┼───────┤ │ 2 6 5 │ 8 4 . │ 9 1 7 │ │ 3 9 4 │ 7 6 1 │ 8 2 5 │ │ 7 1 8 │ 9 2 5 │ 3 4 6 │ ├───────┼───────┼───────┤ │ 5 7 6 │ 4 8 2 │ 1 9 3 │ │ 9 4 2 │ 3 1 7 │ 5 6 8 │ │ 8 . 1 │ 5 9 6 │ 4 7 2 │ └───────┴───────┴───────┘ Valid moves: 28, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 1 3 . │ 2 4 6 │ 5 7 9 │ │ 4 7 6 │ 1 9 8 │ 3 2 . │ │ 8 5 2 │ . 3 7 │ 1 4 6 │ ├───────┼───────┼───────┤ │ . 6 4 │ 8 7 3 │ 9 5 1 │ │ 2 8 7 │ 4 5 9 │ 6 3 . │ │ 5 . 9 │ 6 2 1 │ 4 . 8 │ ├───────┼───────┼───────┤ │ 6 2 3 │ 7 1 5 │ 8 9 4 │ │ 9 4 8 │ 3 6 2 │ 7 1 5 │ │ 7 1 5 │ 9 8 4 │ 2 6 3 │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, -1) # Backtrack: no position filled yet ============================================================ Valid moves: 28, Final state: failed Final board: ┌───────┬───────┬───────┐ │ 1 3 . │ 2 4 6 │ 5 7 9 │ │ 4 7 6 │ 1 9 8 │ 3 2 . │ │ 8 5 2 │ . 3 7 │ 1 4 6 │ ├───────┼───────┼───────┤ │ . 6 4 │ 8 7 3 │ 9 5 1 │ │ 2 8 7 │ 4 5 9 │ 6 3 . │ │ 5 . 9 │ 6 2 1 │ 4 . 8 │ ├───────┼───────┼───────┤ │ 6 2 3 │ 7 1 5 │ 8 9 4 │ │ 9 4 8 │ 3 6 2 │ 7 1 5 │ │ 7 1 5 │ 9 8 4 │ 2 6 3 │ └───────┴───────┴───────┘ Valid moves: 28, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 1 3 . │ 2 4 6 │ 5 7 9 │ │ 4 7 6 │ 1 9 8 │ 3 2 . │ │ 8 5 2 │ . 3 7 │ 1 4 6 │ ├───────┼───────┼───────┤ │ . 6 4 │ 8 7 3 │ 9 5 1 │ │ 2 8 7 │ 4 5 9 │ 6 3 . │ │ 5 . 9 │ 6 2 1 │ 4 . 8 │ ├───────┼───────┼───────┤ │ 6 2 3 │ 7 1 5 │ 8 9 4 │ │ 9 4 8 │ 3 6 2 │ 7 1 5 │ │ 7 1 5 │ 9 8 4 │ 2 6 3 │ └───────┴───────┴───────┘ Valid moves: 28, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 1 3 . │ 2 4 6 │ 5 7 9 │ │ 4 7 6 │ 1 9 8 │ 3 2 . │ │ 8 5 2 │ . 3 7 │ 1 4 6 │ ├───────┼───────┼───────┤ │ . 6 4 │ 8 7 3 │ 9 5 1 │ │ 2 8 7 │ 4 5 9 │ 6 3 . │ │ 5 . 9 │ 6 2 1 │ 4 . 8 │ ├───────┼───────┼───────┤ │ 6 2 3 │ 7 1 5 │ 8 9 4 │ │ 9 4 8 │ 3 6 2 │ 7 1 5 │ │ 7 1 5 │ 9 8 4 │ 2 6 3 │ └───────┴───────┴───────┘ Valid moves: 27, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 3 4 6 │ 1 . 5 │ 7 2 9 │ │ 7 1 . │ 2 3 9 │ 5 4 8 │ │ 5 9 2 │ 8 6 4 │ 3 . 1 │ ├───────┼───────┼───────┤ │ 4 6 5 │ 3 2 7 │ 8 1 . │ │ 1 3 9 │ . 4 8 │ 2 5 6 │ │ 2 7 8 │ 6 9 1 │ 4 3 . │ ├───────┼───────┼───────┤ │ 8 2 1 │ 5 7 3 │ 6 9 4 │ │ 9 5 3 │ 4 8 6 │ 1 7 2 │ │ 6 . 7 │ 9 1 2 │ . 8 3 │ └───────┴───────┴───────┘ Valid moves: 27, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 3 4 6 │ 1 . 5 │ 7 2 9 │ │ 7 1 . │ 2 3 9 │ 5 4 8 │ │ 5 9 2 │ 8 6 4 │ 3 . 1 │ ├───────┼───────┼───────┤ │ 4 6 5 │ 3 2 7 │ 8 1 . │ │ 1 3 9 │ . 4 8 │ 2 5 6 │ │ 2 7 8 │ 6 9 1 │ 4 3 . │ ├───────┼───────┼───────┤ │ 8 2 1 │ 5 7 3 │ 6 9 4 │ │ 9 5 3 │ 4 8 6 │ 1 7 2 │ │ 6 . 7 │ 9 1 2 │ . 8 3 │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, -1) # Backtrack: no more empty cells to place numbers ============================================================ Valid moves: 27, Final state: failed Final board: ┌───────┬───────┬───────┐ │ 3 4 6 │ 1 . 5 │ 7 2 9 │ │ 7 1 . │ 2 3 9 │ 5 4 8 │ │ 5 9 2 │ 8 6 4 │ 3 . 1 │ ├───────┼───────┼───────┤ │ 4 6 5 │ 3 2 7 │ 8 1 . │ │ 1 3 9 │ . 4 8 │ 2 5 6 │ │ 2 7 8 │ 6 9 1 │ 4 3 . │ ├───────┼───────┼───────┤ │ 8 2 1 │ 5 7 3 │ 6 9 4 │ │ 9 5 3 │ 4 8 6 │ 1 7 2 │ │ 6 . 7 │ 9 1 2 │ . 8 3 │ └───────┴───────┴───────┘ Valid moves: 27, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 3 4 6 │ 1 . 5 │ 7 2 9 │ │ 7 1 . │ 2 3 9 │ 5 4 8 │ │ 5 9 2 │ 8 6 4 │ 3 . 1 │ ├───────┼───────┼───────┤ │ 4 6 5 │ 3 2 7 │ 8 1 . │ │ 1 3 9 │ . 4 8 │ 2 5 6 │ │ 2 7 8 │ 6 9 1 │ 4 3 . │ ├───────┼───────┼───────┤ │ 8 2 1 │ 5 7 3 │ 6 9 4 │ │ 9 5 3 │ 4 8 6 │ 1 7 2 │ │ 6 . 7 │ 9 1 2 │ . 8 3 │ └───────┴───────┴───────┘ Valid moves: 26, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 1 8 3 │ 2 5 . │ 6 9 7 │ │ 2 4 . │ 6 9 7 │ 5 1 8 │ │ 6 5 7 │ 4 8 1 │ 3 2 . │ ├───────┼───────┼───────┤ │ 4 7 1 │ 5 6 8 │ 2 . 3 │ │ 3 6 5 │ 1 7 2 │ 9 4 . │ │ 8 2 . │ 9 3 4 │ . 6 1 │ ├───────┼───────┼───────┤ │ 5 . 2 │ 7 1 6 │ 8 3 9 │ │ 7 3 6 │ 8 4 9 │ 1 5 2 │ │ 9 1 4 │ 3 2 5 │ 7 . 6 │ └───────┴───────┴───────┘ Valid moves: 26, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 1 8 3 │ 2 5 . │ 6 9 7 │ │ 2 4 . │ 6 9 7 │ 5 1 8 │ │ 6 5 7 │ 4 8 1 │ 3 2 . │ ├───────┼───────┼───────┤ │ 4 7 1 │ 5 6 8 │ 2 . 3 │ │ 3 6 5 │ 1 7 2 │ 9 4 . │ │ 8 2 . │ 9 3 4 │ . 6 1 │ ├───────┼───────┼───────┤ │ 5 . 2 │ 7 1 6 │ 8 3 9 │ │ 7 3 6 │ 8 4 9 │ 1 5 2 │ │ 9 1 4 │ 3 2 5 │ 7 . 6 │ └───────┴───────┴───────┘ Valid moves: 26, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 1 8 3 │ 2 5 . │ 6 9 7 │ │ 2 4 . │ 6 9 7 │ 5 1 8 │ │ 6 5 7 │ 4 8 1 │ 3 2 . │ ├───────┼───────┼───────┤ │ 4 7 1 │ 5 6 8 │ 2 . 3 │ │ 3 6 5 │ 1 7 2 │ 9 4 . │ │ 8 2 . │ 9 3 4 │ . 6 1 │ ├───────┼───────┼───────┤ │ 5 . 2 │ 7 1 6 │ 8 3 9 │ │ 7 3 6 │ 8 4 9 │ 1 5 2 │ │ 9 1 4 │ 3 2 5 │ 7 . 6 │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, -1) # Backtrack: no empty cells to fill ============================================================ Valid moves: 26, Final state: failed Final board: ┌───────┬───────┬───────┐ │ 1 8 3 │ 2 5 . │ 6 9 7 │ │ 2 4 . │ 6 9 7 │ 5 1 8 │ │ 6 5 7 │ 4 8 1 │ 3 2 . │ ├───────┼───────┼───────┤ │ 4 7 1 │ 5 6 8 │ 2 . 3 │ │ 3 6 5 │ 1 7 2 │ 9 4 . │ │ 8 2 . │ 9 3 4 │ . 6 1 │ ├───────┼───────┼───────┤ │ 5 . 2 │ 7 1 6 │ 8 3 9 │ │ 7 3 6 │ 8 4 9 │ 1 5 2 │ │ 9 1 4 │ 3 2 5 │ 7 . 6 │ └───────┴───────┴───────┘ Valid moves: 31, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 7 1 4 │ 2 3 5 │ 6 9 8 │ │ 6 5 2 │ 4 9 8 │ 7 1 3 │ │ 3 9 8 │ 7 1 6 │ 2 4 5 │ ├───────┼───────┼───────┤ │ 2 8 5 │ 1 6 9 │ 4 7 . │ │ 1 4 6 │ 3 5 7 │ 8 2 9 │ │ 9 3 7 │ . 4 2 │ 5 6 1 │ ├───────┼───────┼───────┤ │ 8 6 1 │ 9 7 4 │ 3 5 2 │ │ 5 7 9 │ 8 2 3 │ 1 . 4 │ │ 4 2 3 │ 5 . 1 │ 9 8 6 │ └───────┴───────┴───────┘ Valid moves: 31, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 7 1 4 │ 2 3 5 │ 6 9 8 │ │ 6 5 2 │ 4 9 8 │ 7 1 3 │ │ 3 9 8 │ 7 1 6 │ 2 4 5 │ ├───────┼───────┼───────┤ │ 2 8 5 │ 1 6 9 │ 4 7 . │ │ 1 4 6 │ 3 5 7 │ 8 2 9 │ │ 9 3 7 │ . 4 2 │ 5 6 1 │ ├───────┼───────┼───────┤ │ 8 6 1 │ 9 7 4 │ 3 5 2 │ │ 5 7 9 │ 8 2 3 │ 1 . 4 │ │ 4 2 3 │ 5 . 1 │ 9 8 6 │ └───────┴───────┴───────┘ Valid moves: 31, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 7 1 4 │ 2 3 5 │ 6 9 8 │ │ 6 5 2 │ 4 9 8 │ 7 1 3 │ │ 3 9 8 │ 7 1 6 │ 2 4 5 │ ├───────┼───────┼───────┤ │ 2 8 5 │ 1 6 9 │ 4 7 . │ │ 1 4 6 │ 3 5 7 │ 8 2 9 │ │ 9 3 7 │ . 4 2 │ 5 6 1 │ ├───────┼───────┼───────┤ │ 8 6 1 │ 9 7 4 │ 3 5 2 │ │ 5 7 9 │ 8 2 3 │ 1 . 4 │ │ 4 2 3 │ 5 . 1 │ 9 8 6 │ └───────┴───────┴───────┘ Valid moves: 31, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 7 1 4 │ 2 3 5 │ 6 9 8 │ │ 6 5 2 │ 4 9 8 │ 7 1 3 │ │ 3 9 8 │ 7 1 6 │ 2 4 5 │ ├───────┼───────┼───────┤ │ 2 8 5 │ 1 6 9 │ 4 7 . │ │ 1 4 6 │ 3 5 7 │ 8 2 9 │ │ 9 3 7 │ . 4 2 │ 5 6 1 │ ├───────┼───────┼───────┤ │ 8 6 1 │ 9 7 4 │ 3 5 2 │ │ 5 7 9 │ 8 2 3 │ 1 . 4 │ │ 4 2 3 │ 5 . 1 │ 9 8 6 │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, 0) # Backtrack: no empty cell found backtrack earlier ============================================================ Valid moves: 30, Final state: failed Final board: ┌───────┬───────┬───────┐ │ 2 5 3 │ 4 1 6 │ 8 7 9 │ │ 4 6 7 │ 3 2 8 │ 1 5 . │ │ 1 8 . │ 5 9 7 │ 4 2 3 │ ├───────┼───────┼───────┤ │ 3 2 8 │ 1 6 4 │ 5 9 7 │ │ 5 1 . │ 2 8 9 │ 6 3 4 │ │ 6 9 4 │ 7 3 5 │ 2 8 1 │ ├───────┼───────┼───────┤ │ 7 . 1 │ 6 5 2 │ 3 4 8 │ │ 8 4 5 │ . 7 3 │ 9 1 2 │ │ 9 3 2 │ 8 4 1 │ 7 6 5 │ └───────┴───────┴───────┘ Valid moves: 30, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 2 5 3 │ 4 1 6 │ 8 7 9 │ │ 4 6 7 │ 3 2 8 │ 1 5 . │ │ 1 8 . │ 5 9 7 │ 4 2 3 │ ├───────┼───────┼───────┤ │ 3 2 8 │ 1 6 4 │ 5 9 7 │ │ 5 1 . │ 2 8 9 │ 6 3 4 │ │ 6 9 4 │ 7 3 5 │ 2 8 1 │ ├───────┼───────┼───────┤ │ 7 . 1 │ 6 5 2 │ 3 4 8 │ │ 8 4 5 │ . 7 3 │ 9 1 2 │ │ 9 3 2 │ 8 4 1 │ 7 6 5 │ └───────┴───────┴───────┘ Valid moves: 30, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 2 5 3 │ 4 1 6 │ 8 7 9 │ │ 4 6 7 │ 3 2 8 │ 1 5 . │ │ 1 8 . │ 5 9 7 │ 4 2 3 │ ├───────┼───────┼───────┤ │ 3 2 8 │ 1 6 4 │ 5 9 7 │ │ 5 1 . │ 2 8 9 │ 6 3 4 │ │ 6 9 4 │ 7 3 5 │ 2 8 1 │ ├───────┼───────┼───────┤ │ 7 . 1 │ 6 5 2 │ 3 4 8 │ │ 8 4 5 │ . 7 3 │ 9 1 2 │ │ 9 3 2 │ 8 4 1 │ 7 6 5 │ └───────┴───────┴───────┘ Valid moves: 30, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 2 5 3 │ 4 1 6 │ 8 7 9 │ │ 4 6 7 │ 3 2 8 │ 1 5 . │ │ 1 8 . │ 5 9 7 │ 4 2 3 │ ├───────┼───────┼───────┤ │ 3 2 8 │ 1 6 4 │ 5 9 7 │ │ 5 1 . │ 2 8 9 │ 6 3 4 │ │ 6 9 4 │ 7 3 5 │ 2 8 1 │ ├───────┼───────┼───────┤ │ 7 . 1 │ 6 5 2 │ 3 4 8 │ │ 8 4 5 │ . 7 3 │ 9 1 2 │ │ 9 3 2 │ 8 4 1 │ 7 6 5 │ └───────┴───────┴───────┘ Valid moves: 30, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 1 7 2 │ 3 . 5 │ 6 8 9 │ │ 6 5 3 │ 8 2 9 │ 1 4 . │ │ 9 8 . │ 4 6 7 │ 3 5 2 │ ├───────┼───────┼───────┤ │ 4 1 9 │ 7 5 3 │ 2 6 8 │ │ 2 3 5 │ 6 8 4 │ 7 9 1 │ │ 7 6 8 │ 2 9 1 │ 5 . 3 │ ├───────┼───────┼───────┤ │ 8 9 6 │ . 7 2 │ 4 1 5 │ │ 5 2 1 │ 9 4 6 │ 8 3 7 │ │ 3 4 7 │ 5 1 8 │ 9 2 6 │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, -1) # Backtrack: no position found for this backtrack step ============================================================ Valid moves: 30, Final state: failed Final board: ┌───────┬───────┬───────┐ │ 1 7 2 │ 3 . 5 │ 6 8 9 │ │ 6 5 3 │ 8 2 9 │ 1 4 . │ │ 9 8 . │ 4 6 7 │ 3 5 2 │ ├───────┼───────┼───────┤ │ 4 1 9 │ 7 5 3 │ 2 6 8 │ │ 2 3 5 │ 6 8 4 │ 7 9 1 │ │ 7 6 8 │ 2 9 1 │ 5 . 3 │ ├───────┼───────┼───────┤ │ 8 9 6 │ . 7 2 │ 4 1 5 │ │ 5 2 1 │ 9 4 6 │ 8 3 7 │ │ 3 4 7 │ 5 1 8 │ 9 2 6 │ └───────┴───────┴───────┘ Valid moves: 30, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 1 7 2 │ 3 . 5 │ 6 8 9 │ │ 6 5 3 │ 8 2 9 │ 1 4 . │ │ 9 8 . │ 4 6 7 │ 3 5 2 │ ├───────┼───────┼───────┤ │ 4 1 9 │ 7 5 3 │ 2 6 8 │ │ 2 3 5 │ 6 8 4 │ 7 9 1 │ │ 7 6 8 │ 2 9 1 │ 5 . 3 │ ├───────┼───────┼───────┤ │ 8 9 6 │ . 7 2 │ 4 1 5 │ │ 5 2 1 │ 9 4 6 │ 8 3 7 │ │ 3 4 7 │ 5 1 8 │ 9 2 6 │ └───────┴───────┴───────┘ Valid moves: 30, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 1 7 2 │ 3 . 5 │ 6 8 9 │ │ 6 5 3 │ 8 2 9 │ 1 4 . │ │ 9 8 . │ 4 6 7 │ 3 5 2 │ ├───────┼───────┼───────┤ │ 4 1 9 │ 7 5 3 │ 2 6 8 │ │ 2 3 5 │ 6 8 4 │ 7 9 1 │ │ 7 6 8 │ 2 9 1 │ 5 . 3 │ ├───────┼───────┼───────┤ │ 8 9 6 │ . 7 2 │ 4 1 5 │ │ 5 2 1 │ 9 4 6 │ 8 3 7 │ │ 3 4 7 │ 5 1 8 │ 9 2 6 │ └───────┴───────┴───────┘ Valid moves: 31, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 2 7 6 │ 1 3 5 │ 4 8 9 │ │ 3 4 1 │ 2 8 9 │ 5 6 7 │ │ 8 9 5 │ 4 7 6 │ 1 2 3 │ ├───────┼───────┼───────┤ │ 4 2 7 │ 8 1 3 │ 9 5 6 │ │ 1 8 9 │ 5 2 4 │ 3 . . │ │ 6 5 3 │ 9 . 7 │ 8 4 1 │ ├───────┼───────┼───────┤ │ 5 3 8 │ 6 9 1 │ 2 7 4 │ │ 9 6 2 │ 7 4 8 │ . 1 5 │ │ 7 1 4 │ 3 5 2 │ 6 9 8 │ └───────┴───────┴───────┘ Valid moves: 31, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 2 7 6 │ 1 3 5 │ 4 8 9 │ │ 3 4 1 │ 2 8 9 │ 5 6 7 │ │ 8 9 5 │ 4 7 6 │ 1 2 3 │ ├───────┼───────┼───────┤ │ 4 2 7 │ 8 1 3 │ 9 5 6 │ │ 1 8 9 │ 5 2 4 │ 3 . . │ │ 6 5 3 │ 9 . 7 │ 8 4 1 │ ├───────┼───────┼───────┤ │ 5 3 8 │ 6 9 1 │ 2 7 4 │ │ 9 6 2 │ 7 4 8 │ . 1 5 │ │ 7 1 4 │ 3 5 2 │ 6 9 8 │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, -1) # Backtrack: no cell found ============================================================ Valid moves: 31, Final state: failed Final board: ┌───────┬───────┬───────┐ │ 2 7 6 │ 1 3 5 │ 4 8 9 │ │ 3 4 1 │ 2 8 9 │ 5 6 7 │ │ 8 9 5 │ 4 7 6 │ 1 2 3 │ ├───────┼───────┼───────┤ │ 4 2 7 │ 8 1 3 │ 9 5 6 │ │ 1 8 9 │ 5 2 4 │ 3 . . │ │ 6 5 3 │ 9 . 7 │ 8 4 1 │ ├───────┼───────┼───────┤ │ 5 3 8 │ 6 9 1 │ 2 7 4 │ │ 9 6 2 │ 7 4 8 │ . 1 5 │ │ 7 1 4 │ 3 5 2 │ 6 9 8 │ └───────┴───────┴───────┘ Valid moves: 31, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 2 7 6 │ 1 3 5 │ 4 8 9 │ │ 3 4 1 │ 2 8 9 │ 5 6 7 │ │ 8 9 5 │ 4 7 6 │ 1 2 3 │ ├───────┼───────┼───────┤ │ 4 2 7 │ 8 1 3 │ 9 5 6 │ │ 1 8 9 │ 5 2 4 │ 3 . . │ │ 6 5 3 │ 9 . 7 │ 8 4 1 │ ├───────┼───────┼───────┤ │ 5 3 8 │ 6 9 1 │ 2 7 4 │ │ 9 6 2 │ 7 4 8 │ . 1 5 │ │ 7 1 4 │ 3 5 2 │ 6 9 8 │ └───────┴───────┴───────┘ Valid moves: 33, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 6 1 8 │ 2 4 3 │ 7 9 5 │ │ 3 4 5 │ 1 9 7 │ 6 2 8 │ │ 2 9 7 │ 5 8 6 │ 1 4 3 │ ├───────┼───────┼───────┤ │ 1 5 3 │ 7 6 9 │ 4 8 2 │ │ 4 2 6 │ 8 3 5 │ 9 7 1 │ │ 7 8 9 │ 4 1 2 │ 5 3 6 │ ├───────┼───────┼───────┤ │ 9 7 2 │ 6 5 1 │ 3 . 4 │ │ 8 6 1 │ 3 7 4 │ 2 5 9 │ │ 5 3 4 │ 9 2 8 │ . 6 7 │ └───────┴───────┴───────┘ Valid moves: 33, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 6 1 8 │ 2 4 3 │ 7 9 5 │ │ 3 4 5 │ 1 9 7 │ 6 2 8 │ │ 2 9 7 │ 5 8 6 │ 1 4 3 │ ├───────┼───────┼───────┤ │ 1 5 3 │ 7 6 9 │ 4 8 2 │ │ 4 2 6 │ 8 3 5 │ 9 7 1 │ │ 7 8 9 │ 4 1 2 │ 5 3 6 │ ├───────┼───────┼───────┤ │ 9 7 2 │ 6 5 1 │ 3 . 4 │ │ 8 6 1 │ 3 7 4 │ 2 5 9 │ │ 5 3 4 │ 9 2 8 │ . 6 7 │ └───────┴───────┴───────┘ Valid moves: 33, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 6 1 8 │ 2 4 3 │ 7 9 5 │ │ 3 4 5 │ 1 9 7 │ 6 2 8 │ │ 2 9 7 │ 5 8 6 │ 1 4 3 │ ├───────┼───────┼───────┤ │ 1 5 3 │ 7 6 9 │ 4 8 2 │ │ 4 2 6 │ 8 3 5 │ 9 7 1 │ │ 7 8 9 │ 4 1 2 │ 5 3 6 │ ├───────┼───────┼───────┤ │ 9 7 2 │ 6 5 1 │ 3 . 4 │ │ 8 6 1 │ 3 7 4 │ 2 5 9 │ │ 5 3 4 │ 9 2 8 │ . 6 7 │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Row and column checks for i in range(9): if board[row][i] == number or board[i][col] == number: return False # Box checks box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, -1) # Backtrack: no cell found ============================================================ Valid moves: 33, Final state: failed Final board: ┌───────┬───────┬───────┐ │ 6 1 8 │ 2 4 3 │ 7 9 5 │ │ 3 4 5 │ 1 9 7 │ 6 2 8 │ │ 2 9 7 │ 5 8 6 │ 1 4 3 │ ├───────┼───────┼───────┤ │ 1 5 3 │ 7 6 9 │ 4 8 2 │ │ 4 2 6 │ 8 3 5 │ 9 7 1 │ │ 7 8 9 │ 4 1 2 │ 5 3 6 │ ├───────┼───────┼───────┤ │ 9 7 2 │ 6 5 1 │ 3 . 4 │ │ 8 6 1 │ 3 7 4 │ 2 5 9 │ │ 5 3 4 │ 9 2 8 │ . 6 7 │ └───────┴───────┴───────┘ Valid moves: 31, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 5 1 3 │ 2 7 4 │ 6 8 . │ │ 4 7 6 │ 3 9 8 │ 1 2 5 │ │ 2 9 8 │ 5 1 6 │ 4 3 7 │ ├───────┼───────┼───────┤ │ 6 8 7 │ 1 3 2 │ 9 5 4 │ │ 1 2 5 │ 7 4 9 │ 8 6 3 │ │ 3 4 9 │ 8 6 5 │ 2 7 1 │ ├───────┼───────┼───────┤ │ 9 3 1 │ 4 2 . │ 7 . 6 │ │ 8 6 4 │ 9 . 7 │ 5 1 2 │ │ 7 5 2 │ 6 8 1 │ 3 4 9 │ └───────┴───────┴───────┘ Valid moves: 31, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 5 1 3 │ 2 7 4 │ 6 8 . │ │ 4 7 6 │ 3 9 8 │ 1 2 5 │ │ 2 9 8 │ 5 1 6 │ 4 3 7 │ ├───────┼───────┼───────┤ │ 6 8 7 │ 1 3 2 │ 9 5 4 │ │ 1 2 5 │ 7 4 9 │ 8 6 3 │ │ 3 4 9 │ 8 6 5 │ 2 7 1 │ ├───────┼───────┼───────┤ │ 9 3 1 │ 4 2 . │ 7 . 6 │ │ 8 6 4 │ 9 . 7 │ 5 1 2 │ │ 7 5 2 │ 6 8 1 │ 3 4 9 │ └───────┴───────┴───────┘ Valid moves: 31, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 5 1 3 │ 2 7 4 │ 6 8 . │ │ 4 7 6 │ 3 9 8 │ 1 2 5 │ │ 2 9 8 │ 5 1 6 │ 4 3 7 │ ├───────┼───────┼───────┤ │ 6 8 7 │ 1 3 2 │ 9 5 4 │ │ 1 2 5 │ 7 4 9 │ 8 6 3 │ │ 3 4 9 │ 8 6 5 │ 2 7 1 │ ├───────┼───────┼───────┤ │ 9 3 1 │ 4 2 . │ 7 . 6 │ │ 8 6 4 │ 9 . 7 │ 5 1 2 │ │ 7 5 2 │ 6 8 1 │ 3 4 9 │ └───────┴───────┴───────┘ Valid moves: 31, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 5 1 3 │ 2 7 4 │ 6 8 . │ │ 4 7 6 │ 3 9 8 │ 1 2 5 │ │ 2 9 8 │ 5 1 6 │ 4 3 7 │ ├───────┼───────┼───────┤ │ 6 8 7 │ 1 3 2 │ 9 5 4 │ │ 1 2 5 │ 7 4 9 │ 8 6 3 │ │ 3 4 9 │ 8 6 5 │ 2 7 1 │ ├───────┼───────┼───────┤ │ 9 3 1 │ 4 2 . │ 7 . 6 │ │ 8 6 4 │ 9 . 7 │ 5 1 2 │ │ 7 5 2 │ 6 8 1 │ 3 4 9 │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, -1) # Backtrack: no empty cell found with a valid number ============================================================ Valid moves: 35, Final state: success Final board: ┌───────┬───────┬───────┐ │ 6 3 9 │ 1 4 2 │ 7 5 8 │ │ 8 1 4 │ 3 5 7 │ 6 2 9 │ │ 7 5 2 │ 6 9 8 │ 1 3 4 │ ├───────┼───────┼───────┤ │ 1 8 3 │ 9 2 4 │ 5 6 7 │ │ 2 4 5 │ 7 8 6 │ 3 9 1 │ │ 9 6 7 │ 5 3 1 │ 4 8 2 │ ├───────┼───────┼───────┤ │ 3 2 8 │ 4 1 5 │ 9 7 6 │ │ 5 7 1 │ 2 6 9 │ 8 4 3 │ │ 4 9 6 │ 8 7 3 │ 2 1 5 │ └───────┴───────┴───────┘ Valid moves: 35, Final state: success Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 6 3 9 │ 1 4 2 │ 7 5 8 │ │ 8 1 4 │ 3 5 7 │ 6 2 9 │ │ 7 5 2 │ 6 9 8 │ 1 3 4 │ ├───────┼───────┼───────┤ │ 1 8 3 │ 9 2 4 │ 5 6 7 │ │ 2 4 5 │ 7 8 6 │ 3 9 1 │ │ 9 6 7 │ 5 3 1 │ 4 8 2 │ ├───────┼───────┼───────┤ │ 3 2 8 │ 4 1 5 │ 9 7 6 │ │ 5 7 1 │ 2 6 9 │ 8 4 3 │ │ 4 9 6 │ 8 7 3 │ 2 1 5 │ └───────┴───────┴───────┘ Valid moves: 35, Final state: success Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 6 3 9 │ 1 4 2 │ 7 5 8 │ │ 8 1 4 │ 3 5 7 │ 6 2 9 │ │ 7 5 2 │ 6 9 8 │ 1 3 4 │ ├───────┼───────┼───────┤ │ 1 8 3 │ 9 2 4 │ 5 6 7 │ │ 2 4 5 │ 7 8 6 │ 3 9 1 │ │ 9 6 7 │ 5 3 1 │ 4 8 2 │ ├───────┼───────┼───────┤ │ 3 2 8 │ 4 1 5 │ 9 7 6 │ │ 5 7 1 │ 2 6 9 │ 8 4 3 │ │ 4 9 6 │ 8 7 3 │ 2 1 5 │ └───────┴───────┴───────┘ Valid moves: 35, Final state: success Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 6 3 9 │ 1 4 2 │ 7 5 8 │ │ 8 1 4 │ 3 5 7 │ 6 2 9 │ │ 7 5 2 │ 6 9 8 │ 1 3 4 │ ├───────┼───────┼───────┤ │ 1 8 3 │ 9 2 4 │ 5 6 7 │ │ 2 4 5 │ 7 8 6 │ 3 9 1 │ │ 9 6 7 │ 5 3 1 │ 4 8 2 │ ├───────┼───────┼───────┤ │ 3 2 8 │ 4 1 5 │ 9 7 6 │ │ 5 7 1 │ 2 6 9 │ 8 4 3 │ │ 4 9 6 │ 8 7 3 │ 2 1 5 │ └───────┴───────┴───────┘ Valid moves: 27, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 1 9 3 │ 2 6 4 │ 5 8 7 │ │ 2 4 6 │ 1 3 7 │ . . 9 │ │ 8 5 7 │ . 9 . │ 4 6 2 │ ├───────┼───────┼───────┤ │ 3 6 9 │ 4 5 8 │ 7 2 1 │ │ . 7 2 │ 6 1 3 │ 8 4 5 │ │ 5 1 8 │ 7 . 2 │ 6 9 3 │ ├───────┼───────┼───────┤ │ 7 8 1 │ 9 4 6 │ 2 5 . │ │ 6 3 5 │ 8 2 1 │ 9 7 4 │ │ 9 2 4 │ . 7 5 │ 3 1 6 │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, -1) # Backtrack: no solution found for this backtrack step ============================================================ Valid moves: 27, Final state: failed Final board: ┌───────┬───────┬───────┐ │ 1 9 3 │ 2 6 4 │ 5 8 7 │ │ 2 4 6 │ 1 3 7 │ . . 9 │ │ 8 5 7 │ . 9 . │ 4 6 2 │ ├───────┼───────┼───────┤ │ 3 6 9 │ 4 5 8 │ 7 2 1 │ │ . 7 2 │ 6 1 3 │ 8 4 5 │ │ 5 1 8 │ 7 . 2 │ 6 9 3 │ ├───────┼───────┼───────┤ │ 7 8 1 │ 9 4 6 │ 2 5 . │ │ 6 3 5 │ 8 2 1 │ 9 7 4 │ │ 9 2 4 │ . 7 5 │ 3 1 6 │ └───────┴───────┴───────┘ Valid moves: 27, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 1 9 3 │ 2 6 4 │ 5 8 7 │ │ 2 4 6 │ 1 3 7 │ . . 9 │ │ 8 5 7 │ . 9 . │ 4 6 2 │ ├───────┼───────┼───────┤ │ 3 6 9 │ 4 5 8 │ 7 2 1 │ │ . 7 2 │ 6 1 3 │ 8 4 5 │ │ 5 1 8 │ 7 . 2 │ 6 9 3 │ ├───────┼───────┼───────┤ │ 7 8 1 │ 9 4 6 │ 2 5 . │ │ 6 3 5 │ 8 2 1 │ 9 7 4 │ │ 9 2 4 │ . 7 5 │ 3 1 6 │ └───────┴───────┴───────┘ Valid moves: 27, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 1 9 3 │ 2 6 4 │ 5 8 7 │ │ 2 4 6 │ 1 3 7 │ . . 9 │ │ 8 5 7 │ . 9 . │ 4 6 2 │ ├───────┼───────┼───────┤ │ 3 6 9 │ 4 5 8 │ 7 2 1 │ │ . 7 2 │ 6 1 3 │ 8 4 5 │ │ 5 1 8 │ 7 . 2 │ 6 9 3 │ ├───────┼───────┼───────┤ │ 7 8 1 │ 9 4 6 │ 2 5 . │ │ 6 3 5 │ 8 2 1 │ 9 7 4 │ │ 9 2 4 │ . 7 5 │ 3 1 6 │ └───────┴───────┴───────┘ Valid moves: 35, Final state: success Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 8 6 1 │ 2 3 4 │ 5 7 9 │ │ 2 4 3 │ 5 7 9 │ 1 6 8 │ │ 9 7 5 │ 1 6 8 │ 3 4 2 │ ├───────┼───────┼───────┤ │ 1 2 4 │ 8 9 3 │ 6 5 7 │ │ 6 3 7 │ 4 5 2 │ 8 9 1 │ │ 5 8 9 │ 7 1 6 │ 4 2 3 │ ├───────┼───────┼───────┤ │ 4 9 6 │ 3 2 1 │ 7 8 5 │ │ 3 5 2 │ 6 8 7 │ 9 1 4 │ │ 7 1 8 │ 9 4 5 │ 2 3 6 │ └───────┴───────┴───────┘ Valid moves: 35, Final state: success Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 8 6 1 │ 2 3 4 │ 5 7 9 │ │ 2 4 3 │ 5 7 9 │ 1 6 8 │ │ 9 7 5 │ 1 6 8 │ 3 4 2 │ ├───────┼───────┼───────┤ │ 1 2 4 │ 8 9 3 │ 6 5 7 │ │ 6 3 7 │ 4 5 2 │ 8 9 1 │ │ 5 8 9 │ 7 1 6 │ 4 2 3 │ ├───────┼───────┼───────┤ │ 4 9 6 │ 3 2 1 │ 7 8 5 │ │ 3 5 2 │ 6 8 7 │ 9 1 4 │ │ 7 1 8 │ 9 4 5 │ 2 3 6 │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, -1) # Backtrack: no cell found for this backtrack step ============================================================ Valid moves: 35, Final state: success Final board: ┌───────┬───────┬───────┐ │ 8 6 1 │ 2 3 4 │ 5 7 9 │ │ 2 4 3 │ 5 7 9 │ 1 6 8 │ │ 9 7 5 │ 1 6 8 │ 3 4 2 │ ├───────┼───────┼───────┤ │ 1 2 4 │ 8 9 3 │ 6 5 7 │ │ 6 3 7 │ 4 5 2 │ 8 9 1 │ │ 5 8 9 │ 7 1 6 │ 4 2 3 │ ├───────┼───────┼───────┤ │ 4 9 6 │ 3 2 1 │ 7 8 5 │ │ 3 5 2 │ 6 8 7 │ 9 1 4 │ │ 7 1 8 │ 9 4 5 │ 2 3 6 │ └───────┴───────┴───────┘ Valid moves: 35, Final state: success Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 8 6 1 │ 2 3 4 │ 5 7 9 │ │ 2 4 3 │ 5 7 9 │ 1 6 8 │ │ 9 7 5 │ 1 6 8 │ 3 4 2 │ ├───────┼───────┼───────┤ │ 1 2 4 │ 8 9 3 │ 6 5 7 │ │ 6 3 7 │ 4 5 2 │ 8 9 1 │ │ 5 8 9 │ 7 1 6 │ 4 2 3 │ ├───────┼───────┼───────┤ │ 4 9 6 │ 3 2 1 │ 7 8 5 │ │ 3 5 2 │ 6 8 7 │ 9 1 4 │ │ 7 1 8 │ 9 4 5 │ 2 3 6 │ └───────┴───────┴───────┘ Valid moves: 28, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 3 6 . │ 1 7 2 │ 9 4 8 │ │ 9 1 8 │ 4 3 5 │ 6 7 2 │ │ 2 7 5 │ 6 8 9 │ 3 1 . │ ├───────┼───────┼───────┤ │ 1 5 2 │ 7 4 8 │ . 9 6 │ │ 6 8 4 │ 9 5 1 │ 7 2 3 │ │ . 9 7 │ 3 2 6 │ 4 8 1 │ ├───────┼───────┼───────┤ │ 5 4 6 │ 8 1 3 │ 2 . 7 │ │ 8 3 9 │ 2 6 7 │ 5 . 4 │ │ 7 2 1 │ 5 9 4 │ 8 3 . │ └───────┴───────┴───────┘ Valid moves: 28, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 3 6 . │ 1 7 2 │ 9 4 8 │ │ 9 1 8 │ 4 3 5 │ 6 7 2 │ │ 2 7 5 │ 6 8 9 │ 3 1 . │ ├───────┼───────┼───────┤ │ 1 5 2 │ 7 4 8 │ . 9 6 │ │ 6 8 4 │ 9 5 1 │ 7 2 3 │ │ . 9 7 │ 3 2 6 │ 4 8 1 │ ├───────┼───────┼───────┤ │ 5 4 6 │ 8 1 3 │ 2 . 7 │ │ 8 3 9 │ 2 6 7 │ 5 . 4 │ │ 7 2 1 │ 5 9 4 │ 8 3 . │ └───────┴───────┴───────┘ Valid moves: 28, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 3 6 . │ 1 7 2 │ 9 4 8 │ │ 9 1 8 │ 4 3 5 │ 6 7 2 │ │ 2 7 5 │ 6 8 9 │ 3 1 . │ ├───────┼───────┼───────┤ │ 1 5 2 │ 7 4 8 │ . 9 6 │ │ 6 8 4 │ 9 5 1 │ 7 2 3 │ │ . 9 7 │ 3 2 6 │ 4 8 1 │ ├───────┼───────┼───────┤ │ 5 4 6 │ 8 1 3 │ 2 . 7 │ │ 8 3 9 │ 2 6 7 │ 5 . 4 │ │ 7 2 1 │ 5 9 4 │ 8 3 . │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, -1) # Backtrack: solved or board is empty ============================================================ Valid moves: 28, Final state: failed Final board: ┌───────┬───────┬───────┐ │ 3 6 . │ 1 7 2 │ 9 4 8 │ │ 9 1 8 │ 4 3 5 │ 6 7 2 │ │ 2 7 5 │ 6 8 9 │ 3 1 . │ ├───────┼───────┼───────┤ │ 1 5 2 │ 7 4 8 │ . 9 6 │ │ 6 8 4 │ 9 5 1 │ 7 2 3 │ │ . 9 7 │ 3 2 6 │ 4 8 1 │ ├───────┼───────┼───────┤ │ 5 4 6 │ 8 1 3 │ 2 . 7 │ │ 8 3 9 │ 2 6 7 │ 5 . 4 │ │ 7 2 1 │ 5 9 4 │ 8 3 . │ └───────┴───────┴───────┘ Valid moves: 31, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 8 1 5 │ 3 2 4 │ 6 9 7 │ │ 7 4 2 │ 1 9 5 │ . 3 8 │ │ 3 6 9 │ 7 8 . │ 1 4 2 │ ├───────┼───────┼───────┤ │ 1 2 6 │ 9 5 8 │ 4 7 3 │ │ 5 7 8 │ 4 6 3 │ 9 2 1 │ │ 4 9 3 │ 2 7 1 │ 8 6 5 │ ├───────┼───────┼───────┤ │ 9 3 7 │ 8 4 . │ 2 1 6 │ │ 2 5 1 │ 6 3 9 │ 7 8 4 │ │ 6 8 4 │ . 1 2 │ 3 5 9 │ └───────┴───────┴───────┘ Valid moves: 31, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 8 1 5 │ 3 2 4 │ 6 9 7 │ │ 7 4 2 │ 1 9 5 │ . 3 8 │ │ 3 6 9 │ 7 8 . │ 1 4 2 │ ├───────┼───────┼───────┤ │ 1 2 6 │ 9 5 8 │ 4 7 3 │ │ 5 7 8 │ 4 6 3 │ 9 2 1 │ │ 4 9 3 │ 2 7 1 │ 8 6 5 │ ├───────┼───────┼───────┤ │ 9 3 7 │ 8 4 . │ 2 1 6 │ │ 2 5 1 │ 6 3 9 │ 7 8 4 │ │ 6 8 4 │ . 1 2 │ 3 5 9 │ └───────┴───────┴───────┘ Valid moves: 31, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 8 1 5 │ 3 2 4 │ 6 9 7 │ │ 7 4 2 │ 1 9 5 │ . 3 8 │ │ 3 6 9 │ 7 8 . │ 1 4 2 │ ├───────┼───────┼───────┤ │ 1 2 6 │ 9 5 8 │ 4 7 3 │ │ 5 7 8 │ 4 6 3 │ 9 2 1 │ │ 4 9 3 │ 2 7 1 │ 8 6 5 │ ├───────┼───────┼───────┤ │ 9 3 7 │ 8 4 . │ 2 1 6 │ │ 2 5 1 │ 6 3 9 │ 7 8 4 │ │ 6 8 4 │ . 1 2 │ 3 5 9 │ └───────┴───────┴───────┘ Valid moves: 31, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 8 1 5 │ 3 2 4 │ 6 9 7 │ │ 7 4 2 │ 1 9 5 │ . 3 8 │ │ 3 6 9 │ 7 8 . │ 1 4 2 │ ├───────┼───────┼───────┤ │ 1 2 6 │ 9 5 8 │ 4 7 3 │ │ 5 7 8 │ 4 6 3 │ 9 2 1 │ │ 4 9 3 │ 2 7 1 │ 8 6 5 │ ├───────┼───────┼───────┤ │ 9 3 7 │ 8 4 . │ 2 1 6 │ │ 2 5 1 │ 6 3 9 │ 7 8 4 │ │ 6 8 4 │ . 1 2 │ 3 5 9 │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, -1) # Backtrack: no cell found for this backtrack step ============================================================ Valid moves: 30, Final state: failed Final board: ┌───────┬───────┬───────┐ │ 8 9 4 │ 1 3 2 │ 5 6 7 │ │ 2 5 7 │ 6 4 8 │ 3 1 9 │ │ 6 3 1 │ 7 5 9 │ 2 4 . │ ├───────┼───────┼───────┤ │ 1 2 5 │ 3 8 6 │ 7 9 4 │ │ 4 6 8 │ 2 7 5 │ . 3 1 │ │ 3 7 9 │ 4 . 1 │ 6 5 8 │ ├───────┼───────┼───────┤ │ 7 8 3 │ 9 6 4 │ 1 2 5 │ │ 5 1 6 │ . 2 7 │ 9 8 3 │ │ 9 . 2 │ 5 1 3 │ 4 7 6 │ └───────┴───────┴───────┘ Valid moves: 30, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 8 9 4 │ 1 3 2 │ 5 6 7 │ │ 2 5 7 │ 6 4 8 │ 3 1 9 │ │ 6 3 1 │ 7 5 9 │ 2 4 . │ ├───────┼───────┼───────┤ │ 1 2 5 │ 3 8 6 │ 7 9 4 │ │ 4 6 8 │ 2 7 5 │ . 3 1 │ │ 3 7 9 │ 4 . 1 │ 6 5 8 │ ├───────┼───────┼───────┤ │ 7 8 3 │ 9 6 4 │ 1 2 5 │ │ 5 1 6 │ . 2 7 │ 9 8 3 │ │ 9 . 2 │ 5 1 3 │ 4 7 6 │ └───────┴───────┴───────┘ Valid moves: 30, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 8 9 4 │ 1 3 2 │ 5 6 7 │ │ 2 5 7 │ 6 4 8 │ 3 1 9 │ │ 6 3 1 │ 7 5 9 │ 2 4 . │ ├───────┼───────┼───────┤ │ 1 2 5 │ 3 8 6 │ 7 9 4 │ │ 4 6 8 │ 2 7 5 │ . 3 1 │ │ 3 7 9 │ 4 . 1 │ 6 5 8 │ ├───────┼───────┼───────┤ │ 7 8 3 │ 9 6 4 │ 1 2 5 │ │ 5 1 6 │ . 2 7 │ 9 8 3 │ │ 9 . 2 │ 5 1 3 │ 4 7 6 │ └───────┴───────┴───────┘ Valid moves: 30, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 8 9 4 │ 1 3 2 │ 5 6 7 │ │ 2 5 7 │ 6 4 8 │ 3 1 9 │ │ 6 3 1 │ 7 5 9 │ 2 4 . │ ├───────┼───────┼───────┤ │ 1 2 5 │ 3 8 6 │ 7 9 4 │ │ 4 6 8 │ 2 7 5 │ . 3 1 │ │ 3 7 9 │ 4 . 1 │ 6 5 8 │ ├───────┼───────┼───────┤ │ 7 8 3 │ 9 6 4 │ 1 2 5 │ │ 5 1 6 │ . 2 7 │ 9 8 3 │ │ 9 . 2 │ 5 1 3 │ 4 7 6 │ └───────┴───────┴───────┘ Valid moves: 30, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 3 9 . │ 1 2 5 │ 8 6 7 │ │ 5 7 1 │ 3 6 8 │ 2 9 4 │ │ 6 2 8 │ 7 9 4 │ 3 5 1 │ ├───────┼───────┼───────┤ │ 7 8 5 │ 6 1 2 │ 9 4 3 │ │ . 4 2 │ 8 5 9 │ 1 7 6 │ │ 1 3 6 │ 4 . 7 │ 5 8 2 │ ├───────┼───────┼───────┤ │ 2 1 7 │ 9 8 3 │ 4 . 5 │ │ 8 6 3 │ 5 4 1 │ 7 2 9 │ │ 9 5 4 │ 2 7 6 │ . 1 8 │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, -1) # Backtrack: no empty cell found for this backtracking step ============================================================ Valid moves: 30, Final state: failed Final board: ┌───────┬───────┬───────┐ │ 3 9 . │ 1 2 5 │ 8 6 7 │ │ 5 7 1 │ 3 6 8 │ 2 9 4 │ │ 6 2 8 │ 7 9 4 │ 3 5 1 │ ├───────┼───────┼───────┤ │ 7 8 5 │ 6 1 2 │ 9 4 3 │ │ . 4 2 │ 8 5 9 │ 1 7 6 │ │ 1 3 6 │ 4 . 7 │ 5 8 2 │ ├───────┼───────┼───────┤ │ 2 1 7 │ 9 8 3 │ 4 . 5 │ │ 8 6 3 │ 5 4 1 │ 7 2 9 │ │ 9 5 4 │ 2 7 6 │ . 1 8 │ └───────┴───────┴───────┘ Valid moves: 30, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 3 9 . │ 1 2 5 │ 8 6 7 │ │ 5 7 1 │ 3 6 8 │ 2 9 4 │ │ 6 2 8 │ 7 9 4 │ 3 5 1 │ ├───────┼───────┼───────┤ │ 7 8 5 │ 6 1 2 │ 9 4 3 │ │ . 4 2 │ 8 5 9 │ 1 7 6 │ │ 1 3 6 │ 4 . 7 │ 5 8 2 │ ├───────┼───────┼───────┤ │ 2 1 7 │ 9 8 3 │ 4 . 5 │ │ 8 6 3 │ 5 4 1 │ 7 2 9 │ │ 9 5 4 │ 2 7 6 │ . 1 8 │ └───────┴───────┴───────┘ Valid moves: 30, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 3 9 . │ 1 2 5 │ 8 6 7 │ │ 5 7 1 │ 3 6 8 │ 2 9 4 │ │ 6 2 8 │ 7 9 4 │ 3 5 1 │ ├───────┼───────┼───────┤ │ 7 8 5 │ 6 1 2 │ 9 4 3 │ │ . 4 2 │ 8 5 9 │ 1 7 6 │ │ 1 3 6 │ 4 . 7 │ 5 8 2 │ ├───────┼───────┼───────┤ │ 2 1 7 │ 9 8 3 │ 4 . 5 │ │ 8 6 3 │ 5 4 1 │ 7 2 9 │ │ 9 5 4 │ 2 7 6 │ . 1 8 │ └───────┴───────┴───────┘ Valid moves: 29, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 6 3 . │ 1 4 2 │ 5 7 8 │ │ 8 4 2 │ 3 7 5 │ 6 1 9 │ │ 1 5 . │ 8 6 9 │ 2 3 4 │ ├───────┼───────┼───────┤ │ 5 2 7 │ 4 3 6 │ 9 8 1 │ │ 3 6 9 │ 5 8 1 │ 7 4 2 │ │ 4 1 8 │ 9 2 7 │ 3 6 5 │ ├───────┼───────┼───────┤ │ 7 9 1 │ 6 . 4 │ 8 2 3 │ │ . 8 4 │ 2 9 3 │ 1 5 6 │ │ 2 . 3 │ 7 5 8 │ 4 9 . │ └───────┴───────┴───────┘ Valid moves: 29, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 6 3 . │ 1 4 2 │ 5 7 8 │ │ 8 4 2 │ 3 7 5 │ 6 1 9 │ │ 1 5 . │ 8 6 9 │ 2 3 4 │ ├───────┼───────┼───────┤ │ 5 2 7 │ 4 3 6 │ 9 8 1 │ │ 3 6 9 │ 5 8 1 │ 7 4 2 │ │ 4 1 8 │ 9 2 7 │ 3 6 5 │ ├───────┼───────┼───────┤ │ 7 9 1 │ 6 . 4 │ 8 2 3 │ │ . 8 4 │ 2 9 3 │ 1 5 6 │ │ 2 . 3 │ 7 5 8 │ 4 9 . │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, -1) # Backtrack: no cell found for this backtrack step ============================================================ Valid moves: 29, Final state: failed Final board: ┌───────┬───────┬───────┐ │ 6 3 . │ 1 4 2 │ 5 7 8 │ │ 8 4 2 │ 3 7 5 │ 6 1 9 │ │ 1 5 . │ 8 6 9 │ 2 3 4 │ ├───────┼───────┼───────┤ │ 5 2 7 │ 4 3 6 │ 9 8 1 │ │ 3 6 9 │ 5 8 1 │ 7 4 2 │ │ 4 1 8 │ 9 2 7 │ 3 6 5 │ ├───────┼───────┼───────┤ │ 7 9 1 │ 6 . 4 │ 8 2 3 │ │ . 8 4 │ 2 9 3 │ 1 5 6 │ │ 2 . 3 │ 7 5 8 │ 4 9 . │ └───────┴───────┴───────┘ Valid moves: 29, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 6 3 . │ 1 4 2 │ 5 7 8 │ │ 8 4 2 │ 3 7 5 │ 6 1 9 │ │ 1 5 . │ 8 6 9 │ 2 3 4 │ ├───────┼───────┼───────┤ │ 5 2 7 │ 4 3 6 │ 9 8 1 │ │ 3 6 9 │ 5 8 1 │ 7 4 2 │ │ 4 1 8 │ 9 2 7 │ 3 6 5 │ ├───────┼───────┼───────┤ │ 7 9 1 │ 6 . 4 │ 8 2 3 │ │ . 8 4 │ 2 9 3 │ 1 5 6 │ │ 2 . 3 │ 7 5 8 │ 4 9 . │ └───────┴───────┴───────┘ Valid moves: 30, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 4 1 6 │ 2 5 3 │ 8 9 7 │ │ 3 7 5 │ 1 8 9 │ 2 4 6 │ │ 2 9 8 │ 7 4 6 │ 1 5 3 │ ├───────┼───────┼───────┤ │ 1 3 9 │ 4 7 8 │ 5 6 2 │ │ 6 4 7 │ 5 9 1 │ 3 8 . │ │ 5 2 . │ 6 3 . │ 9 7 1 │ ├───────┼───────┼───────┤ │ 7 8 3 │ 9 1 4 │ 6 2 5 │ │ 9 . 2 │ 3 6 5 │ 7 1 8 │ │ . 5 1 │ 8 2 7 │ 4 3 9 │ └───────┴───────┴───────┘ Valid moves: 30, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 4 1 6 │ 2 5 3 │ 8 9 7 │ │ 3 7 5 │ 1 8 9 │ 2 4 6 │ │ 2 9 8 │ 7 4 6 │ 1 5 3 │ ├───────┼───────┼───────┤ │ 1 3 9 │ 4 7 8 │ 5 6 2 │ │ 6 4 7 │ 5 9 1 │ 3 8 . │ │ 5 2 . │ 6 3 . │ 9 7 1 │ ├───────┼───────┼───────┤ │ 7 8 3 │ 9 1 4 │ 6 2 5 │ │ 9 . 2 │ 3 6 5 │ 7 1 8 │ │ . 5 1 │ 8 2 7 │ 4 3 9 │ └───────┴───────┴───────┘ Valid moves: 30, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 4 1 6 │ 2 5 3 │ 8 9 7 │ │ 3 7 5 │ 1 8 9 │ 2 4 6 │ │ 2 9 8 │ 7 4 6 │ 1 5 3 │ ├───────┼───────┼───────┤ │ 1 3 9 │ 4 7 8 │ 5 6 2 │ │ 6 4 7 │ 5 9 1 │ 3 8 . │ │ 5 2 . │ 6 3 . │ 9 7 1 │ ├───────┼───────┼───────┤ │ 7 8 3 │ 9 1 4 │ 6 2 5 │ │ 9 . 2 │ 3 6 5 │ 7 1 8 │ │ . 5 1 │ 8 2 7 │ 4 3 9 │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, -1) # Backtrack: no empty cell found with a valid digit ============================================================ Valid moves: 30, Final state: failed Final board: ┌───────┬───────┬───────┐ │ 4 1 6 │ 2 5 3 │ 8 9 7 │ │ 3 7 5 │ 1 8 9 │ 2 4 6 │ │ 2 9 8 │ 7 4 6 │ 1 5 3 │ ├───────┼───────┼───────┤ │ 1 3 9 │ 4 7 8 │ 5 6 2 │ │ 6 4 7 │ 5 9 1 │ 3 8 . │ │ 5 2 . │ 6 3 . │ 9 7 1 │ ├───────┼───────┼───────┤ │ 7 8 3 │ 9 1 4 │ 6 2 5 │ │ 9 . 2 │ 3 6 5 │ 7 1 8 │ │ . 5 1 │ 8 2 7 │ 4 3 9 │ └───────┴───────┴───────┘ Valid moves: 29, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 2 9 8 │ 1 3 5 │ 4 7 6 │ │ 4 5 1 │ 2 6 7 │ 3 8 . │ │ 7 3 6 │ 4 8 9 │ 1 2 5 │ ├───────┼───────┼───────┤ │ 1 6 5 │ 8 2 3 │ . 4 7 │ │ 3 2 7 │ . 4 . │ 6 1 9 │ │ 8 4 9 │ 6 7 1 │ 5 3 2 │ ├───────┼───────┼───────┤ │ 6 7 4 │ 3 9 8 │ 2 5 1 │ │ 9 1 2 │ 5 . 6 │ 7 . 3 │ │ 5 8 3 │ 7 1 2 │ 9 6 4 │ └───────┴───────┴───────┘ Valid moves: 29, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 2 9 8 │ 1 3 5 │ 4 7 6 │ │ 4 5 1 │ 2 6 7 │ 3 8 . │ │ 7 3 6 │ 4 8 9 │ 1 2 5 │ ├───────┼───────┼───────┤ │ 1 6 5 │ 8 2 3 │ . 4 7 │ │ 3 2 7 │ . 4 . │ 6 1 9 │ │ 8 4 9 │ 6 7 1 │ 5 3 2 │ ├───────┼───────┼───────┤ │ 6 7 4 │ 3 9 8 │ 2 5 1 │ │ 9 1 2 │ 5 . 6 │ 7 . 3 │ │ 5 8 3 │ 7 1 2 │ 9 6 4 │ └───────┴───────┴───────┘ Valid moves: 29, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 2 9 8 │ 1 3 5 │ 4 7 6 │ │ 4 5 1 │ 2 6 7 │ 3 8 . │ │ 7 3 6 │ 4 8 9 │ 1 2 5 │ ├───────┼───────┼───────┤ │ 1 6 5 │ 8 2 3 │ . 4 7 │ │ 3 2 7 │ . 4 . │ 6 1 9 │ │ 8 4 9 │ 6 7 1 │ 5 3 2 │ ├───────┼───────┼───────┤ │ 6 7 4 │ 3 9 8 │ 2 5 1 │ │ 9 1 2 │ 5 . 6 │ 7 . 3 │ │ 5 8 3 │ 7 1 2 │ 9 6 4 │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, -1) # Backtrack: no empty cell found with a valid digit ============================================================ Valid moves: 24, Final state: failed Final board: ┌───────┬───────┬───────┐ │ 8 1 4 │ 6 2 5 │ 7 . 9 │ │ 9 2 6 │ 7 3 1 │ 5 . 8 │ │ 7 3 . │ . 9 8 │ 4 2 6 │ ├───────┼───────┼───────┤ │ 2 4 1 │ . 7 6 │ 9 3 5 │ │ 3 7 5 │ 2 8 4 │ 1 6 . │ │ 6 . 9 │ 3 5 . │ 8 4 2 │ ├───────┼───────┼───────┤ │ 4 8 2 │ 9 6 7 │ 3 5 1 │ │ 5 6 7 │ 4 . 3 │ 2 9 . │ │ . 9 3 │ 5 1 2 │ 6 8 4 │ └───────┴───────┴───────┘ Valid moves: 24, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 8 1 4 │ 6 2 5 │ 7 . 9 │ │ 9 2 6 │ 7 3 1 │ 5 . 8 │ │ 7 3 . │ . 9 8 │ 4 2 6 │ ├───────┼───────┼───────┤ │ 2 4 1 │ . 7 6 │ 9 3 5 │ │ 3 7 5 │ 2 8 4 │ 1 6 . │ │ 6 . 9 │ 3 5 . │ 8 4 2 │ ├───────┼───────┼───────┤ │ 4 8 2 │ 9 6 7 │ 3 5 1 │ │ 5 6 7 │ 4 . 3 │ 2 9 . │ │ . 9 3 │ 5 1 2 │ 6 8 4 │ └───────┴───────┴───────┘ Valid moves: 24, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 8 1 4 │ 6 2 5 │ 7 . 9 │ │ 9 2 6 │ 7 3 1 │ 5 . 8 │ │ 7 3 . │ . 9 8 │ 4 2 6 │ ├───────┼───────┼───────┤ │ 2 4 1 │ . 7 6 │ 9 3 5 │ │ 3 7 5 │ 2 8 4 │ 1 6 . │ │ 6 . 9 │ 3 5 . │ 8 4 2 │ ├───────┼───────┼───────┤ │ 4 8 2 │ 9 6 7 │ 3 5 1 │ │ 5 6 7 │ 4 . 3 │ 2 9 . │ │ . 9 3 │ 5 1 2 │ 6 8 4 │ └───────┴───────┴───────┘ Valid moves: 24, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 8 1 4 │ 6 2 5 │ 7 . 9 │ │ 9 2 6 │ 7 3 1 │ 5 . 8 │ │ 7 3 . │ . 9 8 │ 4 2 6 │ ├───────┼───────┼───────┤ │ 2 4 1 │ . 7 6 │ 9 3 5 │ │ 3 7 5 │ 2 8 4 │ 1 6 . │ │ 6 . 9 │ 3 5 . │ 8 4 2 │ ├───────┼───────┼───────┤ │ 4 8 2 │ 9 6 7 │ 3 5 1 │ │ 5 6 7 │ 4 . 3 │ 2 9 . │ │ . 9 3 │ 5 1 2 │ 6 8 4 │ └───────┴───────┴───────┘ Valid moves: 33, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 4 2 6 │ 1 3 5 │ 7 8 9 │ │ 1 3 9 │ 2 6 8 │ 4 . 5 │ │ 5 . 8 │ 4 7 9 │ 1 6 2 │ ├───────┼───────┼───────┤ │ 2 9 4 │ 3 8 1 │ 6 5 7 │ │ 3 8 7 │ 5 4 6 │ 2 9 1 │ │ 6 1 5 │ 7 9 2 │ 8 4 3 │ ├───────┼───────┼───────┤ │ 7 6 2 │ 9 5 4 │ 3 1 8 │ │ 9 4 1 │ 8 2 3 │ 5 7 6 │ │ 8 5 3 │ 6 1 7 │ 9 2 4 │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, -1) # Backtrack: no solution found for backtracking ============================================================ Valid moves: 33, Final state: failed Final board: ┌───────┬───────┬───────┐ │ 4 2 6 │ 1 3 5 │ 7 8 9 │ │ 1 3 9 │ 2 6 8 │ 4 . 5 │ │ 5 . 8 │ 4 7 9 │ 1 6 2 │ ├───────┼───────┼───────┤ │ 2 9 4 │ 3 8 1 │ 6 5 7 │ │ 3 8 7 │ 5 4 6 │ 2 9 1 │ │ 6 1 5 │ 7 9 2 │ 8 4 3 │ ├───────┼───────┼───────┤ │ 7 6 2 │ 9 5 4 │ 3 1 8 │ │ 9 4 1 │ 8 2 3 │ 5 7 6 │ │ 8 5 3 │ 6 1 7 │ 9 2 4 │ └───────┴───────┴───────┘ Valid moves: 33, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 4 2 6 │ 1 3 5 │ 7 8 9 │ │ 1 3 9 │ 2 6 8 │ 4 . 5 │ │ 5 . 8 │ 4 7 9 │ 1 6 2 │ ├───────┼───────┼───────┤ │ 2 9 4 │ 3 8 1 │ 6 5 7 │ │ 3 8 7 │ 5 4 6 │ 2 9 1 │ │ 6 1 5 │ 7 9 2 │ 8 4 3 │ ├───────┼───────┼───────┤ │ 7 6 2 │ 9 5 4 │ 3 1 8 │ │ 9 4 1 │ 8 2 3 │ 5 7 6 │ │ 8 5 3 │ 6 1 7 │ 9 2 4 │ └───────┴───────┴───────┘ Valid moves: 33, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 4 2 6 │ 1 3 5 │ 7 8 9 │ │ 1 3 9 │ 2 6 8 │ 4 . 5 │ │ 5 . 8 │ 4 7 9 │ 1 6 2 │ ├───────┼───────┼───────┤ │ 2 9 4 │ 3 8 1 │ 6 5 7 │ │ 3 8 7 │ 5 4 6 │ 2 9 1 │ │ 6 1 5 │ 7 9 2 │ 8 4 3 │ ├───────┼───────┼───────┤ │ 7 6 2 │ 9 5 4 │ 3 1 8 │ │ 9 4 1 │ 8 2 3 │ 5 7 6 │ │ 8 5 3 │ 6 1 7 │ 9 2 4 │ └───────┴───────┴───────┘ Valid moves: 33, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 4 5 1 │ 2 6 3 │ 7 8 9 │ │ 3 7 9 │ 4 8 1 │ 5 6 2 │ │ 8 2 6 │ 5 7 9 │ 4 3 1 │ ├───────┼───────┼───────┤ │ 2 6 5 │ 1 4 8 │ 9 7 3 │ │ 9 3 4 │ 7 5 2 │ 6 1 8 │ │ 7 1 8 │ 9 3 6 │ 2 5 4 │ ├───────┼───────┼───────┤ │ 1 4 3 │ 6 2 5 │ 8 9 7 │ │ 5 9 2 │ 8 1 4 │ 3 . 6 │ │ 6 8 7 │ 3 9 . │ 1 2 5 │ └───────┴───────┴───────┘ Valid moves: 33, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 4 5 1 │ 2 6 3 │ 7 8 9 │ │ 3 7 9 │ 4 8 1 │ 5 6 2 │ │ 8 2 6 │ 5 7 9 │ 4 3 1 │ ├───────┼───────┼───────┤ │ 2 6 5 │ 1 4 8 │ 9 7 3 │ │ 9 3 4 │ 7 5 2 │ 6 1 8 │ │ 7 1 8 │ 9 3 6 │ 2 5 4 │ ├───────┼───────┼───────┤ │ 1 4 3 │ 6 2 5 │ 8 9 7 │ │ 5 9 2 │ 8 1 4 │ 3 . 6 │ │ 6 8 7 │ 3 9 . │ 1 2 5 │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, -1) # Backtrack: no solution found for current backtrack step ============================================================ Valid moves: 33, Final state: failed Final board: ┌───────┬───────┬───────┐ │ 4 5 1 │ 2 6 3 │ 7 8 9 │ │ 3 7 9 │ 4 8 1 │ 5 6 2 │ │ 8 2 6 │ 5 7 9 │ 4 3 1 │ ├───────┼───────┼───────┤ │ 2 6 5 │ 1 4 8 │ 9 7 3 │ │ 9 3 4 │ 7 5 2 │ 6 1 8 │ │ 7 1 8 │ 9 3 6 │ 2 5 4 │ ├───────┼───────┼───────┤ │ 1 4 3 │ 6 2 5 │ 8 9 7 │ │ 5 9 2 │ 8 1 4 │ 3 . 6 │ │ 6 8 7 │ 3 9 . │ 1 2 5 │ └───────┴───────┴───────┘ Valid moves: 33, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 4 5 1 │ 2 6 3 │ 7 8 9 │ │ 3 7 9 │ 4 8 1 │ 5 6 2 │ │ 8 2 6 │ 5 7 9 │ 4 3 1 │ ├───────┼───────┼───────┤ │ 2 6 5 │ 1 4 8 │ 9 7 3 │ │ 9 3 4 │ 7 5 2 │ 6 1 8 │ │ 7 1 8 │ 9 3 6 │ 2 5 4 │ ├───────┼───────┼───────┤ │ 1 4 3 │ 6 2 5 │ 8 9 7 │ │ 5 9 2 │ 8 1 4 │ 3 . 6 │ │ 6 8 7 │ 3 9 . │ 1 2 5 │ └───────┴───────┴───────┘ Valid moves: 29, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 5 7 4 │ 1 2 3 │ 8 9 6 │ │ 3 2 6 │ 4 9 . │ 5 1 7 │ │ 1 9 . │ 5 8 7 │ 2 4 3 │ ├───────┼───────┼───────┤ │ 4 1 3 │ 8 7 2 │ 9 . 5 │ │ 2 5 7 │ . 1 4 │ 3 6 8 │ │ 8 6 9 │ . 3 5 │ 4 7 1 │ ├───────┼───────┼───────┤ │ 9 8 5 │ 2 6 . │ 1 3 4 │ │ 6 3 1 │ 9 4 8 │ 7 5 2 │ │ 7 4 2 │ 3 5 1 │ 6 8 9 │ └───────┴───────┴───────┘ Valid moves: 29, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 5 7 4 │ 1 2 3 │ 8 9 6 │ │ 3 2 6 │ 4 9 . │ 5 1 7 │ │ 1 9 . │ 5 8 7 │ 2 4 3 │ ├───────┼───────┼───────┤ │ 4 1 3 │ 8 7 2 │ 9 . 5 │ │ 2 5 7 │ . 1 4 │ 3 6 8 │ │ 8 6 9 │ . 3 5 │ 4 7 1 │ ├───────┼───────┼───────┤ │ 9 8 5 │ 2 6 . │ 1 3 4 │ │ 6 3 1 │ 9 4 8 │ 7 5 2 │ │ 7 4 2 │ 3 5 1 │ 6 8 9 │ └───────┴───────┴───────┘ Valid moves: 29, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 5 7 4 │ 1 2 3 │ 8 9 6 │ │ 3 2 6 │ 4 9 . │ 5 1 7 │ │ 1 9 . │ 5 8 7 │ 2 4 3 │ ├───────┼───────┼───────┤ │ 4 1 3 │ 8 7 2 │ 9 . 5 │ │ 2 5 7 │ . 1 4 │ 3 6 8 │ │ 8 6 9 │ . 3 5 │ 4 7 1 │ ├───────┼───────┼───────┤ │ 9 8 5 │ 2 6 . │ 1 3 4 │ │ 6 3 1 │ 9 4 8 │ 7 5 2 │ │ 7 4 2 │ 3 5 1 │ 6 8 9 │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, -1) # Backtrack: no solution found in this branch ============================================================ Valid moves: 29, Final state: failed Final board: ┌───────┬───────┬───────┐ │ 5 7 4 │ 1 2 3 │ 8 9 6 │ │ 3 2 6 │ 4 9 . │ 5 1 7 │ │ 1 9 . │ 5 8 7 │ 2 4 3 │ ├───────┼───────┼───────┤ │ 4 1 3 │ 8 7 2 │ 9 . 5 │ │ 2 5 7 │ . 1 4 │ 3 6 8 │ │ 8 6 9 │ . 3 5 │ 4 7 1 │ ├───────┼───────┼───────┤ │ 9 8 5 │ 2 6 . │ 1 3 4 │ │ 6 3 1 │ 9 4 8 │ 7 5 2 │ │ 7 4 2 │ 3 5 1 │ 6 8 9 │ └───────┴───────┴───────┘ Valid moves: 29, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 2 1 5 │ 3 4 6 │ 7 8 9 │ │ 4 3 7 │ 1 8 9 │ 5 . 2 │ │ 8 9 . │ 5 2 7 │ 1 6 4 │ ├───────┼───────┼───────┤ │ 3 2 4 │ . 5 1 │ 9 7 8 │ │ 5 7 6 │ 4 9 8 │ 2 3 1 │ │ 1 . 8 │ 2 7 3 │ 4 5 6 │ ├───────┼───────┼───────┤ │ 6 4 3 │ 7 1 2 │ 8 9 5 │ │ 7 8 1 │ 6 . 5 │ . 4 3 │ │ 9 5 2 │ 8 3 4 │ 6 1 7 │ └───────┴───────┴───────┘ Valid moves: 29, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 2 1 5 │ 3 4 6 │ 7 8 9 │ │ 4 3 7 │ 1 8 9 │ 5 . 2 │ │ 8 9 . │ 5 2 7 │ 1 6 4 │ ├───────┼───────┼───────┤ │ 3 2 4 │ . 5 1 │ 9 7 8 │ │ 5 7 6 │ 4 9 8 │ 2 3 1 │ │ 1 . 8 │ 2 7 3 │ 4 5 6 │ ├───────┼───────┼───────┤ │ 6 4 3 │ 7 1 2 │ 8 9 5 │ │ 7 8 1 │ 6 . 5 │ . 4 3 │ │ 9 5 2 │ 8 3 4 │ 6 1 7 │ └───────┴───────┴───────┘ Valid moves: 29, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 2 1 5 │ 3 4 6 │ 7 8 9 │ │ 4 3 7 │ 1 8 9 │ 5 . 2 │ │ 8 9 . │ 5 2 7 │ 1 6 4 │ ├───────┼───────┼───────┤ │ 3 2 4 │ . 5 1 │ 9 7 8 │ │ 5 7 6 │ 4 9 8 │ 2 3 1 │ │ 1 . 8 │ 2 7 3 │ 4 5 6 │ ├───────┼───────┼───────┤ │ 6 4 3 │ 7 1 2 │ 8 9 5 │ │ 7 8 1 │ 6 . 5 │ . 4 3 │ │ 9 5 2 │ 8 3 4 │ 6 1 7 │ └───────┴───────┴───────┘ Valid moves: 29, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 2 1 5 │ 3 4 6 │ 7 8 9 │ │ 4 3 7 │ 1 8 9 │ 5 . 2 │ │ 8 9 . │ 5 2 7 │ 1 6 4 │ ├───────┼───────┼───────┤ │ 3 2 4 │ . 5 1 │ 9 7 8 │ │ 5 7 6 │ 4 9 8 │ 2 3 1 │ │ 1 . 8 │ 2 7 3 │ 4 5 6 │ ├───────┼───────┼───────┤ │ 6 4 3 │ 7 1 2 │ 8 9 5 │ │ 7 8 1 │ 6 . 5 │ . 4 3 │ │ 9 5 2 │ 8 3 4 │ 6 1 7 │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, -1) # Backtrack: no move found ============================================================ Valid moves: 30, Final state: failed Final board: ┌───────┬───────┬───────┐ │ 6 9 8 │ 3 1 2 │ 5 7 4 │ │ 5 3 4 │ 6 8 7 │ 1 9 2 │ │ 7 1 2 │ 5 4 9 │ 3 8 6 │ ├───────┼───────┼───────┤ │ 1 2 3 │ 4 5 8 │ 7 6 9 │ │ 9 6 7 │ 2 . 3 │ 4 5 1 │ │ 4 8 5 │ 1 7 6 │ 2 3 . │ ├───────┼───────┼───────┤ │ 8 7 6 │ 9 2 4 │ . 1 3 │ │ 2 4 1 │ 8 3 5 │ 9 . 7 │ │ 3 5 9 │ 7 6 1 │ . 2 8 │ └───────┴───────┴───────┘ Valid moves: 30, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 6 9 8 │ 3 1 2 │ 5 7 4 │ │ 5 3 4 │ 6 8 7 │ 1 9 2 │ │ 7 1 2 │ 5 4 9 │ 3 8 6 │ ├───────┼───────┼───────┤ │ 1 2 3 │ 4 5 8 │ 7 6 9 │ │ 9 6 7 │ 2 . 3 │ 4 5 1 │ │ 4 8 5 │ 1 7 6 │ 2 3 . │ ├───────┼───────┼───────┤ │ 8 7 6 │ 9 2 4 │ . 1 3 │ │ 2 4 1 │ 8 3 5 │ 9 . 7 │ │ 3 5 9 │ 7 6 1 │ . 2 8 │ └───────┴───────┴───────┘ Valid moves: 30, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 6 9 8 │ 3 1 2 │ 5 7 4 │ │ 5 3 4 │ 6 8 7 │ 1 9 2 │ │ 7 1 2 │ 5 4 9 │ 3 8 6 │ ├───────┼───────┼───────┤ │ 1 2 3 │ 4 5 8 │ 7 6 9 │ │ 9 6 7 │ 2 . 3 │ 4 5 1 │ │ 4 8 5 │ 1 7 6 │ 2 3 . │ ├───────┼───────┼───────┤ │ 8 7 6 │ 9 2 4 │ . 1 3 │ │ 2 4 1 │ 8 3 5 │ 9 . 7 │ │ 3 5 9 │ 7 6 1 │ . 2 8 │ └───────┴───────┴───────┘ Valid moves: 30, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 6 9 8 │ 3 1 2 │ 5 7 4 │ │ 5 3 4 │ 6 8 7 │ 1 9 2 │ │ 7 1 2 │ 5 4 9 │ 3 8 6 │ ├───────┼───────┼───────┤ │ 1 2 3 │ 4 5 8 │ 7 6 9 │ │ 9 6 7 │ 2 . 3 │ 4 5 1 │ │ 4 8 5 │ 1 7 6 │ 2 3 . │ ├───────┼───────┼───────┤ │ 8 7 6 │ 9 2 4 │ . 1 3 │ │ 2 4 1 │ 8 3 5 │ 9 . 7 │ │ 3 5 9 │ 7 6 1 │ . 2 8 │ └───────┴───────┴───────┘ Valid moves: 28, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 5 1 6 │ 2 . 3 │ 4 9 7 │ │ . 2 9 │ 4 7 8 │ 3 5 6 │ │ 4 3 7 │ 5 6 9 │ 1 2 8 │ ├───────┼───────┼───────┤ │ 1 9 2 │ 8 4 5 │ 7 6 3 │ │ 6 4 8 │ 3 2 7 │ 9 1 5 │ │ 3 7 5 │ 1 9 6 │ 2 8 4 │ ├───────┼───────┼───────┤ │ 8 5 1 │ 9 3 2 │ . 7 . │ │ 7 6 4 │ . 5 . │ 8 3 9 │ │ 9 . 3 │ 7 8 1 │ 6 4 2 │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, -1) # Backtrack: board is solved ============================================================ Valid moves: 28, Final state: failed Final board: ┌───────┬───────┬───────┐ │ 5 1 6 │ 2 . 3 │ 4 9 7 │ │ . 2 9 │ 4 7 8 │ 3 5 6 │ │ 4 3 7 │ 5 6 9 │ 1 2 8 │ ├───────┼───────┼───────┤ │ 1 9 2 │ 8 4 5 │ 7 6 3 │ │ 6 4 8 │ 3 2 7 │ 9 1 5 │ │ 3 7 5 │ 1 9 6 │ 2 8 4 │ ├───────┼───────┼───────┤ │ 8 5 1 │ 9 3 2 │ . 7 . │ │ 7 6 4 │ . 5 . │ 8 3 9 │ │ 9 . 3 │ 7 8 1 │ 6 4 2 │ └───────┴───────┴───────┘ Valid moves: 28, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 5 1 6 │ 2 . 3 │ 4 9 7 │ │ . 2 9 │ 4 7 8 │ 3 5 6 │ │ 4 3 7 │ 5 6 9 │ 1 2 8 │ ├───────┼───────┼───────┤ │ 1 9 2 │ 8 4 5 │ 7 6 3 │ │ 6 4 8 │ 3 2 7 │ 9 1 5 │ │ 3 7 5 │ 1 9 6 │ 2 8 4 │ ├───────┼───────┼───────┤ │ 8 5 1 │ 9 3 2 │ . 7 . │ │ 7 6 4 │ . 5 . │ 8 3 9 │ │ 9 . 3 │ 7 8 1 │ 6 4 2 │ └───────┴───────┴───────┘ Valid moves: 28, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 5 1 6 │ 2 . 3 │ 4 9 7 │ │ . 2 9 │ 4 7 8 │ 3 5 6 │ │ 4 3 7 │ 5 6 9 │ 1 2 8 │ ├───────┼───────┼───────┤ │ 1 9 2 │ 8 4 5 │ 7 6 3 │ │ 6 4 8 │ 3 2 7 │ 9 1 5 │ │ 3 7 5 │ 1 9 6 │ 2 8 4 │ ├───────┼───────┼───────┤ │ 8 5 1 │ 9 3 2 │ . 7 . │ │ 7 6 4 │ . 5 . │ 8 3 9 │ │ 9 . 3 │ 7 8 1 │ 6 4 2 │ └───────┴───────┴───────┘ Valid moves: 30, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 2 3 5 │ 1 6 9 │ 4 7 8 │ │ 6 1 4 │ 2 3 7 │ . 5 9 │ │ 7 9 8 │ 5 4 . │ 1 6 3 │ ├───────┼───────┼───────┤ │ 1 8 3 │ 6 9 2 │ 5 4 7 │ │ 4 2 7 │ 3 5 1 │ 8 9 6 │ │ 9 5 6 │ 8 7 4 │ 3 2 1 │ ├───────┼───────┼───────┤ │ 3 7 9 │ . 2 5 │ 6 1 4 │ │ 5 4 1 │ 9 . 3 │ 7 8 2 │ │ . 6 2 │ 4 1 8 │ 9 3 5 │ └───────┴───────┴───────┘ Valid moves: 30, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 2 3 5 │ 1 6 9 │ 4 7 8 │ │ 6 1 4 │ 2 3 7 │ . 5 9 │ │ 7 9 8 │ 5 4 . │ 1 6 3 │ ├───────┼───────┼───────┤ │ 1 8 3 │ 6 9 2 │ 5 4 7 │ │ 4 2 7 │ 3 5 1 │ 8 9 6 │ │ 9 5 6 │ 8 7 4 │ 3 2 1 │ ├───────┼───────┼───────┤ │ 3 7 9 │ . 2 5 │ 6 1 4 │ │ 5 4 1 │ 9 . 3 │ 7 8 2 │ │ . 6 2 │ 4 1 8 │ 9 3 5 │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, -1) # Backtrack: no cell found for this backtrack step ============================================================ Valid moves: 30, Final state: failed Final board: ┌───────┬───────┬───────┐ │ 2 3 5 │ 1 6 9 │ 4 7 8 │ │ 6 1 4 │ 2 3 7 │ . 5 9 │ │ 7 9 8 │ 5 4 . │ 1 6 3 │ ├───────┼───────┼───────┤ │ 1 8 3 │ 6 9 2 │ 5 4 7 │ │ 4 2 7 │ 3 5 1 │ 8 9 6 │ │ 9 5 6 │ 8 7 4 │ 3 2 1 │ ├───────┼───────┼───────┤ │ 3 7 9 │ . 2 5 │ 6 1 4 │ │ 5 4 1 │ 9 . 3 │ 7 8 2 │ │ . 6 2 │ 4 1 8 │ 9 3 5 │ └───────┴───────┴───────┘ Valid moves: 30, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 2 3 5 │ 1 6 9 │ 4 7 8 │ │ 6 1 4 │ 2 3 7 │ . 5 9 │ │ 7 9 8 │ 5 4 . │ 1 6 3 │ ├───────┼───────┼───────┤ │ 1 8 3 │ 6 9 2 │ 5 4 7 │ │ 4 2 7 │ 3 5 1 │ 8 9 6 │ │ 9 5 6 │ 8 7 4 │ 3 2 1 │ ├───────┼───────┼───────┤ │ 3 7 9 │ . 2 5 │ 6 1 4 │ │ 5 4 1 │ 9 . 3 │ 7 8 2 │ │ . 6 2 │ 4 1 8 │ 9 3 5 │ └───────┴───────┴───────┘ Valid moves: 33, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 1 9 5 │ 2 3 4 │ 7 8 6 │ │ 3 6 2 │ 1 7 8 │ 4 5 9 │ │ 4 8 7 │ 9 6 5 │ 1 2 3 │ ├───────┼───────┼───────┤ │ 2 1 9 │ 7 4 3 │ 8 6 5 │ │ 5 3 8 │ 6 1 9 │ 2 7 4 │ │ 6 7 4 │ 8 5 2 │ 9 3 1 │ ├───────┼───────┼───────┤ │ 7 2 6 │ 5 8 1 │ 3 9 . │ │ 9 5 1 │ 3 2 7 │ 6 4 8 │ │ 8 4 3 │ . 9 6 │ 5 1 7 │ └───────┴───────┴───────┘ Valid moves: 33, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 1 9 5 │ 2 3 4 │ 7 8 6 │ │ 3 6 2 │ 1 7 8 │ 4 5 9 │ │ 4 8 7 │ 9 6 5 │ 1 2 3 │ ├───────┼───────┼───────┤ │ 2 1 9 │ 7 4 3 │ 8 6 5 │ │ 5 3 8 │ 6 1 9 │ 2 7 4 │ │ 6 7 4 │ 8 5 2 │ 9 3 1 │ ├───────┼───────┼───────┤ │ 7 2 6 │ 5 8 1 │ 3 9 . │ │ 9 5 1 │ 3 2 7 │ 6 4 8 │ │ 8 4 3 │ . 9 6 │ 5 1 7 │ └───────┴───────┴───────┘ Valid moves: 33, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 1 9 5 │ 2 3 4 │ 7 8 6 │ │ 3 6 2 │ 1 7 8 │ 4 5 9 │ │ 4 8 7 │ 9 6 5 │ 1 2 3 │ ├───────┼───────┼───────┤ │ 2 1 9 │ 7 4 3 │ 8 6 5 │ │ 5 3 8 │ 6 1 9 │ 2 7 4 │ │ 6 7 4 │ 8 5 2 │ 9 3 1 │ ├───────┼───────┼───────┤ │ 7 2 6 │ 5 8 1 │ 3 9 . │ │ 9 5 1 │ 3 2 7 │ 6 4 8 │ │ 8 4 3 │ . 9 6 │ 5 1 7 │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, -1) # Backtrack: no solution found for current board state ============================================================ Valid moves: 33, Final state: failed Final board: ┌───────┬───────┬───────┐ │ 1 9 5 │ 2 3 4 │ 7 8 6 │ │ 3 6 2 │ 1 7 8 │ 4 5 9 │ │ 4 8 7 │ 9 6 5 │ 1 2 3 │ ├───────┼───────┼───────┤ │ 2 1 9 │ 7 4 3 │ 8 6 5 │ │ 5 3 8 │ 6 1 9 │ 2 7 4 │ │ 6 7 4 │ 8 5 2 │ 9 3 1 │ ├───────┼───────┼───────┤ │ 7 2 6 │ 5 8 1 │ 3 9 . │ │ 9 5 1 │ 3 2 7 │ 6 4 8 │ │ 8 4 3 │ . 9 6 │ 5 1 7 │ └───────┴───────┴───────┘ Valid moves: 26, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 6 3 7 │ 1 2 4 │ 5 9 8 │ │ 9 2 8 │ 3 5 6 │ 4 1 7 │ │ 1 4 . │ 7 8 . │ 2 6 3 │ ├───────┼───────┼───────┤ │ . 7 9 │ 2 6 5 │ 8 3 4 │ │ 3 8 1 │ 4 7 . │ 6 2 5 │ │ 4 5 2 │ 9 . 3 │ . 7 1 │ ├───────┼───────┼───────┤ │ 2 1 3 │ 5 4 8 │ 7 . 6 │ │ 7 6 4 │ . 3 9 │ 1 5 2 │ │ 8 9 5 │ 6 1 2 │ 3 4 . │ └───────┴───────┴───────┘ Valid moves: 26, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 6 3 7 │ 1 2 4 │ 5 9 8 │ │ 9 2 8 │ 3 5 6 │ 4 1 7 │ │ 1 4 . │ 7 8 . │ 2 6 3 │ ├───────┼───────┼───────┤ │ . 7 9 │ 2 6 5 │ 8 3 4 │ │ 3 8 1 │ 4 7 . │ 6 2 5 │ │ 4 5 2 │ 9 . 3 │ . 7 1 │ ├───────┼───────┼───────┤ │ 2 1 3 │ 5 4 8 │ 7 . 6 │ │ 7 6 4 │ . 3 9 │ 1 5 2 │ │ 8 9 5 │ 6 1 2 │ 3 4 . │ └───────┴───────┴───────┘ Valid moves: 26, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 6 3 7 │ 1 2 4 │ 5 9 8 │ │ 9 2 8 │ 3 5 6 │ 4 1 7 │ │ 1 4 . │ 7 8 . │ 2 6 3 │ ├───────┼───────┼───────┤ │ . 7 9 │ 2 6 5 │ 8 3 4 │ │ 3 8 1 │ 4 7 . │ 6 2 5 │ │ 4 5 2 │ 9 . 3 │ . 7 1 │ ├───────┼───────┼───────┤ │ 2 1 3 │ 5 4 8 │ 7 . 6 │ │ 7 6 4 │ . 3 9 │ 1 5 2 │ │ 8 9 5 │ 6 1 2 │ 3 4 . │ └───────┴───────┴───────┘ Valid moves: 26, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 6 3 7 │ 1 2 4 │ 5 9 8 │ │ 9 2 8 │ 3 5 6 │ 4 1 7 │ │ 1 4 . │ 7 8 . │ 2 6 3 │ ├───────┼───────┼───────┤ │ . 7 9 │ 2 6 5 │ 8 3 4 │ │ 3 8 1 │ 4 7 . │ 6 2 5 │ │ 4 5 2 │ 9 . 3 │ . 7 1 │ ├───────┼───────┼───────┤ │ 2 1 3 │ 5 4 8 │ 7 . 6 │ │ 7 6 4 │ . 3 9 │ 1 5 2 │ │ 8 9 5 │ 6 1 2 │ 3 4 . │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, -1) # Backtrack: no solution found in this backtracking step ============================================================ Valid moves: 29, Final state: failed Final board: ┌───────┬───────┬───────┐ │ 6 3 5 │ 1 2 7 │ 8 4 9 │ │ 8 1 4 │ 3 5 9 │ 2 6 7 │ │ 9 2 7 │ 4 6 8 │ 3 1 5 │ ├───────┼───────┼───────┤ │ 2 4 3 │ 7 1 . │ 6 5 8 │ │ 7 6 8 │ 2 4 5 │ 9 3 1 │ │ 1 5 9 │ 6 3 . │ 7 2 4 │ ├───────┼───────┼───────┤ │ 3 8 2 │ 5 7 4 │ 1 . 6 │ │ 4 7 1 │ 8 . 3 │ 5 9 2 │ │ 5 . 6 │ 9 . 2 │ 4 7 3 │ └───────┴───────┴───────┘ Valid moves: 29, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 6 3 5 │ 1 2 7 │ 8 4 9 │ │ 8 1 4 │ 3 5 9 │ 2 6 7 │ │ 9 2 7 │ 4 6 8 │ 3 1 5 │ ├───────┼───────┼───────┤ │ 2 4 3 │ 7 1 . │ 6 5 8 │ │ 7 6 8 │ 2 4 5 │ 9 3 1 │ │ 1 5 9 │ 6 3 . │ 7 2 4 │ ├───────┼───────┼───────┤ │ 3 8 2 │ 5 7 4 │ 1 . 6 │ │ 4 7 1 │ 8 . 3 │ 5 9 2 │ │ 5 . 6 │ 9 . 2 │ 4 7 3 │ └───────┴───────┴───────┘ Valid moves: 29, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 6 3 5 │ 1 2 7 │ 8 4 9 │ │ 8 1 4 │ 3 5 9 │ 2 6 7 │ │ 9 2 7 │ 4 6 8 │ 3 1 5 │ ├───────┼───────┼───────┤ │ 2 4 3 │ 7 1 . │ 6 5 8 │ │ 7 6 8 │ 2 4 5 │ 9 3 1 │ │ 1 5 9 │ 6 3 . │ 7 2 4 │ ├───────┼───────┼───────┤ │ 3 8 2 │ 5 7 4 │ 1 . 6 │ │ 4 7 1 │ 8 . 3 │ 5 9 2 │ │ 5 . 6 │ 9 . 2 │ 4 7 3 │ └───────┴───────┴───────┘ Valid moves: 29, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 6 3 5 │ 1 2 7 │ 8 4 9 │ │ 8 1 4 │ 3 5 9 │ 2 6 7 │ │ 9 2 7 │ 4 6 8 │ 3 1 5 │ ├───────┼───────┼───────┤ │ 2 4 3 │ 7 1 . │ 6 5 8 │ │ 7 6 8 │ 2 4 5 │ 9 3 1 │ │ 1 5 9 │ 6 3 . │ 7 2 4 │ ├───────┼───────┼───────┤ │ 3 8 2 │ 5 7 4 │ 1 . 6 │ │ 4 7 1 │ 8 . 3 │ 5 9 2 │ │ 5 . 6 │ 9 . 2 │ 4 7 3 │ └───────┴───────┴───────┘ Valid moves: 31, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 5 2 1 │ 4 3 7 │ 6 9 8 │ │ 4 9 8 │ 5 6 1 │ 3 2 7 │ │ 6 3 7 │ 2 8 9 │ 1 4 5 │ ├───────┼───────┼───────┤ │ 2 6 5 │ 1 9 4 │ 7 8 3 │ │ 1 8 4 │ 3 2 6 │ 9 5 . │ │ 3 7 9 │ 8 5 . │ 4 6 1 │ ├───────┼───────┼───────┤ │ 7 5 6 │ 9 4 3 │ 2 1 . │ │ 9 1 2 │ 7 . 5 │ 8 3 4 │ │ 8 4 3 │ 6 1 2 │ 5 7 9 │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, -1) # Backtrack: no solution found for current backtrack step ============================================================ Valid moves: 31, Final state: failed Final board: ┌───────┬───────┬───────┐ │ 5 2 1 │ 4 3 7 │ 6 9 8 │ │ 4 9 8 │ 5 6 1 │ 3 2 7 │ │ 6 3 7 │ 2 8 9 │ 1 4 5 │ ├───────┼───────┼───────┤ │ 2 6 5 │ 1 9 4 │ 7 8 3 │ │ 1 8 4 │ 3 2 6 │ 9 5 . │ │ 3 7 9 │ 8 5 . │ 4 6 1 │ ├───────┼───────┼───────┤ │ 7 5 6 │ 9 4 3 │ 2 1 . │ │ 9 1 2 │ 7 . 5 │ 8 3 4 │ │ 8 4 3 │ 6 1 2 │ 5 7 9 │ └───────┴───────┴───────┘ Valid moves: 31, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 5 2 1 │ 4 3 7 │ 6 9 8 │ │ 4 9 8 │ 5 6 1 │ 3 2 7 │ │ 6 3 7 │ 2 8 9 │ 1 4 5 │ ├───────┼───────┼───────┤ │ 2 6 5 │ 1 9 4 │ 7 8 3 │ │ 1 8 4 │ 3 2 6 │ 9 5 . │ │ 3 7 9 │ 8 5 . │ 4 6 1 │ ├───────┼───────┼───────┤ │ 7 5 6 │ 9 4 3 │ 2 1 . │ │ 9 1 2 │ 7 . 5 │ 8 3 4 │ │ 8 4 3 │ 6 1 2 │ 5 7 9 │ └───────┴───────┴───────┘ Valid moves: 31, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 5 2 1 │ 4 3 7 │ 6 9 8 │ │ 4 9 8 │ 5 6 1 │ 3 2 7 │ │ 6 3 7 │ 2 8 9 │ 1 4 5 │ ├───────┼───────┼───────┤ │ 2 6 5 │ 1 9 4 │ 7 8 3 │ │ 1 8 4 │ 3 2 6 │ 9 5 . │ │ 3 7 9 │ 8 5 . │ 4 6 1 │ ├───────┼───────┼───────┤ │ 7 5 6 │ 9 4 3 │ 2 1 . │ │ 9 1 2 │ 7 . 5 │ 8 3 4 │ │ 8 4 3 │ 6 1 2 │ 5 7 9 │ └───────┴───────┴───────┘ Valid moves: 28, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 5 8 6 │ 1 2 3 │ 4 7 9 │ │ 7 2 4 │ . 5 6 │ 1 8 3 │ │ 3 1 9 │ 4 . 7 │ 2 6 5 │ ├───────┼───────┼───────┤ │ 1 5 2 │ 9 8 . │ 3 4 6 │ │ 9 3 7 │ 6 4 1 │ 5 . 2 │ │ 6 4 8 │ 2 3 5 │ 9 1 7 │ ├───────┼───────┼───────┤ │ 4 9 1 │ 5 7 2 │ 6 3 8 │ │ . 7 3 │ 8 6 9 │ . 2 1 │ │ 2 6 5 │ 3 1 4 │ 7 9 . │ └───────┴───────┴───────┘ Valid moves: 28, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 5 8 6 │ 1 2 3 │ 4 7 9 │ │ 7 2 4 │ . 5 6 │ 1 8 3 │ │ 3 1 9 │ 4 . 7 │ 2 6 5 │ ├───────┼───────┼───────┤ │ 1 5 2 │ 9 8 . │ 3 4 6 │ │ 9 3 7 │ 6 4 1 │ 5 . 2 │ │ 6 4 8 │ 2 3 5 │ 9 1 7 │ ├───────┼───────┼───────┤ │ 4 9 1 │ 5 7 2 │ 6 3 8 │ │ . 7 3 │ 8 6 9 │ . 2 1 │ │ 2 6 5 │ 3 1 4 │ 7 9 . │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, -1) # Backtrack: no solution found for this backtrack step ============================================================ Valid moves: 28, Final state: failed Final board: ┌───────┬───────┬───────┐ │ 5 8 6 │ 1 2 3 │ 4 7 9 │ │ 7 2 4 │ . 5 6 │ 1 8 3 │ │ 3 1 9 │ 4 . 7 │ 2 6 5 │ ├───────┼───────┼───────┤ │ 1 5 2 │ 9 8 . │ 3 4 6 │ │ 9 3 7 │ 6 4 1 │ 5 . 2 │ │ 6 4 8 │ 2 3 5 │ 9 1 7 │ ├───────┼───────┼───────┤ │ 4 9 1 │ 5 7 2 │ 6 3 8 │ │ . 7 3 │ 8 6 9 │ . 2 1 │ │ 2 6 5 │ 3 1 4 │ 7 9 . │ └───────┴───────┴───────┘ Valid moves: 28, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 5 8 6 │ 1 2 3 │ 4 7 9 │ │ 7 2 4 │ . 5 6 │ 1 8 3 │ │ 3 1 9 │ 4 . 7 │ 2 6 5 │ ├───────┼───────┼───────┤ │ 1 5 2 │ 9 8 . │ 3 4 6 │ │ 9 3 7 │ 6 4 1 │ 5 . 2 │ │ 6 4 8 │ 2 3 5 │ 9 1 7 │ ├───────┼───────┼───────┤ │ 4 9 1 │ 5 7 2 │ 6 3 8 │ │ . 7 3 │ 8 6 9 │ . 2 1 │ │ 2 6 5 │ 3 1 4 │ 7 9 . │ └───────┴───────┴───────┘ Valid moves: 28, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 6 7 3 │ 1 2 4 │ 9 5 . │ │ 9 5 2 │ 3 6 7 │ 4 1 8 │ │ 1 . 4 │ 5 8 9 │ 2 3 6 │ ├───────┼───────┼───────┤ │ 2 4 5 │ 8 7 1 │ 3 6 . │ │ 3 8 6 │ 9 4 . │ 1 2 5 │ │ 7 9 1 │ 2 3 5 │ 8 . 4 │ ├───────┼───────┼───────┤ │ 4 1 7 │ 6 5 2 │ . 8 9 │ │ 5 2 8 │ 7 9 3 │ 6 4 1 │ │ . 3 9 │ 4 1 8 │ 5 7 2 │ └───────┴───────┴───────┘ Valid moves: 28, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 6 7 3 │ 1 2 4 │ 9 5 . │ │ 9 5 2 │ 3 6 7 │ 4 1 8 │ │ 1 . 4 │ 5 8 9 │ 2 3 6 │ ├───────┼───────┼───────┤ │ 2 4 5 │ 8 7 1 │ 3 6 . │ │ 3 8 6 │ 9 4 . │ 1 2 5 │ │ 7 9 1 │ 2 3 5 │ 8 . 4 │ ├───────┼───────┼───────┤ │ 4 1 7 │ 6 5 2 │ . 8 9 │ │ 5 2 8 │ 7 9 3 │ 6 4 1 │ │ . 3 9 │ 4 1 8 │ 5 7 2 │ └───────┴───────┴───────┘ Valid moves: 28, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 6 7 3 │ 1 2 4 │ 9 5 . │ │ 9 5 2 │ 3 6 7 │ 4 1 8 │ │ 1 . 4 │ 5 8 9 │ 2 3 6 │ ├───────┼───────┼───────┤ │ 2 4 5 │ 8 7 1 │ 3 6 . │ │ 3 8 6 │ 9 4 . │ 1 2 5 │ │ 7 9 1 │ 2 3 5 │ 8 . 4 │ ├───────┼───────┼───────┤ │ 4 1 7 │ 6 5 2 │ . 8 9 │ │ 5 2 8 │ 7 9 3 │ 6 4 1 │ │ . 3 9 │ 4 1 8 │ 5 7 2 │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, -1) # Backtrack: no solution found for this backtrack step ============================================================ Valid moves: 28, Final state: failed Final board: ┌───────┬───────┬───────┐ │ 6 7 3 │ 1 2 4 │ 9 5 . │ │ 9 5 2 │ 3 6 7 │ 4 1 8 │ │ 1 . 4 │ 5 8 9 │ 2 3 6 │ ├───────┼───────┼───────┤ │ 2 4 5 │ 8 7 1 │ 3 6 . │ │ 3 8 6 │ 9 4 . │ 1 2 5 │ │ 7 9 1 │ 2 3 5 │ 8 . 4 │ ├───────┼───────┼───────┤ │ 4 1 7 │ 6 5 2 │ . 8 9 │ │ 5 2 8 │ 7 9 3 │ 6 4 1 │ │ . 3 9 │ 4 1 8 │ 5 7 2 │ └───────┴───────┴───────┘ Valid moves: 32, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 7 4 8 │ 1 2 5 │ 3 6 9 │ │ 3 5 . │ 4 7 6 │ 1 8 2 │ │ 1 6 2 │ 3 9 8 │ 7 4 5 │ ├───────┼───────┼───────┤ │ 5 3 6 │ 2 1 4 │ 9 7 8 │ │ 2 8 1 │ 9 6 7 │ 5 3 4 │ │ 4 7 9 │ 5 8 3 │ 6 2 1 │ ├───────┼───────┼───────┤ │ 6 2 7 │ 8 3 1 │ 4 9 . │ │ 8 1 3 │ 6 4 9 │ 2 5 7 │ │ 9 . 4 │ 7 5 2 │ 8 1 6 │ └───────┴───────┴───────┘ Valid moves: 32, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 7 4 8 │ 1 2 5 │ 3 6 9 │ │ 3 5 . │ 4 7 6 │ 1 8 2 │ │ 1 6 2 │ 3 9 8 │ 7 4 5 │ ├───────┼───────┼───────┤ │ 5 3 6 │ 2 1 4 │ 9 7 8 │ │ 2 8 1 │ 9 6 7 │ 5 3 4 │ │ 4 7 9 │ 5 8 3 │ 6 2 1 │ ├───────┼───────┼───────┤ │ 6 2 7 │ 8 3 1 │ 4 9 . │ │ 8 1 3 │ 6 4 9 │ 2 5 7 │ │ 9 . 4 │ 7 5 2 │ 8 1 6 │ └───────┴───────┴───────┘ Valid moves: 32, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 7 4 8 │ 1 2 5 │ 3 6 9 │ │ 3 5 . │ 4 7 6 │ 1 8 2 │ │ 1 6 2 │ 3 9 8 │ 7 4 5 │ ├───────┼───────┼───────┤ │ 5 3 6 │ 2 1 4 │ 9 7 8 │ │ 2 8 1 │ 9 6 7 │ 5 3 4 │ │ 4 7 9 │ 5 8 3 │ 6 2 1 │ ├───────┼───────┼───────┤ │ 6 2 7 │ 8 3 1 │ 4 9 . │ │ 8 1 3 │ 6 4 9 │ 2 5 7 │ │ 9 . 4 │ 7 5 2 │ 8 1 6 │ └───────┴───────┴───────┘ Valid moves: 32, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 7 4 8 │ 1 2 5 │ 3 6 9 │ │ 3 5 . │ 4 7 6 │ 1 8 2 │ │ 1 6 2 │ 3 9 8 │ 7 4 5 │ ├───────┼───────┼───────┤ │ 5 3 6 │ 2 1 4 │ 9 7 8 │ │ 2 8 1 │ 9 6 7 │ 5 3 4 │ │ 4 7 9 │ 5 8 3 │ 6 2 1 │ ├───────┼───────┼───────┤ │ 6 2 7 │ 8 3 1 │ 4 9 . │ │ 8 1 3 │ 6 4 9 │ 2 5 7 │ │ 9 . 4 │ 7 5 2 │ 8 1 6 │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, -1) # Backtrack: no cell found for this backtrack step ============================================================ Valid moves: 33, Final state: failed Final board: ┌───────┬───────┬───────┐ │ 7 3 8 │ 1 2 4 │ 6 5 9 │ │ 2 9 6 │ 7 8 5 │ 3 1 4 │ │ 1 5 4 │ 9 3 6 │ 2 7 8 │ ├───────┼───────┼───────┤ │ 3 4 2 │ 5 1 7 │ 9 8 6 │ │ 5 6 1 │ 3 4 8 │ 7 . 2 │ │ 8 7 9 │ 6 . 2 │ 5 3 1 │ ├───────┼───────┼───────┤ │ 4 8 5 │ 2 7 9 │ 1 6 3 │ │ 6 2 3 │ 4 5 1 │ 8 9 7 │ │ 9 1 7 │ 8 6 3 │ 4 2 5 │ └───────┴───────┴───────┘ Valid moves: 33, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 7 3 8 │ 1 2 4 │ 6 5 9 │ │ 2 9 6 │ 7 8 5 │ 3 1 4 │ │ 1 5 4 │ 9 3 6 │ 2 7 8 │ ├───────┼───────┼───────┤ │ 3 4 2 │ 5 1 7 │ 9 8 6 │ │ 5 6 1 │ 3 4 8 │ 7 . 2 │ │ 8 7 9 │ 6 . 2 │ 5 3 1 │ ├───────┼───────┼───────┤ │ 4 8 5 │ 2 7 9 │ 1 6 3 │ │ 6 2 3 │ 4 5 1 │ 8 9 7 │ │ 9 1 7 │ 8 6 3 │ 4 2 5 │ └───────┴───────┴───────┘ Valid moves: 33, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 7 3 8 │ 1 2 4 │ 6 5 9 │ │ 2 9 6 │ 7 8 5 │ 3 1 4 │ │ 1 5 4 │ 9 3 6 │ 2 7 8 │ ├───────┼───────┼───────┤ │ 3 4 2 │ 5 1 7 │ 9 8 6 │ │ 5 6 1 │ 3 4 8 │ 7 . 2 │ │ 8 7 9 │ 6 . 2 │ 5 3 1 │ ├───────┼───────┼───────┤ │ 4 8 5 │ 2 7 9 │ 1 6 3 │ │ 6 2 3 │ 4 5 1 │ 8 9 7 │ │ 9 1 7 │ 8 6 3 │ 4 2 5 │ └───────┴───────┴───────┘ Valid moves: 33, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 7 3 8 │ 1 2 4 │ 6 5 9 │ │ 2 9 6 │ 7 8 5 │ 3 1 4 │ │ 1 5 4 │ 9 3 6 │ 2 7 8 │ ├───────┼───────┼───────┤ │ 3 4 2 │ 5 1 7 │ 9 8 6 │ │ 5 6 1 │ 3 4 8 │ 7 . 2 │ │ 8 7 9 │ 6 . 2 │ 5 3 1 │ ├───────┼───────┼───────┤ │ 4 8 5 │ 2 7 9 │ 1 6 3 │ │ 6 2 3 │ 4 5 1 │ 8 9 7 │ │ 9 1 7 │ 8 6 3 │ 4 2 5 │ └───────┴───────┴───────┘ Valid moves: 30, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 7 2 . │ 3 1 4 │ 6 5 9 │ │ 3 9 5 │ 2 7 6 │ 1 4 8 │ │ 4 6 1 │ 5 9 8 │ 3 2 7 │ ├───────┼───────┼───────┤ │ 2 5 9 │ 1 6 3 │ 7 8 4 │ │ 1 8 3 │ 7 4 5 │ 9 . 2 │ │ . . 4 │ 8 2 9 │ 5 1 6 │ ├───────┼───────┼───────┤ │ 6 1 2 │ 4 5 7 │ 8 9 3 │ │ 9 4 . │ 6 8 1 │ 2 7 5 │ │ 5 7 8 │ 9 3 2 │ 4 6 1 │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid[row, col, number]: # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid[row, col, number]: return (row, col, number) return (-1, -1, -1) # Backtrack: no solution found for this backtracking step ============================================================ Valid moves: 30, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 7 2 . │ 3 1 4 │ 6 5 9 │ │ 3 9 5 │ 2 7 6 │ 1 4 8 │ │ 4 6 1 │ 5 9 8 │ 3 2 7 │ ├───────┼───────┼───────┤ │ 2 5 9 │ 1 6 3 │ 7 8 4 │ │ 1 8 3 │ 7 4 5 │ 9 . 2 │ │ . . 4 │ 8 2 9 │ 5 1 6 │ ├───────┼───────┼───────┤ │ 6 1 2 │ 4 5 7 │ 8 9 3 │ │ 9 4 . │ 6 8 1 │ 2 7 5 │ │ 5 7 8 │ 9 3 2 │ 4 6 1 │ └───────┴───────┴───────┘ Valid moves: 30, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 7 2 . │ 3 1 4 │ 6 5 9 │ │ 3 9 5 │ 2 7 6 │ 1 4 8 │ │ 4 6 1 │ 5 9 8 │ 3 2 7 │ ├───────┼───────┼───────┤ │ 2 5 9 │ 1 6 3 │ 7 8 4 │ │ 1 8 3 │ 7 4 5 │ 9 . 2 │ │ . . 4 │ 8 2 9 │ 5 1 6 │ ├───────┼───────┼───────┤ │ 6 1 2 │ 4 5 7 │ 8 9 3 │ │ 9 4 . │ 6 8 1 │ 2 7 5 │ │ 5 7 8 │ 9 3 2 │ 4 6 1 │ └───────┴───────┴───────┘ Valid moves: 33, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 4 1 2 │ 3 5 7 │ 9 6 8 │ │ 7 3 6 │ 2 9 8 │ 1 5 4 │ │ 5 8 9 │ 4 6 1 │ 2 7 3 │ ├───────┼───────┼───────┤ │ 1 7 8 │ 5 4 2 │ 3 9 6 │ │ 2 9 4 │ 1 3 6 │ 7 8 5 │ │ 3 6 5 │ 8 7 9 │ 4 1 2 │ ├───────┼───────┼───────┤ │ 8 2 3 │ 7 1 5 │ 6 4 9 │ │ 6 5 1 │ 9 2 3 │ 8 . 7 │ │ 9 4 7 │ 6 8 . │ 5 2 1 │ └───────┴───────┴───────┘ Valid moves: 33, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 4 1 2 │ 3 5 7 │ 9 6 8 │ │ 7 3 6 │ 2 9 8 │ 1 5 4 │ │ 5 8 9 │ 4 6 1 │ 2 7 3 │ ├───────┼───────┼───────┤ │ 1 7 8 │ 5 4 2 │ 3 9 6 │ │ 2 9 4 │ 1 3 6 │ 7 8 5 │ │ 3 6 5 │ 8 7 9 │ 4 1 2 │ ├───────┼───────┼───────┤ │ 8 2 3 │ 7 1 5 │ 6 4 9 │ │ 6 5 1 │ 9 2 3 │ 8 . 7 │ │ 9 4 7 │ 6 8 . │ 5 2 1 │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, -1) # Backtrack: no solution found for current backtrack step ============================================================ Valid moves: 33, Final state: failed Final board: ┌───────┬───────┬───────┐ │ 4 1 2 │ 3 5 7 │ 9 6 8 │ │ 7 3 6 │ 2 9 8 │ 1 5 4 │ │ 5 8 9 │ 4 6 1 │ 2 7 3 │ ├───────┼───────┼───────┤ │ 1 7 8 │ 5 4 2 │ 3 9 6 │ │ 2 9 4 │ 1 3 6 │ 7 8 5 │ │ 3 6 5 │ 8 7 9 │ 4 1 2 │ ├───────┼───────┼───────┤ │ 8 2 3 │ 7 1 5 │ 6 4 9 │ │ 6 5 1 │ 9 2 3 │ 8 . 7 │ │ 9 4 7 │ 6 8 . │ 5 2 1 │ └───────┴───────┴───────┘ Valid moves: 33, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 4 1 2 │ 3 5 7 │ 9 6 8 │ │ 7 3 6 │ 2 9 8 │ 1 5 4 │ │ 5 8 9 │ 4 6 1 │ 2 7 3 │ ├───────┼───────┼───────┤ │ 1 7 8 │ 5 4 2 │ 3 9 6 │ │ 2 9 4 │ 1 3 6 │ 7 8 5 │ │ 3 6 5 │ 8 7 9 │ 4 1 2 │ ├───────┼───────┼───────┤ │ 8 2 3 │ 7 1 5 │ 6 4 9 │ │ 6 5 1 │ 9 2 3 │ 8 . 7 │ │ 9 4 7 │ 6 8 . │ 5 2 1 │ └───────┴───────┴───────┘ Valid moves: 35, Final state: success Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 6 1 3 │ 2 4 5 │ 7 8 9 │ │ 7 9 2 │ 1 8 3 │ 4 5 6 │ │ 4 8 5 │ 6 7 9 │ 1 2 3 │ ├───────┼───────┼───────┤ │ 3 5 6 │ 4 2 8 │ 9 7 1 │ │ 8 4 7 │ 5 9 1 │ 6 3 2 │ │ 1 2 9 │ 7 3 6 │ 5 4 8 │ ├───────┼───────┼───────┤ │ 2 3 1 │ 9 5 4 │ 8 6 7 │ │ 5 6 8 │ 3 1 7 │ 2 9 4 │ │ 9 7 4 │ 8 6 2 │ 3 1 5 │ └───────┴───────┴───────┘ Valid moves: 35, Final state: success Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 6 1 3 │ 2 4 5 │ 7 8 9 │ │ 7 9 2 │ 1 8 3 │ 4 5 6 │ │ 4 8 5 │ 6 7 9 │ 1 2 3 │ ├───────┼───────┼───────┤ │ 3 5 6 │ 4 2 8 │ 9 7 1 │ │ 8 4 7 │ 5 9 1 │ 6 3 2 │ │ 1 2 9 │ 7 3 6 │ 5 4 8 │ ├───────┼───────┼───────┤ │ 2 3 1 │ 9 5 4 │ 8 6 7 │ │ 5 6 8 │ 3 1 7 │ 2 9 4 │ │ 9 7 4 │ 8 6 2 │ 3 1 5 │ └───────┴───────┴───────┘ Valid moves: 35, Final state: success Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 6 1 3 │ 2 4 5 │ 7 8 9 │ │ 7 9 2 │ 1 8 3 │ 4 5 6 │ │ 4 8 5 │ 6 7 9 │ 1 2 3 │ ├───────┼───────┼───────┤ │ 3 5 6 │ 4 2 8 │ 9 7 1 │ │ 8 4 7 │ 5 9 1 │ 6 3 2 │ │ 1 2 9 │ 7 3 6 │ 5 4 8 │ ├───────┼───────┼───────┤ │ 2 3 1 │ 9 5 4 │ 8 6 7 │ │ 5 6 8 │ 3 1 7 │ 2 9 4 │ │ 9 7 4 │ 8 6 2 │ 3 1 5 │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, -1) # Backtrack: no solution found for current branch ============================================================ Valid moves: 35, Final state: success Final board: ┌───────┬───────┬───────┐ │ 6 1 3 │ 2 4 5 │ 7 8 9 │ │ 7 9 2 │ 1 8 3 │ 4 5 6 │ │ 4 8 5 │ 6 7 9 │ 1 2 3 │ ├───────┼───────┼───────┤ │ 3 5 6 │ 4 2 8 │ 9 7 1 │ │ 8 4 7 │ 5 9 1 │ 6 3 2 │ │ 1 2 9 │ 7 3 6 │ 5 4 8 │ ├───────┼───────┼───────┤ │ 2 3 1 │ 9 5 4 │ 8 6 7 │ │ 5 6 8 │ 3 1 7 │ 2 9 4 │ │ 9 7 4 │ 8 6 2 │ 3 1 5 │ └───────┴───────┴───────┘ Valid moves: 32, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 2 6 1 │ 3 5 4 │ 7 8 9 │ │ 7 3 9 │ 1 6 2 │ 4 . 5 │ │ 8 5 4 │ 9 . 7 │ 2 1 3 │ ├───────┼───────┼───────┤ │ 4 8 5 │ 7 1 3 │ 6 9 2 │ │ 6 1 3 │ 8 2 9 │ 5 4 7 │ │ 9 2 7 │ 6 4 5 │ 8 3 1 │ ├───────┼───────┼───────┤ │ 1 7 8 │ 2 3 6 │ 9 5 4 │ │ 5 9 6 │ 4 7 1 │ 3 2 8 │ │ 3 4 2 │ 5 9 8 │ 1 6 . │ └───────┴───────┴───────┘ Valid moves: 32, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 2 6 1 │ 3 5 4 │ 7 8 9 │ │ 7 3 9 │ 1 6 2 │ 4 . 5 │ │ 8 5 4 │ 9 . 7 │ 2 1 3 │ ├───────┼───────┼───────┤ │ 4 8 5 │ 7 1 3 │ 6 9 2 │ │ 6 1 3 │ 8 2 9 │ 5 4 7 │ │ 9 2 7 │ 6 4 5 │ 8 3 1 │ ├───────┼───────┼───────┤ │ 1 7 8 │ 2 3 6 │ 9 5 4 │ │ 5 9 6 │ 4 7 1 │ 3 2 8 │ │ 3 4 2 │ 5 9 8 │ 1 6 . │ └───────┴───────┴───────┘ Valid moves: 32, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 2 6 1 │ 3 5 4 │ 7 8 9 │ │ 7 3 9 │ 1 6 2 │ 4 . 5 │ │ 8 5 4 │ 9 . 7 │ 2 1 3 │ ├───────┼───────┼───────┤ │ 4 8 5 │ 7 1 3 │ 6 9 2 │ │ 6 1 3 │ 8 2 9 │ 5 4 7 │ │ 9 2 7 │ 6 4 5 │ 8 3 1 │ ├───────┼───────┼───────┤ │ 1 7 8 │ 2 3 6 │ 9 5 4 │ │ 5 9 6 │ 4 7 1 │ 3 2 8 │ │ 3 4 2 │ 5 9 8 │ 1 6 . │ └───────┴───────┴───────┘ Valid moves: 32, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 2 6 1 │ 3 5 4 │ 7 8 9 │ │ 7 3 9 │ 1 6 2 │ 4 . 5 │ │ 8 5 4 │ 9 . 7 │ 2 1 3 │ ├───────┼───────┼───────┤ │ 4 8 5 │ 7 1 3 │ 6 9 2 │ │ 6 1 3 │ 8 2 9 │ 5 4 7 │ │ 9 2 7 │ 6 4 5 │ 8 3 1 │ ├───────┼───────┼───────┤ │ 1 7 8 │ 2 3 6 │ 9 5 4 │ │ 5 9 6 │ 4 7 1 │ 3 2 8 │ │ 3 4 2 │ 5 9 8 │ 1 6 . │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, -1) # Backtrack: no cell found for this backtrack step ============================================================ Valid moves: 27, Final state: failed Final board: ┌───────┬───────┬───────┐ │ 8 5 3 │ 1 2 6 │ 7 4 9 │ │ 2 9 4 │ 3 . 7 │ 1 6 8 │ │ . 7 1 │ 4 9 8 │ 2 5 3 │ ├───────┼───────┼───────┤ │ 1 2 8 │ 6 3 . │ 5 7 . │ │ 6 . 9 │ 5 7 1 │ 8 3 4 │ │ 7 3 5 │ 9 8 4 │ 6 1 2 │ ├───────┼───────┼───────┤ │ 5 8 6 │ 2 4 3 │ 9 . 1 │ │ 4 . 2 │ 7 1 . │ 3 8 5 │ │ 3 1 7 │ 8 5 9 │ 4 2 6 │ └───────┴───────┴───────┘ Valid moves: 27, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 8 5 3 │ 1 2 6 │ 7 4 9 │ │ 2 9 4 │ 3 . 7 │ 1 6 8 │ │ . 7 1 │ 4 9 8 │ 2 5 3 │ ├───────┼───────┼───────┤ │ 1 2 8 │ 6 3 . │ 5 7 . │ │ 6 . 9 │ 5 7 1 │ 8 3 4 │ │ 7 3 5 │ 9 8 4 │ 6 1 2 │ ├───────┼───────┼───────┤ │ 5 8 6 │ 2 4 3 │ 9 . 1 │ │ 4 . 2 │ 7 1 . │ 3 8 5 │ │ 3 1 7 │ 8 5 9 │ 4 2 6 │ └───────┴───────┴───────┘ Valid moves: 27, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 8 5 3 │ 1 2 6 │ 7 4 9 │ │ 2 9 4 │ 3 . 7 │ 1 6 8 │ │ . 7 1 │ 4 9 8 │ 2 5 3 │ ├───────┼───────┼───────┤ │ 1 2 8 │ 6 3 . │ 5 7 . │ │ 6 . 9 │ 5 7 1 │ 8 3 4 │ │ 7 3 5 │ 9 8 4 │ 6 1 2 │ ├───────┼───────┼───────┤ │ 5 8 6 │ 2 4 3 │ 9 . 1 │ │ 4 . 2 │ 7 1 . │ 3 8 5 │ │ 3 1 7 │ 8 5 9 │ 4 2 6 │ └───────┴───────┴───────┘ Valid moves: 27, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 8 5 3 │ 1 2 6 │ 7 4 9 │ │ 2 9 4 │ 3 . 7 │ 1 6 8 │ │ . 7 1 │ 4 9 8 │ 2 5 3 │ ├───────┼───────┼───────┤ │ 1 2 8 │ 6 3 . │ 5 7 . │ │ 6 . 9 │ 5 7 1 │ 8 3 4 │ │ 7 3 5 │ 9 8 4 │ 6 1 2 │ ├───────┼───────┼───────┤ │ 5 8 6 │ 2 4 3 │ 9 . 1 │ │ 4 . 2 │ 7 1 . │ 3 8 5 │ │ 3 1 7 │ 8 5 9 │ 4 2 6 │ └───────┴───────┴───────┘ Valid moves: 30, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 5 2 6 │ 1 3 4 │ 7 8 9 │ │ 1 3 4 │ 7 8 9 │ 5 2 6 │ │ 7 8 9 │ 5 2 . │ 1 4 3 │ ├───────┼───────┼───────┤ │ 4 1 7 │ 3 9 6 │ . 5 8 │ │ 2 9 3 │ . 1 5 │ 4 6 7 │ │ 6 5 . │ 8 4 7 │ 3 1 2 │ ├───────┼───────┼───────┤ │ 3 6 5 │ 2 7 1 │ 8 9 4 │ │ 9 7 2 │ 4 5 8 │ 6 3 1 │ │ 8 4 1 │ 6 . 3 │ 2 7 5 │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, -1) # Backtrack: no cell found with a valid number ============================================================ Valid moves: 30, Final state: failed Final board: ┌───────┬───────┬───────┐ │ 5 2 6 │ 1 3 4 │ 7 8 9 │ │ 1 3 4 │ 7 8 9 │ 5 2 6 │ │ 7 8 9 │ 5 2 . │ 1 4 3 │ ├───────┼───────┼───────┤ │ 4 1 7 │ 3 9 6 │ . 5 8 │ │ 2 9 3 │ . 1 5 │ 4 6 7 │ │ 6 5 . │ 8 4 7 │ 3 1 2 │ ├───────┼───────┼───────┤ │ 3 6 5 │ 2 7 1 │ 8 9 4 │ │ 9 7 2 │ 4 5 8 │ 6 3 1 │ │ 8 4 1 │ 6 . 3 │ 2 7 5 │ └───────┴───────┴───────┘ Valid moves: 30, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 5 2 6 │ 1 3 4 │ 7 8 9 │ │ 1 3 4 │ 7 8 9 │ 5 2 6 │ │ 7 8 9 │ 5 2 . │ 1 4 3 │ ├───────┼───────┼───────┤ │ 4 1 7 │ 3 9 6 │ . 5 8 │ │ 2 9 3 │ . 1 5 │ 4 6 7 │ │ 6 5 . │ 8 4 7 │ 3 1 2 │ ├───────┼───────┼───────┤ │ 3 6 5 │ 2 7 1 │ 8 9 4 │ │ 9 7 2 │ 4 5 8 │ 6 3 1 │ │ 8 4 1 │ 6 . 3 │ 2 7 5 │ └───────┴───────┴───────┘ Valid moves: 30, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 5 2 6 │ 1 3 4 │ 7 8 9 │ │ 1 3 4 │ 7 8 9 │ 5 2 6 │ │ 7 8 9 │ 5 2 . │ 1 4 3 │ ├───────┼───────┼───────┤ │ 4 1 7 │ 3 9 6 │ . 5 8 │ │ 2 9 3 │ . 1 5 │ 4 6 7 │ │ 6 5 . │ 8 4 7 │ 3 1 2 │ ├───────┼───────┼───────┤ │ 3 6 5 │ 2 7 1 │ 8 9 4 │ │ 9 7 2 │ 4 5 8 │ 6 3 1 │ │ 8 4 1 │ 6 . 3 │ 2 7 5 │ └───────┴───────┴───────┘ Valid moves: 33, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 1 7 4 │ 5 2 6 │ 8 3 9 │ │ 5 3 . │ 1 8 9 │ 2 7 4 │ │ 8 9 2 │ 3 4 7 │ 1 6 5 │ ├───────┼───────┼───────┤ │ 2 8 3 │ 4 6 1 │ 5 9 7 │ │ . 6 1 │ 7 5 3 │ 4 2 8 │ │ 4 5 7 │ 8 9 2 │ 6 1 3 │ ├───────┼───────┼───────┤ │ 3 2 5 │ 9 1 4 │ 7 8 6 │ │ 7 1 8 │ 6 3 5 │ 9 4 2 │ │ 9 4 6 │ 2 7 8 │ 3 5 1 │ └───────┴───────┴───────┘ Valid moves: 33, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 1 7 4 │ 5 2 6 │ 8 3 9 │ │ 5 3 . │ 1 8 9 │ 2 7 4 │ │ 8 9 2 │ 3 4 7 │ 1 6 5 │ ├───────┼───────┼───────┤ │ 2 8 3 │ 4 6 1 │ 5 9 7 │ │ . 6 1 │ 7 5 3 │ 4 2 8 │ │ 4 5 7 │ 8 9 2 │ 6 1 3 │ ├───────┼───────┼───────┤ │ 3 2 5 │ 9 1 4 │ 7 8 6 │ │ 7 1 8 │ 6 3 5 │ 9 4 2 │ │ 9 4 6 │ 2 7 8 │ 3 5 1 │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, -1) # Backtrack: no solution found for this backtrack step ============================================================ Valid moves: 33, Final state: failed Final board: ┌───────┬───────┬───────┐ │ 1 7 4 │ 5 2 6 │ 8 3 9 │ │ 5 3 . │ 1 8 9 │ 2 7 4 │ │ 8 9 2 │ 3 4 7 │ 1 6 5 │ ├───────┼───────┼───────┤ │ 2 8 3 │ 4 6 1 │ 5 9 7 │ │ . 6 1 │ 7 5 3 │ 4 2 8 │ │ 4 5 7 │ 8 9 2 │ 6 1 3 │ ├───────┼───────┼───────┤ │ 3 2 5 │ 9 1 4 │ 7 8 6 │ │ 7 1 8 │ 6 3 5 │ 9 4 2 │ │ 9 4 6 │ 2 7 8 │ 3 5 1 │ └───────┴───────┴───────┘ Valid moves: 33, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 1 7 4 │ 5 2 6 │ 8 3 9 │ │ 5 3 . │ 1 8 9 │ 2 7 4 │ │ 8 9 2 │ 3 4 7 │ 1 6 5 │ ├───────┼───────┼───────┤ │ 2 8 3 │ 4 6 1 │ 5 9 7 │ │ . 6 1 │ 7 5 3 │ 4 2 8 │ │ 4 5 7 │ 8 9 2 │ 6 1 3 │ ├───────┼───────┼───────┤ │ 3 2 5 │ 9 1 4 │ 7 8 6 │ │ 7 1 8 │ 6 3 5 │ 9 4 2 │ │ 9 4 6 │ 2 7 8 │ 3 5 1 │ └───────┴───────┴───────┘ Valid moves: 28, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 9 6 2 │ 1 3 7 │ 5 4 8 │ │ 7 5 1 │ 4 6 8 │ 2 3 9 │ │ 3 8 4 │ 2 5 9 │ 1 6 . │ ├───────┼───────┼───────┤ │ 1 3 6 │ 8 2 4 │ 7 5 . │ │ 2 7 8 │ 6 1 5 │ 9 . 3 │ │ 4 9 5 │ . 7 3 │ 6 1 2 │ ├───────┼───────┼───────┤ │ 5 1 3 │ 7 8 6 │ 4 9 . │ │ 8 2 9 │ 5 4 1 │ 3 . 7 │ │ 6 4 7 │ 3 9 2 │ 8 . 1 │ └───────┴───────┴───────┘ Valid moves: 28, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 9 6 2 │ 1 3 7 │ 5 4 8 │ │ 7 5 1 │ 4 6 8 │ 2 3 9 │ │ 3 8 4 │ 2 5 9 │ 1 6 . │ ├───────┼───────┼───────┤ │ 1 3 6 │ 8 2 4 │ 7 5 . │ │ 2 7 8 │ 6 1 5 │ 9 . 3 │ │ 4 9 5 │ . 7 3 │ 6 1 2 │ ├───────┼───────┼───────┤ │ 5 1 3 │ 7 8 6 │ 4 9 . │ │ 8 2 9 │ 5 4 1 │ 3 . 7 │ │ 6 4 7 │ 3 9 2 │ 8 . 1 │ └───────┴───────┴───────┘ Valid moves: 28, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 9 6 2 │ 1 3 7 │ 5 4 8 │ │ 7 5 1 │ 4 6 8 │ 2 3 9 │ │ 3 8 4 │ 2 5 9 │ 1 6 . │ ├───────┼───────┼───────┤ │ 1 3 6 │ 8 2 4 │ 7 5 . │ │ 2 7 8 │ 6 1 5 │ 9 . 3 │ │ 4 9 5 │ . 7 3 │ 6 1 2 │ ├───────┼───────┼───────┤ │ 5 1 3 │ 7 8 6 │ 4 9 . │ │ 8 2 9 │ 5 4 1 │ 3 . 7 │ │ 6 4 7 │ 3 9 2 │ 8 . 1 │ └───────┴───────┴───────┘ ============================================================ def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: return False # Check 3x3 box box_row, box_col = row - row % 3, col - col % 3 for r in range(3): for c in range(3): if board[box_row + r][box_col + c] == number: return False return True for row in range(9): for col in range(9): if initial[row][col] == 0 and board[row][col] == 0: for number in range(1, 10): if is_valid(row, col, number): return (row, col, number) return (-1, -1, -1) # Backtrack: no position found for this backtrack step ============================================================ Valid moves: 28, Final state: failed Final board: ┌───────┬───────┬───────┐ │ 9 6 2 │ 1 3 7 │ 5 4 8 │ │ 7 5 1 │ 4 6 8 │ 2 3 9 │ │ 3 8 4 │ 2 5 9 │ 1 6 . │ ├───────┼───────┼───────┤ │ 1 3 6 │ 8 2 4 │ 7 5 . │ │ 2 7 8 │ 6 1 5 │ 9 . 3 │ │ 4 9 5 │ . 7 3 │ 6 1 2 │ ├───────┼───────┼───────┤ │ 5 1 3 │ 7 8 6 │ 4 9 . │ │ 8 2 9 │ 5 4 1 │ 3 . 7 │ │ 6 4 7 │ 3 9 2 │ 8 . 1 │ └───────┴───────┴───────┘ Valid moves: 33, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 7 9 3 │ 2 1 4 │ 5 8 6 │ │ 5 4 2 │ 6 3 8 │ 7 1 9 │ │ 6 1 8 │ 5 9 7 │ 2 4 3 │ ├───────┼───────┼───────┤ │ 1 3 4 │ 9 7 2 │ 6 5 8 │ │ 8 5 6 │ 1 . 3 │ 4 7 2 │ │ 2 7 . │ 4 6 5 │ 9 3 1 │ ├───────┼───────┼───────┤ │ 3 2 7 │ 8 4 9 │ 1 6 5 │ │ 9 8 1 │ 7 5 6 │ 3 2 4 │ │ 4 6 5 │ 3 2 1 │ 8 9 7 │ └───────┴───────┴───────┘ Valid moves: 33, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for i in range(9): if board[row][i] == number or board[i][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 7 9 3 │ 2 1 4 │ 5 8 6 │ │ 5 4 2 │ 6 3 8 │ 7 1 9 │ │ 6 1 8 │ 5 9 7 │ 2 4 3 │ ├───────┼───────┼───────┤ │ 1 3 4 │ 9 7 2 │ 6 5 8 │ │ 8 5 6 │ 1 . 3 │ 4 7 2 │ │ 2 7 . │ 4 6 5 │ 9 3 1 │ ├───────┼───────┼───────┤ │ 3 2 7 │ 8 4 9 │ 1 6 5 │ │ 9 8 1 │ 7 5 6 │ 3 2 4 │ │ 4 6 5 │ 3 2 1 │ 8 9 7 │ └───────┴───────┴───────┘ Valid moves: 33, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 7 9 3 │ 2 1 4 │ 5 8 6 │ │ 5 4 2 │ 6 3 8 │ 7 1 9 │ │ 6 1 8 │ 5 9 7 │ 2 4 3 │ ├───────┼───────┼───────┤ │ 1 3 4 │ 9 7 2 │ 6 5 8 │ │ 8 5 6 │ 1 . 3 │ 4 7 2 │ │ 2 7 . │ 4 6 5 │ 9 3 1 │ ├───────┼───────┼───────┤ │ 3 2 7 │ 8 4 9 │ 1 6 5 │ │ 9 8 1 │ 7 5 6 │ 3 2 4 │ │ 4 6 5 │ 3 2 1 │ 8 9 7 │ └───────┴───────┴───────┘ Valid moves: 33, Final state: failed Strategy: def strategy(board, initial): def is_valid(row, col, number): # Check row and column for n in range(9): if board[row][n] == number or board[n][col] == number: ... Final board: ┌───────┬───────┬───────┐ │ 7 9 3 │ 2 1 4 │ 5 8 6 │ │ 5 4 2 │ 6 3 8 │ 7 1 9 │ │ 6 1 8 │ 5 9 7 │ 2 4 3 │ ├───────┼───────┼───────┤ │ 1 3 4 │ 9 7 2 │ 6 5 8 │ │ 8 5 6 │ 1 . 3 │ 4 7 2 │ │ 2 7 . │ 4 6 5 │ 9 3 1 │ ├───────┼───────┼───────┤ │ 3 2 7 │ 8 4 9 │ 1 6 5 │ │ 9 8 1 │ 7 5 6 │ 3 2 4 │ │ 4 6 5 │ 3 2 1 │ 8 9 7 │ └───────┴───────┴───────┘
TrainOutput(global_step=200, training_loss=0.0005054940755871939, metrics={'train_runtime': 7679.959, 'train_samples_per_second': 0.104, 'train_steps_per_second': 0.026, 'total_flos': 0.0, 'train_loss': 0.0005054940755871939}) And now with the LoRA we just trained with GRPO - we first save the LoRA first!
['grpo_saved_lora/processor_config.json']
Verify LoRA is actually trained!
<s><s>[SYSTEM_PROMPT]You are Ministral-3-3B-Instruct-2512, a Large Language Model (LLM) created by Mistral AI, a French startup headquartered in Paris.
You power an AI assistant called Le Chat.
Your knowledge base was last updated on 2023-10-01.
The current date is {today}.
When you're not sure about some information or when the user's request requires up-to-date or specific data, you must use the available tools to fetch the information. Do not hesitate to use tools whenever they can provide a more accurate or complete response. If no relevant tools are available, then clearly state that you don't have the information and avoid making up anything.
If the user's question is not clear, ambiguous, or does not provide enough context for you to accurately answer the question, you do not try to answer it right away and you rather ask the user to clarify their request (e.g. "What are some good restaurants around me?" => "Where are you?" or "When is the next flight to Tokyo" => "Where do you travel from?").
You are always very attentive to dates, in particular you try to resolve dates (e.g. "yesterday" is {yesterday}) and when asked about information at specific dates, you discard information that is at another date.
You follow these instructions in all languages, and always respond to the user in the language they use or request.
Next sections describe the capabilities that you have.
# WEB BROWSING INSTRUCTIONS
You cannot perform any web search or access internet to open URLs, links etc. If it seems like the user is expecting you to do so, you clarify the situation and ask the user to copy paste the text directly in the chat.
# MULTI-MODAL INSTRUCTIONS
You have the ability to read images, but you cannot generate images. You also cannot transcribe audio files or videos.
You cannot read nor transcribe audio files or videos.
# TOOL CALLING INSTRUCTIONS
You may have access to tools that you can use to fetch information or perform actions. You must use these tools in the following situations:
1. When the request requires up-to-date information.
2. When the request requires specific data that you do not have in your knowledge base.
3. When the request involves actions that you cannot perform without tools.
Always prioritize using tools to provide the most accurate and helpful response. If tools are not available, inform the user that you cannot perform the requested action at the moment.[/SYSTEM_PROMPT][INST]Create a Sudoku solving strategy using only native Python built-in functions without any import statements.
You are given two lists of lists (9x9 grids):
- board: current state (0 means empty)
- initial: starting puzzle (0 means was empty, numbers are fixed)
Return a tuple (row, col, number) for the next move.
- row: 0-8 (row index)
- col: 0-8 (column index)
- number: 1-9 (digit to place)
Only place numbers in cells that are BOTH empty in initial AND empty in board (initial[row][col] == 0 AND board[row][col] == 0)
Use Sudoku rules: no duplicates in rows, columns, or 3x3 boxes.
Output your function in backticks:
```python
def strategy(board, initial):
# Your logic here
return (row, col, number)
```
All helper functions must be inside def strategy. Output only the function.[/INST]```python
def strategy(board, initial):
def is_valid(row, col, number):
# Check row and column
for i in range(9):
if board[row][i] == number or board[i][col] == number:
return False
# Check 3x3 box
box_row, box_col = row - row % 3, col - col % 3
for r in range(3):
for c in range(3):
if board[box_row + r][box_col + c] == number:
return False
return True
for row in range(9):
for col in range(9):
if initial[row][col] == 0 and board[row][col] == 0:
for number in range(1, 10):
if is_valid(row, col, number):
return (row, col, number)
return (-1, -1, -1) # Backtrack: no solution found for this backtrack step
```</s> Saving to float16 for VLLM
We also support saving to float16 directly. Select merged_16bit for float16 or merged_4bit for int4. We also allow lora adapters as a fallback. Use push_to_hub_merged to upload to your Hugging Face account! You can go to https://huggingface.co/settings/tokens for your personal tokens. See our docs for more deployment options.
GGUF / llama.cpp Conversion
To save to GGUF / llama.cpp, we support it natively now! We clone llama.cpp and we default save it to q8_0. We allow all methods like q4_k_m. Use save_pretrained_gguf for local saving and push_to_hub_gguf for uploading to HF.
Some supported quant methods (full list on our docs page):
q8_0- Fast conversion. High resource use, but generally acceptable.q4_k_m- Recommended. Uses Q6_K for half of the attention.wv and feed_forward.w2 tensors, else Q4_K.q5_k_m- Recommended. Uses Q6_K for half of the attention.wv and feed_forward.w2 tensors, else Q5_K.
[NEW] To finetune and auto export to Ollama, try our Ollama notebook
Now, use the ministral_finetune.Q8_0.gguf file or ministral_finetune.Q4_K_M.gguf file in llama.cpp.
And we're done! If you have any questions on Unsloth, we have a Discord channel! If you find any bugs or want to keep updated with the latest LLM stuff, or need help, join projects etc, feel free to join our Discord!
Some other resources:
- Train your own reasoning model - Llama GRPO notebook Free Colab
- Saving finetunes to Ollama. Free notebook
- Llama 3.2 Vision finetuning - Radiography use case. Free Colab
- See notebooks for DPO, ORPO, Continued pretraining, conversational finetuning and more on our documentation!
This notebook and all Unsloth notebooks are licensed LGPL-3.0.



