Daa (PBL) New3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

KIET Group of Institutions, Ghaziabad

Computer Science And Information Technology

PBLReport

on

Sudoku Solver

Submitted to: Ms. Swasti Singhal

Submitted By:
Khushi Pachauri
BTech/CSIT(5-B)
2100290110077
INDEX

1. Problem Statement
2. Algorithm
3. Program with Output
4. Scope
5. Extension Declaration
6. Conclusion
PROBLEM STATEMENT

Solving Sudoku has been a challenging problem in the last decade. The purpose has been to

develop a more effective algorithm in order to reduce the computing time and utilize lower

memory space. This essay develops an algorithm for solving Sudoku puzzle by using a method,

called pencil-and-paper algorithm. This algorithm resembles human methods, it describes how

a person tries to solve the puzzle by using certain techniques. Our ambition is to implement the

pencil-and-paper algorithm by using these techniques.

There are currently different variants of Sudoku such as 4X4 grids, 9X9 grids and 16X16 grids.

This work is focused on classic and regular Sudoku of 9X9 board, and then a comparison is

performed between the paper-and-pencil method and the Brute force algorithm. Hopefully, by

doing this work we might be able to answer the following questions: How does the pencil-

andpaper algorithm differ from the Brute force algorithm? Which one of them is more

effective?

Is it possible to make these algorithms more efficient?


ALGORITHM

Back Tracking

The unique missing method and the naked single method are able to solve all puzzles with easy

and medium levels of difficulty. In order to solve puzzles with even more difficult levels such

as hard and evil the backtracking method has been used to complete the algorithm. A human

player solves the puzzle by using simple techniques. If the puzzle is not solvable by using the

techniques the player then tries to fill the rest of the empty squares by guessing.

The backtracking method, which is similar to the human strategy (guessing), is used as a help

method to the pencil-and-paper algorithm. In other words, if the puzzle cannot be filled when

using the unique missing method and the naked single method, the backtracking method will

take the puzzle and fill the rest of the empty squares. Generally, the backtracking method finds

an empty square and assigns the lowest valid number in the square once the content of other

squares in the same row, column and box are considered. However, if none of the numbers from

1 to 9 are valid in a certain square, the algorithm backtracks to the previous square, which was

filled recently.

The above-mentioned methods are an appropriate combination to solve any Sudoku puzzles.

The naked single method can find quickly single candidates for the empty squares that need

only one single value. Since the puzzle comes to its end solution the unique missing method

can be used to fill the rest of the puzzles. Finally, if either method fills the board the algorithm

calls the backtracking method to fill the rest of the board.


PROGRAM WITH OUTPUT

Sample Code SOLVER.PY

board = [

[7,8,0,4,0,0,1,2,0],

[6,0,0,0,7,5,0,0,9],

[0,0,0,6,0,1,0,7,8],

[0,0,7,0,4,0,2,6,0],

[0,0,1,0,5,0,9,3,0],

[9,0.4,0,6,0,0,0,5],

[0,7,0,3,0,0,0,1,2],

[1,2,0,0,0,7,4,0,01,

[0,4.9.2,0,6,0,0,7] def

solve(bo):

find = find_empty(bo)

if not find: return

True clse:

row, col = find for i in

range(1,10): if valid(bo,

i, (row, col)):

bo[row][col] =1 if

solve(bo): return True

bo[row][col] = 0 return

False def valid(bo,

num, pos): # Check row

for i in

range(len(bo[0])): if

bo[pos[0]][i] == num

and pos[1] != i:

return False
# Check column

for i in range(len(bo)): if bo[i][pos[1]]

== num and pos[0] !=1i:

GULPY

import pygamc from working

import solve, valid import time

pygame.font.init() class Grid:

board =

[7,8,0,4,0,0,1,2,0],

[6,0,0,0,7,5,0,0,9],

[0,0,0,6,0,1,0,7,8],

[0,0,7,0,4,0,2,6,0],

[0,0,1,0,5,0,9,3,0],

[9,0,4,0,6,0,0,0, 5],

[0,7,0,3,0,0,0,1,2],

[1,2,0,0,0,7,4,0,0],

[0,4,9,2,0,6,0,0,7]

def __init__(sclf, rows, cols, width, height):

self.rows = rows self.cols = cols self.cubes = [[Cube(self.board[i][j], i, j, width,

height) for j in range(cols)] for i in range(rows)] self. width = width

self height = height

self.model = None

self.selected = None def

update_model(self):

self.model = [[self.cubes[i][j].value for j in range(self.cols)] for i in range(self.rows)]

def place(self, val): row, col = sell.selected if self.cubes[row][col].value == 0:

self.cubes[row][col].set(val) self.update_model() if

valid(sclf.model, val, (row,col)) and solve(sclf.model):

return True else:


self.cubes[row][col].set(0)

sell.cubes[row][col].set_temp(0)

self.update_model() return

(int(y),int(x)) clse:

return None def

is_finished(self): for i in

range(self.rows): for j in

range(self.cols):

if self.cubes[i][j].value == 0:

return False return True class Cube: rows =9

cols=9 def init__(self. value, row. col, width

height):

self. value = value


self.temp = 0 self.row

= row self.col = col

self.width = width

self height = height

self.selected = False

def draw(self, win):

fnt = pygame.font.SysFont("comicsans”, 40)

gap = self.width / 9 x = self.col * gap y =

self.orow * gap if self.temp I= 0 and

self.value == 0:

text = fnt.render(str(self.temp), 1, (128,128,128))

win.blit(text, (x+5, y+5)) elif not(self.value ==

0):

text = fnt.render(str(self.value), 1, (0, 0, 0)) win.blit(text, (x + (gap/2 -

text.get_width()/2), y + (gap/2 - text.get_height()/2))) if sclf.selected:


pygame.draw.rect(win, (255,0,0), (x.y. gap ,gap), 3)

def set(self, val): self value = val key=3 if

event.key == pygame.K_4:

key = 4 if event.key ==

pygame.K_35:

key=5 if event key ==

pygame .K_6:

key=6 if event key ==

pygame K_7:

key=7
if event key == pygame.K_8:

key =8 if event key key=9 if

event.key = pygame.K_DELETE:

board.clear() key = None pygame.K_9:

if event. key == pygame.K_ RETURN:

i, j = board selected if

board.cubes[i][j].temp != 0: if

board.place(board.cubes[i][j].temp):

print(" Success") else:

print("Wrong")

strikes += 1 key =

None if

board.is_finished():

print("Game over")

run = False

if event.type = pygame. MOUSEBUTTONDOWN:

pos = pygame.mouse.get_pos()

clicked = board.click(pos) if

clicked: board.select(clicked[0],

clicked[1]) key = None if

board.selected and key != None:


board.sketch(key) redraw_window(win, board,

play_time, strikes) pygame.display.update()

main() pygame.quit()
OUTPUT SCREENSHOTS
SCOPE

Sudoku is a logic-based puzzle that is played by numbers from 1 to 9. The Puzzle first appeared

in newspapers in November 1892 in France and then Howard Garns an American architect

presented it in its modern form [1,5]. There are already many journals, papers and essays that

researched Sudoku Solvers and most of them present different types of algorithms. Sudoku’s

popularity is based on several reasons. First of all, it is fun and fascinating, and very easy to

learn because of its simple rules. There are currently many different types of Sudoku puzzles,

classic Sudoku that contains a 9X9 grid with given clues in various places, and mini Sudoku

that consists of a grid with 4X4 or 6X6 sizes. The other type of Sudoku is Mega Sudoku which

contains a grid with 12X12 or 16X16 sizes [2]. In this text, the focus is mostly on the classic

Sudoku, i.e. 9X9 grid. Furthermore, Sudoku has become so popular, compared to other games,

all over the world because its rules are easy to understand it can improve our brain and also it

is fun.
Extension Declaration

Further research needs to be carried out in order to optimize the pencil-and-paper algorithm. A

possible way could be implementing of other human strategies (x-wings, swordfish, etc.). Other

alternatives might be to establish whether it is feasible to implement an algorithm based only

on human strategies so that no other algorithm is involved in the pencil-and-paper algorithm

and also make sure that these strategies can solve any puzzles with any level of difficulties.
CONCLUSION

This study has shown that the pencil-and-paper algorithm is a feasible method to solve any

Sudoku puzzles. The algorithm is also an appropriate method to find a solution faster and more

efficient compared to the back tracking algorithm. The proposed algorithm is able to solve such

puzzles with any level of difficulties in a short period of time (less than one second). The testing

results have revealed that the performance of the pencil-and-paper algorithm is better than the

back tracking algorithm with respect to the computing time to solve any puzzle.

The back tracking algorithm seems to be a useful method to solve any Sudoku puzzles and it

can guarantee to find at least one solution. However, this algorithm is not efficient because the

level of difficulties is irrelevant to the algorithm. In other words, the algorithm does not adopt

ntelligent strategies to solve the puzzles. This algorithm checks all possible solutions to the

puzzle until a valid solution is found which is a time consuming procedure resulting an

inefficient solver. As it has already stated the main advantage of using the algorithm is the

ability to solve any puzzles and a solution is certainly guaranteed.

You might also like