Вопрос

I made a sudoku solver, but need to be able to take inputs in to set the puzzle before being solved. The input will be in a 1,2,,,,3,4 format where a space between 2 commas indicates a blank space (and obviously this will all be done row by row). I also have my code set up to use blank spots as 0s and would like to know how to read the input, parse each number into an int and also parse blanks as 0s.

As stated above, this is for C++ and is in Visual Studio Express 2013.

        // ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include "cstdlib"
#include "stdio.h"
#include "iostream"
#include "sstream"

using namespace std;
//find the first empty slot on the puzzle
bool FindEmptyCell(int puzzle[9][9], int &row, int &colm);
//check if the inserted number is a legal move
bool isLegal(int puzzle[9][9], int row, int colm, int num);

//being solving through backtracking
bool solve(int puzzle[9][9])
{
    int row, colm; //establish rows and columns
    //check if there are any empty slots
    if (!FindEmptyCell(puzzle, row, colm))
    {
        return true; //puzzle is assumed solved
    }

    else
    {
        //start backtracking with the number 1
        for (int i = 1; i<10; i++)
        {
            if (isLegal(puzzle, row, colm, i))
            {
                puzzle[row][colm] = i;
                if (solve(puzzle) == true)
                {
                    return true; //if there is no problem with the first number, move on
                }
                else
                {
                    puzzle[row][colm] = 0;
                }
            }
        }
    }
    return false; //start recursion to try next number
}


//check if the move is legal in the 3x3 square, needs the row and column of the square
bool CheckSquare(int puzzle[9][9], int sqRow, int sqColm, int chkNum)
{
    for (int row = 0; row < 3; row++)
    {
        for (int colm = 0; colm < 3; colm++)
        {
            if (puzzle[row + sqRow][colm + sqColm] == chkNum)
            {
                return true; //the number is there and the move is illegal
            }
        }
    }
    return false; //the number is not there and the move is assumed legal
}

//check if the move is legal in the row
bool CheckRow(int puzzle[9][9], int row, int chkNum)
{
    for (int colm = 0; colm <9; colm++)
    {
        if (puzzle[row][colm] == chkNum)
        {
            return true; //the number is there and the move is illegal
        }
    }
    return false; // the number is not there and the move is assumed legal
}

//check if the move is legal in the column
bool CheckColm(int puzzle[9][9], int colm, int chkNum)
{
    for (int row = 0; row <9; row++)
    {
        if (puzzle[row][colm] == chkNum)
        {
            return true; //the number is there and the move is illegal
        }
    }
    return false; // the number is not there and the move is assumed legal
}



//definition of finding empty slot method
bool FindEmptyCell(int puzzle[9][9], int &row, int &colm)
{
    for (colm = 0; colm < 9; colm++)
    {
        for (row = 0; row < 9; row++)
        {
            if (puzzle[row][colm] == 0)
            {
                return true;
            }
        }
    }
    return false;
}

//definition of is legal method
bool isLegal(int p[9][9], int r, int c, int cN)
{
    if (CheckRow(p, r, cN) == false)
    {
        if (CheckColm(p, c, cN) == false)
        {
            if (CheckSquare(p, r - r % 3, c - c % 3, cN) == false) //use % to find the beginning row/column of the square
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
    else
    {
        return false;
    }
}

int main()
{
    int puzzle[9][9];
    string input;
    for (int i=1; i <10; i++)
    {
        for (int j=1;j <10; j++)
        {        
        cout << "Please insert the number for ["<< i << "," << j << "] (0 for no number)" << endl;
        getline(cin, input);
        int value = atoi(input.c_str());
        puzzle[i-1][j-1] = value;
        //get puzzle into correct format
        }
    }


    if (solve(puzzle) == true)
    {
        string s;
        cout << "Solving the puzzle..." << endl;
        //print the puzzle to the screen
        for (int i = 0; i<9;i++)
        {
            for (int j = 0; j<9; j++)
            {
                cout << puzzle[i][j] << ","; 
            }
            cout << endl;
        }
        cout << "Press any button to escape";
        getline(cin, s);
    }
    else
    {
        cout << "Can not solve the puzzle" << endl;
    }
    return 0;
}

Not sure why you need the code but here it is. Everything works properly, it just doesnt take input in the correct format currently.

Это было полезно?

Решение

As your numbers are only of 1 digit, you can:

int puzzle[9][9] = {};
for (int i = 1; i < 10; ++i)
{
   cout << "Please input row #" << i << ": ";
   getline(cin, input);
   int j = 0;
   const char *ptr = input.c_str();
   while (*ptr)
   {
     if (isspace(*ptr)) ; // do nothing
     else if (*ptr == ',') ++j; // new column
     else puzzle[i - 1][j] = *ptr - '0';
     ++ptr;
   }
}

Другие советы

To parse empty spaces as zerors, you need:

  • Initialize your matrix to zeros int puzzle[9][9] = { };

  • read the input line by line (each line representing a whole row)

  • use function strtok() to split (separate) the values using delimeter `','

  • for each separated value, trim it (i.e. remove spaces). Then, Use function atoi to covert it as an integer

Notice that you should check for empty separated string if (strlen(str))

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top