Exercise17_18.cpp

# Exercise17_18.cpp - This program finds just one solution...

This preview shows pages 1–2. Sign up to view the full content.

/* This program finds just one solution, Exercise 8_20 finds all possible solutions */ #include <iostream> using namespace std; int grid[9] [9]; // Hold Sudoku puzzle bool isFree[9] [9]; // isFree[i][j] is true if (i, j) needs to be filled void readAPuzzle(); bool isValid(int i, int j); void printResult(); bool search(int row, int column); void findFreeCells(); /** Read a Sudoku puzzle from the keyboard */ void readAPuzzle() { // Create a Scanner cout << "Enter a Sudoku puzzle:" << endl; for (int i = 0; i < 9; i++) for (int j = 0; j < 9; j++) cin >> grid[i] [j]; } /** Check whether grid[i][j] is valid in the grid */ bool isValid(int i, int j) { // Check whether grid[i][j] is valid at the i's row for (int column = 0; column < 9; column++) if (column != j && grid[i] [column] == grid[i] [j]) return false; // Check whether grid[i][j] is valid at the j's column for (int row = 0; row < 9; row++) if (row != i && grid[row] [j] == grid[i] [j]) return false; // Check whether grid[i][j] is valid in the 3 by 3 box for (int row = (i / 3) * 3; row < (i / 3) * 3 + 3; row++) for (int col = (j / 3) * 3; col < (j / 3) * 3 + 3; col++) if (row != i && col != j && grid[row] [col] == grid[i] [j]) return false; return true; // The current value at grid[i][j] is valid

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern