对递归sudoku_backtracker函数的模糊调用。 [英] ambigous call on recursive sudoku_backtracker function.

查看:219
本文介绍了对递归sudoku_backtracker函数的模糊调用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的程序,通过使用回溯算法来解决数独难题。程序将递归调用自身,直到它被解决或者它是不可解决的。问题是,当我运行它的编译器说,sudoku_backtracker()函数调用在sudoku_solver.cpp的第19行是不明确的。有人可以向我解释为什么它说,以及如何解决它。如果还有其他问题,我也会感谢您的帮助。非常感谢。

This is my program to solve a sudoku puzzle by using a backtracking algorithm. The program will recursively call itself until it is either solved or if it is unsolvable. The problem is that when I run it the compiler says that the sudoku_backtracker() function call in line 19 of sudoku_solver.cpp is ambiguous. Can someone please explain to me why it says that and how can I fix it. If there are other problems I would also appreciate the help. Thanks alot.

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include "sudoku_solver.hpp"

int main()
{


    std::vector< std::vector<int> > board(9,std::vector<int>(9));

    int i =0,j=0;

    for(std::string line;std::getline(std::cin,line);)
    {
        if(i==9)
        {
            i=0;
            break;
        }

        std::stringstream line_stream(line);
        for(std::string token;std::getline(line_stream,token,' ');)
        {
            if(j==9)
            {
                j=0;
            }
            board[i][j] = std::stoi(token);
            j++;
        }
        i++;
    }

    if(sudoku_backtracker(board)==1)
    {
        for(int i = 0;i<9;i++)
        {
            for(int j = 0;j<9;j++)
            {
                std::cout<<board[i][j];
            }
            std::cout<<endl;
        }
    }
    return 0;
}

/* This is sudoku_solver.hpp placed here for better organization on stackoverflow*/


#ifndef __SUDOKU_SOLVER_HPP__
#define __SUDOKU_SOLVER_HPP__
#include <vector>

int sudoku_backtracker(std::vector< std::vector<int> > board);

std::pair<int, int> find_empty_spot(std::vector< std::vector<int> > board);

bool checks_num_is_valid(std::vector< std::vector<int> > board,int row,int column,int number);

#endif //__SUDOKU_SOLVER_HPP__

/*this is sudoku_solver.cpp but I placed it after the header file so I can put it in block on stackoverflow*/


#include "sudoku_solver.hpp"
#include <iostream>
#include <string>

int sudoku_backtracker(std::vector< std::vector<int> > &board)
{
    int test_num = 1;
    std::pair<int,int> empty_spot = find_empty_spot(board);
    if(empty_spot.first == -1)
    {
        return 1;
    }

    while(test_num != 10)
    {
        if(checks_num_is_valid(board,empty_spot.first,empty_spot.second,test_num))
        {
            board[empty_spot.first][empty_spot.second] = test_num;
            int recursive_sudoku = sudoku_backtracker(board);
            if(recursive_sudoku==1)
            {
                return 1;
            }
            board[empty_spot.first][empty_spot.second] = 0;
        }

        test_num++;
    }
    return 0;
}


std::pair<int, int> find_empty_spot(std::vector< std::vector<int> > board,int row,int column)
{
    for(int row=0;row<9;row++)
    {
        for(int column=0;column<9;column++)
        {
            if(board[row][column] == 0){return std::make_pair(row,column);}
        }
    }
    return std::make_pair(-1,-1);
}

bool checks_num_is_valid(std::vector< std::vector<int> > board, int row,int column, int number)
{
    bool num_not_in_column = true;
    bool num_not_in_row = true;
    bool num_not_in_box = true;
    //box_start_row as bsr and box_start_column as bsc
    //this is the starting point to check the numbers inside the box and make
    //sure the test number is valid
    int bsr = 0,bsc = 0;

    //checks the numbers in the same column but different rows
    for(int i =0;i<9;i++)
    {
        if(i==row){continue;}
        if(board[i][column] == number){num_not_in_column = false;break;}
    }
    //checks numbers in the same row but different columns
    for(int i = 0;i<9;i++)
    {
        if(i==column){continue;}
        if(board[row][i] == number){num_not_in_row = false;break;}
    }

    //checks wether the numer is int the same box
    if(row<=2){bsr =0;}
    if(row>=3 && row<=5){bsr = 3;}
    if(row>=6 && row<=8){bsr = 6;}
    if(column <=2){bsc =0;}
    if(column>=3 && column<=5){bsc=3;}
    if(column>=6 && column<=8){bsc=6;}
    //double for loop to check all the values inside the box
    for(bsr;bsr<bsr+3;bsr++)
    {
        for(bsc;bsc<bsc+3;bsc++)
        {
            if(bsr==row && bsc==column)
            {continue;}
            else
            {
                if(board[bsr][bsc] == number)
                {
                    num_not_in_box = false;
                }
            }
        }
    }
    bool result = num_not_in_row && num_not_in_column && num_not_in_box;
    return result;
}


推荐答案

声明:

int sudoku_backtracker(std::vector< std::vector<int> > board);

与定义不符:

int sudoku_backtracker(std::vector< std::vector<int> > &board) { ... }
                                                      ^^^

这篇关于对递归sudoku_backtracker函数的模糊调用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