0% found this document useful (0 votes)
4 views5 pages

CAT 2 Program by Arpit Choudhary 24BAI1150

The document contains a C++ implementation of a Sudoku solver. It defines a 9x9 grid and includes functions to check if a number can be placed in a specific cell, solve the Sudoku puzzle, and print the grid. The main function initializes the grid and calls the solver, displaying the solution or indicating if no solution exists.

Uploaded by

samayoshi3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

CAT 2 Program by Arpit Choudhary 24BAI1150

The document contains a C++ implementation of a Sudoku solver. It defines a 9x9 grid and includes functions to check if a number can be placed in a specific cell, solve the Sudoku puzzle, and print the grid. The main function initializes the grid and calls the solver, displaying the solution or indicating if no solution exists.

Uploaded by

samayoshi3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

main.

cpp Share Run

50 grid[row] [col] num;


51 if (solveSudoku (grid))
52 return true;
53 grid[row] [col] = 0;
54
55
56
57 return false;
58 }
59
60 int main() {
61 int grid [N][N] = {
62 {5, 3, 0, 0, 7, 0, 0, 0, 0},
63 {6, 0, 0, 1, 9, 5, 0, 0, 0},
64 {0, 9, 8, 0, 0, 0, 0, 6, 0}.
65 {8, 0, 0, 0, 6, 0, 0, 0, 3},
66 {4, 0, 0, 8, 0, 3, 0, 0, 1},
67 {7, 0, 0, 0, 2, 0, 0, 0, 6},
68 {0, 6, 0, 0, 0, 0, 2, 8, 0},
69 {0, 0, 0, 4, 1, 9, 0, 0, 5}.
70 {0, 0, 0, 0, 8, 0, 0, 7, 9}
71
72
73 if (solveSudoku(grid) )
main.cpp : Share Run

1 #include <iostream>
2 using namespace std;
3
4 #define N 9
5

6- void printGrid (int grid [N] [N] ){


for (int row = 0; row < N; rowt+) {
3 for (int col = 0; col < N; col++)
9 cout << grid[row] [col] << " ";
10 cout << endl;
11
12 }
13
14- bool isSafe (int grid [N][NJ, int row, int col, int num) {
15 for (int x =0; x < N; xt+) {
16 if (grid [row][x] == num || grid [x][col] =num)
17 return false;
18
19
20 int startRow = row row % 3, startCol col col % 3;
21
22 for (int i =0; i <3; itt)
23 for (int j =0: j <3; j++)
24 if (grid[i + startRow] [j +startCol] == num)
Output
53 46 7 89 1 2
6 7 2 1 953 4 8
198 3 4 2 5 67
8 59 7 6 1 4 2 3
4 2 6 8537 9 1
7139 2 4 8 5 6
96 1 5 3 7 2 8 4
2 874 196 3 5
3 4 5 2 8 6 1 79

== Code Execution Success ful ==


main.cpp Share Run

57 return false;
58 }
59
60 int main() {
61 int grid [N] [N] = {
62 {5, 3, 0, 0, 7, 0, 0, 0, 0},
63 {6, 0, 0, 1, 9, 5, 0, 0, 0},
64 {0, 9, 8, 0, 0, 0, 0, 6, 0}.
65 {8, 0, 0, 0, 6, 0, 0, 0, 3}.
66 {4, 0, 0, 8, 0, 3, 0, 0, 1},
67 {7, 0, 0, 0, 2, 0, 0, 0, 6},
68 {0, 6, 0, 0, 0, 0, 2, 8, 0}.
69
{0, 0, 0, 4, 1, 9, 0, 0, 5}.
70 {0, 0, 0, 0, 8, 0, 0, 7, 9}
71 }:
72
73 if (solveSudoku(gr id))
74 printGrid (grid) ;
75 else
76 cout << "No solution exists";
77
78 return 0:
79 }
80
main.cpp Share Run

25 return false;
26
27 return true;
28 }
29
30 bool solveSudoku(int grid[NJ [NJ) {
31 int row, col;
32 bool empty = false;
33
34 for (rOW = 0; row < N; roW++) {
35 for (col = 0; col < N; col++) {
36 if (grid[ row] [col] == 0) {
37 empty = true;
38 break;
39
40 }
41 if (empty)
42 break;
43 }
44
45 if (!empty)
46 return true;
47
48 for (int num= 1; num <= 9: numt+) {

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy