SlideShare a Scribd company logo
SUDOKU USING
BACK TRACKING
“PLAY IT THE CODE WAY”
B
Y
Abhishek kumar singh
@Code_do_it_better
2
Sudoku?
A simple but addictive puzzle, invented in the
USA in 1979 and called Number Place;
Became popular in Japan in 1986, where it was
renamed Sudoku (~ “single number”);
First appeared in UK newspapers in 2004, and
became an international craze in 2005.
3
How it’s played ?
The matrix(9*9) is partially filled.
You have to fill it such that:
1-> Each row contains all of 9 digits
from 1 to 9.
2-> Each col contains all of 9 digits
from 1 to 9.
3-> Each (3*3) matrix separated by
Dark lines contains all of 9
digits from 1 to 9.
2 1 3 8
5
7 6
1 3
9 8 1 2 5 7
3 1 8
9 8 2
5 6 9 7 8 4
4 2 5
“THAT’S WHAT PEOPLE KNOW ABOUT SUDOKU”
ONLY THING THAT SEPARATE US…….
CODER’S KNOW A BIT MORE.
SO LET’S LEARN TO DO IT OUR WAY.
WHAT IS BACKTRACKING ?
•As the name suggests we backtrack to find the solution. We
start with one possible move out of many available moves
and try to solve the problem if we are able to solve the
problem with the selected move then we will print the
solution else we will backtrack and select some other move
and try to solve it. If none if the moves work out we will
claim that there is no solution for the problem.
•Recursion is the key in backtracking
programming.
BACKTRACKING ALGORITHM
Find row, column of an unassigned cell If there is none,
return true .
For digits from 1 to 9
a) If there is no conflict for digit at row, column and 3*3
matrix box, assign digit to row, column .
b) Recursively try fill in rest of grid.
c) If recursion successful, return true.
c) Else, remove digit and try another If all digits have
been tried and nothing worked, return false.
We are going to solve the above puzzle using…..
Example:
Solve the given Sudoku
problem.
1 1
1 1
4 982
1 4 921 4 92
1 11 1 421 62 4 9 8 1 62 4 98
MOVING THROUGH THE NEXT LINES AND
FOLLOWING THE SAME PROCEDURE WE
WILL BE ABLE TO SOLVE THIS PROBLEM
WITH BACKTRACKING ALGORITHM.
5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 3 4 8
1 9 8 3 4 2 5 6 7
8 5 9 7 6 1 4 2 3
4 2 6 8 5 3 7 9 1
7 1 3 9 2 4 8 5 6
9 6 1 5 3 7 2 8 4
2 8 7 4 1 9 6 3 5
3 4 5 2 8 6 1 7 9
Bool SolveSudoku(grid[N][N])
{
int row, col;
if (!FindUnassignedLocation(grid, row, col))
return true;
do for num = 1 to 9
{
if (isSafe(grid, row, col, num))
{
grid[row][col] = num;
if (SolveSudoku(grid))
return true;
grid[row][col] = UNASSIGNED;
}
}
return false;
}
IMPLEMENTATION
Some Important Points
•Backtracking algorithm will search for every possible
solution.
•If You have a proper question, this method will
definitely provide a solution.
•Backtracking always provides a solution but it uses a lot
of time and space to solve it.
•If problem is large enough, backtracking will take a lot
of time and space and it’s worthless to use it.

More Related Content

What's hot

Control Strategies in AI
Control Strategies in AIControl Strategies in AI
Control Strategies in AI
Amey Kerkar
 
The n Queen Problem
The n Queen ProblemThe n Queen Problem
The n Queen Problem
Sukrit Gupta
 
Sudoku Solving with Computational Intelligence
Sudoku Solving with Computational IntelligenceSudoku Solving with Computational Intelligence
Sudoku Solving with Computational Intelligence
haraldhiss
 
Graph coloring using backtracking
Graph coloring using backtrackingGraph coloring using backtracking
Graph coloring using backtracking
shashidharPapishetty
 
Dbms lab Manual
Dbms lab ManualDbms lab Manual
Dbms lab Manual
Vivek Kumar Sinha
 
