SlideShare une entreprise Scribd logo
1  sur  12
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.

Contenu connexe

Tendances (20)

8 queens problem using ga
8 queens problem using ga8 queens problem using ga
8 queens problem using ga
 
Sudoku solve rmain
Sudoku solve rmainSudoku solve rmain
Sudoku solve rmain
 
Lecture 13 Criptarithmetic problem
Lecture 13 Criptarithmetic problemLecture 13 Criptarithmetic problem
Lecture 13 Criptarithmetic problem
 
Math in Focus: Singapore Math Community Institute (updated)
Math in Focus: Singapore Math Community Institute (updated) Math in Focus: Singapore Math Community Institute (updated)
Math in Focus: Singapore Math Community Institute (updated)
 
Maths puzzle
Maths puzzleMaths puzzle
Maths puzzle
 
long division ppt.pptx
long division ppt.pptxlong division ppt.pptx
long division ppt.pptx
 
Sudoku
SudokuSudoku
Sudoku
 
Systems of 3 Equations in 3 Variables
Systems of 3 Equations in 3 VariablesSystems of 3 Equations in 3 Variables
Systems of 3 Equations in 3 Variables
 
Math puzzles
Math puzzlesMath puzzles
Math puzzles
 
8 queen problem
8 queen problem8 queen problem
8 queen problem
 
Exponents and Polynomials
Exponents and Polynomials Exponents and Polynomials
Exponents and Polynomials
 
Types of Numbers
Types of NumbersTypes of Numbers
Types of Numbers
 
9.1 Systems of Linear Equations
9.1 Systems of Linear Equations9.1 Systems of Linear Equations
9.1 Systems of Linear Equations
 
Chomsky & Greibach Normal Forms
Chomsky & Greibach Normal FormsChomsky & Greibach Normal Forms
Chomsky & Greibach Normal Forms
 
The n Queen Problem
The n Queen ProblemThe n Queen Problem
The n Queen Problem
 
Sum of subsets problem by backtracking 
Sum of subsets problem by backtracking Sum of subsets problem by backtracking 
Sum of subsets problem by backtracking 
 
State Space Representation and Search
State Space Representation and SearchState Space Representation and Search
State Space Representation and Search
 
Knight’s tour algorithm
Knight’s tour algorithmKnight’s tour algorithm
Knight’s tour algorithm
 
BackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesBackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and Examples
 
N queen problem
N queen problemN queen problem
N queen problem
 

En vedette

Sudoku powerpoint
Sudoku powerpointSudoku powerpoint
Sudoku powerpointunion40
 
L1 Sudoku
L1 SudokuL1 Sudoku
L1 Sudokubnmoran
 
The Sudoku Cipher
The Sudoku Cipher The Sudoku Cipher
The Sudoku Cipher Nefeli Zikou
 
บทเรียนการพัฒนาระบบเครือข่ายบริการทางการแพทย์
บทเรียนการพัฒนาระบบเครือข่ายบริการทางการแพทย์บทเรียนการพัฒนาระบบเครือข่ายบริการทางการแพทย์
บทเรียนการพัฒนาระบบเครือข่ายบริการทางการแพทย์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 2008Utai Sukviwatsirikul
 
ซูโดกุ สสวท
ซูโดกุ สสวทซูโดกุ สสวท
ซูโดกุ สสวทNat Krub
 
แนวทางเวชปฏิบัติภาวะสมองเสื่อม 2557
แนวทางเวชปฏิบัติภาวะสมองเสื่อม 2557แนวทางเวชปฏิบัติภาวะสมองเสื่อม 2557
แนวทางเวชปฏิบัติภาวะสมองเสื่อม 2557Utai Sukviwatsirikul
 
การทบทวนสถานการณ์และกลไกจัดการความแตกฉานด้านสุขภาพ
การทบทวนสถานการณ์และกลไกจัดการความแตกฉานด้านสุขภาพการทบทวนสถานการณ์และกลไกจัดการความแตกฉานด้านสุขภาพ
การทบทวนสถานการณ์และกลไกจัดการความแตกฉานด้านสุขภาพThira Woratanarat
 
คู่มือ การสื่อสารกับผู้ป่วยชาวต่างชาติ โรงพยาบาลนพรัตนราชธานี
คู่มือ การสื่อสารกับผู้ป่วยชาวต่างชาติ โรงพยาบาลนพรัตนราชธานีคู่มือ การสื่อสารกับผู้ป่วยชาวต่างชาติ โรงพยาบาลนพรัตนราชธานี
คู่มือ การสื่อสารกับผู้ป่วยชาวต่างชาติ โรงพยาบาลนพรัตนราชธานีtopsaby99
 
ซูโดกุขนาด 9x9 ช่อง แบบตัวอักษร
ซูโดกุขนาด 9x9 ช่อง แบบตัวอักษรซูโดกุขนาด 9x9 ช่อง แบบตัวอักษร
ซูโดกุขนาด 9x9 ช่อง แบบตัวอักษรPawaputanon Mahasarakham
 
