Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //main.cpp
- #include <iostream>
- #include <grid.h>
- int main()
- {
- grid Grid(9);
- if(Grid.gridFromFile("/home/felblade/sudoku/sudoku/grid.txt"))
- std::cout << "Successful read\n\n";
- else std::cout << "Error\n";
- Grid.output();
- std::cout << std::endl;
- grid Gridd(Grid);
- Gridd.output();
- return 0;
- }
- //grid.h
- #ifndef GRID_H
- #define GRID_H
- #include <iostream>
- #include <math.h>
- #include <fstream>
- class grid {
- public:
- int gridSize; // the size of full grid
- int sectorSize; // the size of sector ( sqrt(gridSize) )
- int **matrix; // the dynamic 2d matrix
- grid(int gridSize); // constructor with one parameter
- grid(const grid&); // constructor of copy
- bool gridFromFile(char *filepath); // get the grid from file
- void output(); // prints the grid into console
- ///////////////////////////////////////////////////////////////////////////////////////
- //solving task methods
- bool findUnassignedLocation(int &row, int &col);
- bool findSolution();
- bool usedInRow(int digit, int row); // checks if same digit in row
- bool usedInCol(int digit, int col); // checks if same digit in col
- bool usedInBox(int digit, int row, int col);// checks if same digit in box
- bool isSafe(int digit, int row, int col); // cheks if it is legal to assign digit to current location
- };
- #endif // GRID_H
- //grid.cpp
- #include "grid.h"
- grid::grid(int gridSize) {
- this->gridSize = gridSize;
- this->sectorSize = sqrt(gridSize);
- matrix = new int*[gridSize];
- for(int i = 0; i < gridSize; i++)
- matrix[i] = new int[gridSize];
- }
- grid::grid(const grid &prototype) {
- this->gridSize = prototype.gridSize;
- this->sectorSize = prototype.sectorSize;
- matrix = new int*[gridSize];
- for(int i = 0; i < gridSize;i++)
- matrix[i] = new int[gridSize];
- for(int i = 0; i < gridSize; i++)
- for(int j = 0; j < gridSize; j++)
- matrix[i][j] = prototype.matrix[i][j];
- }
- bool grid::gridFromFile(char *filepath) {
- FILE *fp;
- if(fp = std::fopen(filepath, "r")) {
- for(int i = 0; i < gridSize; i++)
- for(int j = 0; j < gridSize; j++)
- std::fscanf(fp, "%i", &matrix[i][j]);
- fclose(fp);
- return true;
- }
- else
- return false;
- }
- void grid::output() {
- for(int i = 0; i < gridSize; i++) {
- for(int j = 0; j < gridSize; j++) {
- std::cout << matrix[i][j] << " ";
- if(!((j+1)%sectorSize)) std::cout << " ";
- }
- if(!((i+1)%sectorSize)) std::cout << std::endl;
- std::cout << std::endl;
- }
- }
- bool grid::usedInRow(int digit, int row) {
- for(int col = 0; col < gridSize; col++)
- if(matrix[row][col] == digit)
- return true;
- return false;
- }
- bool grid::usedInCol(int digit, int col) {
- for(int row = 0; row < gridSize; row++)
- if(matrix[row][col] == digit)
- return true;
- return false;
- }
- bool grid::usedInBox(int digit, int row, int col) {
- row = row - row % sectorSize;
- col = col - col % sectorSize;
- for(int sRow = 0; sRow < sectorSize; sRow++)
- for(int sCol = 0; sCol < sectorSize; sCol++)
- if(matrix[row+sRow][col+sCol] == digit)
- return true;
- return false;
- }
- bool grid::isSafe(int digit, int row, int col) {
- return !usedInRow(digit, row) && !usedInCol(digit, col) &&
- !usedInBox(digit, row, col);
- }
- bool grid::findSolution() {
- grid *temp = new grid(*this);
- temp->solve();
- }
- bool grid::solve() {
- int row, col;
- if(!this->findUnassignedLocation(row,col)) {
- this->output();
- return true;
- }
- for(int digit = 1; digit <= this->gridSize; digit++)
- if(this->isSafe(digit, row, col)) {
- this->matrix[row][col] = digit;
- if(this->solve())
- return true;
- this->matrix[row][col] = 0;
- }
- //delete &this;
- return false;
- }
- bool grid::findUnassignedLocation(int &row, int &col) {
- for(row = 0; row < gridSize; row++)
- for(col = 0; col < gridSize; col++)
- if(matrix[row][col] == 0)
- return true;
- return false;
- }
- //grid.txt
- 8 0 0 0 0 0 0 0 0
- 0 0 3 6 0 0 0 0 0
- 0 7 0 0 9 0 2 0 0
- 0 5 0 0 0 7 0 0 0
- 0 0 0 0 4 5 7 0 0
- 0 0 0 1 0 0 0 3 0
- 0 0 1 0 0 0 0 6 8
- 0 0 8 5 0 0 0 1 0
- 0 9 0 0 0 0 4 0 0
Advertisement
Add Comment
Please, Sign In to add comment