AI_Session 10 Local search in continious space.pptx
AI_Session 10 Local search in continious space.pptxAI_Session 10 Local search in continious space.pptx
AI_Session 10 Local search in continious space.pptx
Asst.prof M.Gokilavani
 
Sum of subset problem.pptx
Sum of subset problem.pptxSum of subset problem.pptx
Sum of subset problem.pptx
V.V.Vanniaperumal College for Women
 
Sum of subsets problem by backtracking 
Sum of subsets problem by backtracking Sum of subsets problem by backtracking 
Sum of subsets problem by backtracking 
Hasanain Alshadoodee
 
NP completeness
NP completenessNP completeness
NP completeness
Amrinder Arora
 
simple problem to convert NFA with epsilon to without epsilon
simple problem to convert NFA with epsilon to without epsilonsimple problem to convert NFA with epsilon to without epsilon
simple problem to convert NFA with epsilon to without epsilon
kanikkk
 
Backtracking
Backtracking  Backtracking
Backtracking
Vikas Sharma
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
BackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesBackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and Examples
Fahim Ferdous
 
Fundamentals of algorithms
Fundamentals of algorithmsFundamentals of algorithms
Fundamentals of algorithms
Amit Kumar Rathi
 
Lecture 26 local beam search
Lecture 26 local beam searchLecture 26 local beam search
Lecture 26 local beam search
Hema Kashyap
 
Prim's algorithm
Prim's algorithmPrim's algorithm
Prim's algorithm
Pankaj Thakur
 
data structures- back tracking
data structures- back trackingdata structures- back tracking
data structures- back tracking
Abinaya B
 
sum of subset problem using Backtracking
sum of subset problem using Backtrackingsum of subset problem using Backtracking
sum of subset problem using Backtracking
Abhishek Singh
 
recursive transition_networks
recursive transition_networksrecursive transition_networks
recursive transition_networks
Rajendran
 
Fractional Knapsack Problem
Fractional Knapsack ProblemFractional Knapsack Problem
Fractional Knapsack Problem
harsh kothari
 

What's hot (20)

Control Strategies in AI
Control Strategies in AIControl Strategies in AI
Control Strategies in AI
 
The n Queen Problem
The n Queen ProblemThe n Queen Problem
The n Queen Problem
 
Sudoku Solving with Computational Intelligence
Sudoku Solving with Computational IntelligenceSudoku Solving with Computational Intelligence
Sudoku Solving with Computational Intelligence
 
Graph coloring using backtracking
Graph coloring using backtrackingGraph coloring using backtracking
Graph coloring using backtracking
 
Dbms lab Manual
Dbms lab ManualDbms lab Manual
Dbms lab Manual
 
AI_Session 10 Local search in continious space.pptx
AI_Session 10 Local search in continious space.pptxAI_Session 10 Local search in continious space.pptx
AI_Session 10 Local search in continious space.pptx
 
Sum of subset problem.pptx
Sum of subset problem.pptxSum of subset problem.pptx
Sum of subset problem.pptx
 
Sum of subsets problem by backtracking 
Sum of subsets problem by backtracking Sum of subsets problem by backtracking 
Sum of subsets problem by backtracking 
 
NP completeness
NP completenessNP completeness
NP completeness
 
simple problem to convert NFA with epsilon to without epsilon
simple problem to convert NFA with epsilon to without epsilonsimple problem to convert NFA with epsilon to without epsilon
simple problem to convert NFA with epsilon to without epsilon
 
Backtracking
Backtracking  Backtracking
Backtracking
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 
BackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesBackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and Examples
 
Fundamentals of algorithms
Fundamentals of algorithmsFundamentals of algorithms
Fundamentals of algorithms
 
Lecture 26 local beam search
Lecture 26 local beam searchLecture 26 local beam search
Lecture 26 local beam search
 
Prim's algorithm
Prim's algorithmPrim's algorithm
Prim's algorithm
 
data structures- back tracking
data structures- back trackingdata structures- back tracking
data structures- back tracking
 
sum of subset problem using Backtracking
sum of subset problem using Backtrackingsum of subset problem using Backtracking
sum of subset problem using Backtracking
 
recursive transition_networks
recursive transition_networksrecursive transition_networks
recursive transition_networks
 