กายภาพบำบัดในผู้ป่วยAsthma
กายภาพบำบัดในผู้ป่วยAsthmaกายภาพบำบัดในผู้ป่วยAsthma
กายภาพบำบัดในผู้ป่วยAsthmaSureerut Physiotherapist
 
sudoku 9x9
sudoku 9x9sudoku 9x9
sudoku 9x9jucemir
 
คู่มือโรคหลอดเลือดสมอง สำหรับประชาชน (อัมพาต อัมพฤกษ์)
คู่มือโรคหลอดเลือดสมอง สำหรับประชาชน (อัมพาต อัมพฤกษ์)คู่มือโรคหลอดเลือดสมอง สำหรับประชาชน (อัมพาต อัมพฤกษ์)
คู่มือโรคหลอดเลือดสมอง สำหรับประชาชน (อัมพาต อัมพฤกษ์)Utai Sukviwatsirikul
 

En vedette (20)

Sudoku powerpoint
Sudoku powerpointSudoku powerpoint
Sudoku powerpoint
 
Sudoku
SudokuSudoku
Sudoku
 
Sudoku ppt
Sudoku pptSudoku ppt
Sudoku ppt
 
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
 
Sudoku
SudokuSudoku
Sudoku
 
คู่มือโรคหลอดเลือดสมอง สำหรับประชาชน (อัมพาต อัมพฤกษ์)
คู่มือโรคหลอดเลือดสมอง สำหรับประชาชน (อัมพาต อัมพฤกษ์)คู่มือโรคหลอดเลือดสมอง สำหรับประชาชน (อัมพาต อัมพฤกษ์)
คู่มือโรคหลอดเลือดสมอง สำหรับประชาชน (อัมพาต อัมพฤกษ์)
 

Similaire à Sudoku

Sudoku
SudokuSudoku
Sudokub p
 
Sudoku strategies richard dickerson
Sudoku strategies richard dickersonSudoku strategies richard dickerson
Sudoku strategies richard dickersonRodrigo
 
Decimal lattice multiplication
Decimal lattice multiplicationDecimal lattice multiplication
Decimal lattice multiplicationKevin Cummins
 
Solving sudoku
Solving sudokuSolving sudoku
Solving sudokunewmy
 
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.pptRuthAdelekun1
 
5.5 back track
5.5 back track5.5 back track
5.5 back trackKrish_ver2
 
pppppppppppppppppPresentation1SUDOKU-1.pptx
pppppppppppppppppPresentation1SUDOKU-1.pptxpppppppppppppppppPresentation1SUDOKU-1.pptx
pppppppppppppppppPresentation1SUDOKU-1.pptxAlfredCyrusRedulfin1
 
Recreational Mathematics
Recreational MathematicsRecreational Mathematics
Recreational MathematicsMRIDUL 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 SquareAlan Dix
 
Recreational mathematics for MichMATYC 10 10
Recreational mathematics for MichMATYC 10 10Recreational mathematics for MichMATYC 10 10
Recreational mathematics for MichMATYC 10 10nsattler
 
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 quizWynberg 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 AlgorithmAi Sha
 
Math review for 4th period exam
Math review for 4th period examMath review for 4th period exam
Math review for 4th period examDulce Garza
 
Diabolic Str8ts #6, #7, and #9
Diabolic Str8ts #6, #7, and #9Diabolic Str8ts #6, #7, and #9
Diabolic Str8ts #6, #7, and #9SlowThinker
 

Similaire à 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
 
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
 
Diabolic Str8ts #6, #7, and #9
Diabolic Str8ts #6, #7, and #9Diabolic Str8ts #6, #7, and #9
Diabolic Str8ts #6, #7, and #9
 

Plus de 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 boundAbhishek Singh
 
Knights tour on chessboard using backtracking
Knights tour on chessboard using backtrackingKnights tour on chessboard using backtracking
Knights tour on chessboard using backtrackingAbhishek Singh
 
RABIN KARP ALGORITHM STRING MATCHING
RABIN KARP ALGORITHM STRING MATCHINGRABIN KARP ALGORITHM STRING MATCHING
RABIN KARP ALGORITHM STRING MATCHINGAbhishek 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 boundAbhishek Singh
 
kmp_algorithm (string matching)
kmp_algorithm (string matching)kmp_algorithm (string matching)
kmp_algorithm (string matching)Abhishek Singh
 
sum of subset problem using Backtracking
sum of subset problem using Backtrackingsum of subset problem using Backtracking
sum of subset problem using BacktrackingAbhishek Singh
 

Plus de Abhishek Singh (8)

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)
 
sum of subset problem using Backtracking
sum of subset problem using Backtrackingsum of subset problem using Backtracking
sum of subset problem using Backtracking
 

Dernier

Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 

Dernier (20)

Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 

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.