我的目标是在使用回溯算法时解决数独游戏。当我运行它时,我收到此错误消息:
obj / sudoku_solver.o:在功能上
sudoku_backtracker(std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >, std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > >&)': /home/user/Documents/project2v1/src/sudoku_solver.cpp:8: undefined reference to
find_empty_spot(std :: __ 1 :: vector&gt;,std :: __ 1 :: allocator&gt;&gt;&gt;)&#39; clang:错误:链接器命令失败,退出代码为1(使用-v查看 调用) make: * [project2.out]错误1
我不明白发生了什么,如果有人能够解释这意味着什么以及我做错了什么,那将是一个很大的帮助。谢谢你们。
的main.cpp
#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;
}
sudoku_solver.hpp
#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__
sudoku_solver.cpp
#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;
}
答案 0 :(得分:2)
这次宣言:
std::pair<int, int> find_empty_spot(std::vector< std::vector<int> > board);
与定义不匹配:
std::pair<int, int> find_empty_spot(std::vector< std::vector<int> > board,int row,int column)
两种用法都不符合定义:
std::pair<int,int> empty_spot = find_empty_spot(board);