Fractional Knapsack Problem
Fractional Knapsack ProblemFractional Knapsack Problem
Fractional Knapsack Problem
 

Viewers also liked

Presentation - Sudoku Assignment
Presentation - Sudoku  AssignmentPresentation - Sudoku  Assignment
Presentation - Sudoku Assignment
Cj Uni
 
Sudoku powerpoint
Sudoku powerpointSudoku powerpoint
Sudoku powerpoint
union40
 
Sudoku
SudokuSudoku
Sudoku
Elianed
 
Sudoku ppt
Sudoku pptSudoku ppt
Sudoku
SudokuSudoku
L1 Sudoku
L1 SudokuL1 Sudoku
L1 Sudoku
bnmoran
 
The Sudoku Cipher
The Sudoku Cipher The Sudoku Cipher
The Sudoku Cipher
Nefeli Zikou
 
Sudoku puzzles
Sudoku puzzlesSudoku puzzles
Sudoku puzzles
Sadia Zareen
 
บทเรียนการพัฒนาระบบเครือข่ายบริการทางการแพทย์
บทเรียนการพัฒนาระบบเครือข่ายบริการทางการแพทย์บทเรียนการพัฒนาระบบเครือข่ายบริการทางการแพทย์
บทเรียนการพัฒนาระบบเครือข่ายบริการทางการแพทย์
Thira Woratanarat
 
การดูแลสุขภาพระยะยาวเขตบริการสุขภาพที่ 9
การดูแลสุขภาพระยะยาวเขตบริการสุขภาพที่ 9การดูแลสุขภาพระยะยาวเขตบริการสุขภาพที่ 9
การดูแลสุขภาพระยะยาวเขตบริการสุขภาพที่ 9
ทอง บุญยศ
 
Clinical Practice Guideline for Dementia 2008
Clinical Practice Guideline for Dementia 2008Clinical Practice Guideline for Dementia 2008
Clinical Practice Guideline for Dementia 2008
Utai Sukviwatsirikul
 
Sudoku valencia 2
Sudoku valencia  2Sudoku valencia  2
Sudoku valencia 2
Alicia Díaz
 
ซูโดกุ สสวท
ซูโดกุ สสวทซูโดกุ สสวท
ซูโดกุ สสวท
Nat Krub
 
แนวทางเวชปฏิบัติภาวะสมองเสื่อม 2557
แนวทางเวชปฏิบัติภาวะสมองเสื่อม 2557แนวทางเวชปฏิบัติภาวะสมองเสื่อม 2557
แนวทางเวชปฏิบัติภาวะสมองเสื่อม 2557
Utai Sukviwatsirikul
 
การทบทวนสถานการณ์และกลไกจัดการความแตกฉานด้านสุขภาพ
การทบทวนสถานการณ์และกลไกจัดการความแตกฉานด้านสุขภาพการทบทวนสถานการณ์และกลไกจัดการความแตกฉานด้านสุขภาพ
การทบทวนสถานการณ์และกลไกจัดการความแตกฉานด้านสุขภาพ
Thira Woratanarat
 
คู่มือ การสื่อสารกับผู้ป่วยชาวต่างชาติ โรงพยาบาลนพรัตนราชธานี
คู่มือ การสื่อสารกับผู้ป่วยชาวต่างชาติ โรงพยาบาลนพรัตนราชธานีคู่มือ การสื่อสารกับผู้ป่วยชาวต่างชาติ โรงพยาบาลนพรัตนราชธานี
คู่มือ การสื่อสารกับผู้ป่วยชาวต่างชาติ โรงพยาบาลนพรัตนราชธานี
topsaby99
 
Cpg copd
Cpg copdCpg copd
ซูโดกุขนาด 9x9 ช่อง แบบตัวอักษร
ซูโดกุขนาด 9x9 ช่อง แบบตัวอักษรซูโดกุขนาด 9x9 ช่อง แบบตัวอักษร
ซูโดกุขนาด 9x9 ช่อง แบบตัวอักษรPawaputanon Mahasarakham
 
กายภาพบำบัดในผู้ป่วยAsthma
กายภาพบำบัดในผู้ป่วยAsthmaกายภาพบำบัดในผู้ป่วยAsthma
กายภาพบำบัดในผู้ป่วยAsthmaSureerut Physiotherapist
 
sudoku 9x9
sudoku 9x9sudoku 9x9
sudoku 9x9jucemir
 

Viewers also liked (20)

Presentation - Sudoku Assignment
Presentation - Sudoku  AssignmentPresentation - Sudoku  Assignment
Presentation - Sudoku Assignment
 
Sudoku powerpoint
Sudoku powerpointSudoku powerpoint
Sudoku powerpoint
 
Sudoku
SudokuSudoku
Sudoku
 
Sudoku ppt
Sudoku pptSudoku ppt
Sudoku ppt
 
Sudoku
SudokuSudoku
Sudoku
 
L1 Sudoku
L1 SudokuL1 Sudoku
L1 Sudoku
 
The Sudoku Cipher
The Sudoku Cipher The Sudoku Cipher
The Sudoku Cipher
 
Sudoku puzzles
Sudoku puzzlesSudoku puzzles
Sudoku puzzles
 
บทเรียนการพัฒนาระบบเครือข่ายบริการทางการแพทย์
บทเรียนการพัฒนาระบบเครือข่ายบริการทางการแพทย์บทเรียนการพัฒนาระบบเครือข่ายบริการทางการแพทย์
บทเรียนการพัฒนาระบบเครือข่ายบริการทางการแพทย์
 
การดูแลสุขภาพระยะยาวเขตบริการสุขภาพที่ 9
การดูแลสุขภาพระยะยาวเขตบริการสุขภาพที่ 9การดูแลสุขภาพระยะยาวเขตบริการสุขภาพที่ 9
การดูแลสุขภาพระยะยาวเขตบริการสุขภาพที่ 9
 
Clinical Practice Guideline for Dementia 2008
Clinical Practice Guideline for Dementia 2008Clinical Practice Guideline for Dementia 2008
Clinical Practice Guideline for Dementia 2008
 
Sudoku valencia 2
Sudoku valencia  2Sudoku valencia  2
Sudoku valencia 2
 
ซูโดกุ สสวท
ซูโดกุ สสวทซูโดกุ สสวท
ซูโดกุ สสวท
 
แนวทางเวชปฏิบัติภาวะสมองเสื่อม 2557
แนวทางเวชปฏิบัติภาวะสมองเสื่อม 2557แนวทางเวชปฏิบัติภาวะสมองเสื่อม 2557
แนวทางเวชปฏิบัติภาวะสมองเสื่อม 2557
 
การทบทวนสถานการณ์และกลไกจัดการความแตกฉานด้านสุขภาพ
การทบทวนสถานการณ์และกลไกจัดการความแตกฉานด้านสุขภาพการทบทวนสถานการณ์และกลไกจัดการความแตกฉานด้านสุขภาพ
การทบทวนสถานการณ์และกลไกจัดการความแตกฉานด้านสุขภาพ
 
คู่มือ การสื่อสารกับผู้ป่วยชาวต่างชาติ โรงพยาบาลนพรัตนราชธานี
คู่มือ การสื่อสารกับผู้ป่วยชาวต่างชาติ โรงพยาบาลนพรัตนราชธานีคู่มือ การสื่อสารกับผู้ป่วยชาวต่างชาติ โรงพยาบาลนพรัตนราชธานี
คู่มือ การสื่อสารกับผู้ป่วยชาวต่างชาติ โรงพยาบาลนพรัตนราชธานี
 
Cpg copd
Cpg copdCpg copd
Cpg copd
 
ซูโดกุขนาด 9x9 ช่อง แบบตัวอักษร
ซูโดกุขนาด 9x9 ช่อง แบบตัวอักษรซูโดกุขนาด 9x9 ช่อง แบบตัวอักษร
ซูโดกุขนาด 9x9 ช่อง แบบตัวอักษร
 
กายภาพบำบัดในผู้ป่วยAsthma
กายภาพบำบัดในผู้ป่วยAsthmaกายภาพบำบัดในผู้ป่วยAsthma
กายภาพบำบัดในผู้ป่วยAsthma
 
sudoku 9x9
sudoku 9x9sudoku 9x9
sudoku 9x9
 

Similar to Sudoku

Sudoku
SudokuSudoku
Sudoku
b p
 
Sudoku strategies richard dickerson
Sudoku strategies richard dickersonSudoku strategies richard dickerson
Sudoku strategies richard dickerson
Rodrigo
 
Decimal lattice multiplication
Decimal lattice multiplicationDecimal lattice multiplication
Decimal lattice multiplication
Kevin Cummins
 
Sudoku solve rmain
Sudoku solve rmainSudoku solve rmain
Sudoku solve rmain
Arafat Bin Reza
 
mini project new.pptx
mini project new.pptxmini project new.pptx
mini project new.pptx
NishantTiwari411
 
Math trick
Math trickMath trick
Math trick
Aisyah Siti
 
Math trick
Math trickMath trick
Math trick
karinasitip
 
Math Trick
Math TrickMath Trick
Math Trick
Sindy Ma'rufah
 
Solving sudoku
Solving sudokuSolving sudoku
Solving sudoku
newmy
 
23T1W3 Mathematics ppt on Number Sequence.ppt
23T1W3 Mathematics ppt on Number Sequence.ppt23T1W3 Mathematics ppt on Number Sequence.ppt
23T1W3 Mathematics ppt on Number Sequence.ppt
RuthAdelekun1
 
Magic squares
Magic squaresMagic squares
Magic squares
Jhenny Marie Juayong
 
5.5 back track
5.5 back track5.5 back track
5.5 back track
Krish_ver2
 
pppppppppppppppppPresentation1SUDOKU-1.pptx
pppppppppppppppppPresentation1SUDOKU-1.pptxpppppppppppppppppPresentation1SUDOKU-1.pptx
pppppppppppppppppPresentation1SUDOKU-1.pptx
AlfredCyrusRedulfin1
 
Recreational Mathematics
Recreational MathematicsRecreational Mathematics
Recreational Mathematics
MRIDUL GUPTA
 
Odd Permutations - Part 5 of The Mathematics of Professor Alan's Puzzle Square
Odd Permutations - Part 5 of The Mathematics of Professor Alan's Puzzle SquareOdd Permutations - Part 5 of The Mathematics of Professor Alan's Puzzle Square
Odd Permutations - Part 5 of The Mathematics of Professor Alan's Puzzle Square
Alan Dix
 
Recreational mathematics for MichMATYC 10 10
Recreational mathematics for MichMATYC 10 10Recreational mathematics for MichMATYC 10 10
Recreational mathematics for MichMATYC 10 10
nsattler
 
Mathematicalgames
MathematicalgamesMathematicalgames
Wynberg girls high-louise keegan-maths-grade9-maths revision quiz
Wynberg girls high-louise keegan-maths-grade9-maths revision quizWynberg girls high-louise keegan-maths-grade9-maths revision quiz
Wynberg girls high-louise keegan-maths-grade9-maths revision quiz
Wynberg Girls High
 
Solve Sudoku using Constraint Propagation- Search and Genetic Algorithm
Solve Sudoku using  Constraint Propagation- Search and Genetic AlgorithmSolve Sudoku using  Constraint Propagation- Search and Genetic Algorithm
Solve Sudoku using Constraint Propagation- Search and Genetic Algorithm
Ai Sha
 
Math review for 4th period exam
Math review for 4th period examMath review for 4th period exam
Math review for 4th period exam
Dulce Garza
 

Similar to Sudoku (20)

Sudoku
SudokuSudoku
Sudoku
 
Sudoku strategies richard dickerson
Sudoku strategies richard dickersonSudoku strategies richard dickerson
Sudoku strategies richard dickerson
 
Decimal lattice multiplication
Decimal lattice multiplicationDecimal lattice multiplication
Decimal lattice multiplication
 
Sudoku solve rmain
Sudoku solve rmainSudoku solve rmain
Sudoku solve rmain
 
mini project new.pptx
mini project new.pptxmini project new.pptx
mini project new.pptx
 
Math trick
Math trickMath trick
Math trick
 
Math trick
Math trickMath trick
Math trick
 
Math Trick
Math TrickMath Trick
Math Trick
 
Solving sudoku
Solving sudokuSolving sudoku
Solving sudoku
 
23T1W3 Mathematics ppt on Number Sequence.ppt
23T1W3 Mathematics ppt on Number Sequence.ppt23T1W3 Mathematics ppt on Number Sequence.ppt
23T1W3 Mathematics ppt on Number Sequence.ppt
 
Magic squares
Magic squaresMagic squares
Magic squares
 
5.5 back track
5.5 back track5.5 back track
5.5 back track
 
pppppppppppppppppPresentation1SUDOKU-1.pptx
pppppppppppppppppPresentation1SUDOKU-1.pptxpppppppppppppppppPresentation1SUDOKU-1.pptx
pppppppppppppppppPresentation1SUDOKU-1.pptx
 
Recreational Mathematics
Recreational MathematicsRecreational Mathematics
Recreational Mathematics
 
Odd Permutations - Part 5 of The Mathematics of Professor Alan's Puzzle Square
Odd Permutations - Part 5 of The Mathematics of Professor Alan's Puzzle SquareOdd Permutations - Part 5 of The Mathematics of Professor Alan's Puzzle Square
Odd Permutations - Part 5 of The Mathematics of Professor Alan's Puzzle Square
 
Recreational mathematics for MichMATYC 10 10
Recreational mathematics for MichMATYC 10 10Recreational mathematics for MichMATYC 10 10
Recreational mathematics for MichMATYC 10 10
 
Mathematicalgames
MathematicalgamesMathematicalgames
Mathematicalgames
 
Wynberg girls high-louise keegan-maths-grade9-maths revision quiz
Wynberg girls high-louise keegan-maths-grade9-maths revision quizWynberg girls high-louise keegan-maths-grade9-maths revision quiz
Wynberg girls high-louise keegan-maths-grade9-maths revision quiz
 
Solve Sudoku using Constraint Propagation- Search and Genetic Algorithm
Solve Sudoku using  Constraint Propagation- Search and Genetic AlgorithmSolve Sudoku using  Constraint Propagation- Search and Genetic Algorithm
Solve Sudoku using Constraint Propagation- Search and Genetic Algorithm
 
Math review for 4th period exam
Math review for 4th period examMath review for 4th period exam
Math review for 4th period exam
 

More from Abhishek Singh

0 1 knapsack using naive recursive approach and top-down dynamic programming ...
0 1 knapsack using naive recursive approach and top-down dynamic programming ...0 1 knapsack using naive recursive approach and top-down dynamic programming ...
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
Abhishek Singh
 
0 1 knapsack using branch and bound
0 1 knapsack using branch and bound0 1 knapsack using branch and bound
0 1 knapsack using branch and bound
Abhishek Singh
 
Knights tour on chessboard using backtracking
Knights tour on chessboard using backtrackingKnights tour on chessboard using backtracking
Knights tour on chessboard using backtracking
Abhishek Singh
 
RABIN KARP ALGORITHM STRING MATCHING
RABIN KARP ALGORITHM STRING MATCHINGRABIN KARP ALGORITHM STRING MATCHING
RABIN KARP ALGORITHM STRING MATCHING
Abhishek Singh
 
Naive string matching
Naive string matchingNaive string matching
Naive string matching
Abhishek Singh
 
15 puzzle problem using branch and bound
15 puzzle problem using branch and bound15 puzzle problem using branch and bound
15 puzzle problem using branch and bound
Abhishek Singh
 
kmp_algorithm (string matching)
kmp_algorithm (string matching)kmp_algorithm (string matching)
kmp_algorithm (string matching)
Abhishek Singh
 

More from Abhishek Singh (7)

0 1 knapsack using naive recursive approach and top-down dynamic programming ...
0 1 knapsack using naive recursive approach and top-down dynamic programming ...0 1 knapsack using naive recursive approach and top-down dynamic programming ...
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
 
0 1 knapsack using branch and bound
0 1 knapsack using branch and bound0 1 knapsack using branch and bound
0 1 knapsack using branch and bound
 
Knights tour on chessboard using backtracking
Knights tour on chessboard using backtrackingKnights tour on chessboard using backtracking
Knights tour on chessboard using backtracking
 
RABIN KARP ALGORITHM STRING MATCHING
RABIN KARP ALGORITHM STRING MATCHINGRABIN KARP ALGORITHM STRING MATCHING
RABIN KARP ALGORITHM STRING MATCHING
 
Naive string matching
Naive string matchingNaive string matching
Naive string matching
 
15 puzzle problem using branch and bound
15 puzzle problem using branch and bound15 puzzle problem using branch and bound
15 puzzle problem using branch and bound
 
kmp_algorithm (string matching)
kmp_algorithm (string matching)kmp_algorithm (string matching)
kmp_algorithm (string matching)
 

Recently uploaded

Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 

Recently uploaded (20)

Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 

Sudoku

  • 1. SUDOKU USING BACK TRACKING “PLAY IT THE CODE WAY” B Y Abhishek kumar singh @Code_do_it_better
  • 2. 2 Sudoku? A simple but addictive puzzle, invented in the USA in 1979 and called Number Place; Became popular in Japan in 1986, where it was renamed Sudoku (~ “single number”); First appeared in UK newspapers in 2004, and became an international craze in 2005.
  • 3. 3 How it’s played ? The matrix(9*9) is partially filled. You have to fill it such that: 1-> Each row contains all of 9 digits from 1 to 9. 2-> Each col contains all of 9 digits from 1 to 9. 3-> Each (3*3) matrix separated by Dark lines contains all of 9 digits from 1 to 9. 2 1 3 8 5 7 6 1 3 9 8 1 2 5 7 3 1 8 9 8 2 5 6 9 7 8 4 4 2 5
  • 4. “THAT’S WHAT PEOPLE KNOW ABOUT SUDOKU” ONLY THING THAT SEPARATE US……. CODER’S KNOW A BIT MORE. SO LET’S LEARN TO DO IT OUR WAY.
  • 5. WHAT IS BACKTRACKING ? •As the name suggests we backtrack to find the solution. We start with one possible move out of many available moves and try to solve the problem if we are able to solve the problem with the selected move then we will print the solution else we will backtrack and select some other move and try to solve it. If none if the moves work out we will claim that there is no solution for the problem. •Recursion is the key in backtracking programming.
  • 6. BACKTRACKING ALGORITHM Find row, column of an unassigned cell If there is none, return true . For digits from 1 to 9 a) If there is no conflict for digit at row, column and 3*3 matrix box, assign digit to row, column . b) Recursively try fill in rest of grid. c) If recursion successful, return true. c) Else, remove digit and try another If all digits have been tried and nothing worked, return false. We are going to solve the above puzzle using…..
  • 7. Example: Solve the given Sudoku problem.
  • 8. 1 1 1 1 4 982 1 4 921 4 92
  • 9. 1 11 1 421 62 4 9 8 1 62 4 98 MOVING THROUGH THE NEXT LINES AND FOLLOWING THE SAME PROCEDURE WE WILL BE ABLE TO SOLVE THIS PROBLEM WITH BACKTRACKING ALGORITHM.
  • 10. 5 3 4 6 7 8 9 1 2 6 7 2 1 9 5 3 4 8 1 9 8 3 4 2 5 6 7 8 5 9 7 6 1 4 2 3 4 2 6 8 5 3 7 9 1 7 1 3 9 2 4 8 5 6 9 6 1 5 3 7 2 8 4 2 8 7 4 1 9 6 3 5 3 4 5 2 8 6 1 7 9
  • 11. Bool SolveSudoku(grid[N][N]) { int row, col; if (!FindUnassignedLocation(grid, row, col)) return true; do for num = 1 to 9 { if (isSafe(grid, row, col, num)) { grid[row][col] = num; if (SolveSudoku(grid)) return true; grid[row][col] = UNASSIGNED; } } return false; } IMPLEMENTATION
  • 12. Some Important Points •Backtracking algorithm will search for every possible solution. •If You have a proper question, this method will definitely provide a solution. •Backtracking always provides a solution but it uses a lot of time and space to solve it. •If problem is large enough, backtracking will take a lot of time and space and it’s worthless to use it.