嗨,我正在大學做一個專案,它包括以某種方式解決數獨。首先,我們在一個位置尋找可能的數字,然后與同一 ror、colum 和 3x3 中位置的可能數字進行比較。如果有一個強制值,我們把它放在數獨中,如果沒有,我們跳到下一個位置。我的代碼主要運行良好,但有時我不知道為什么它會在無效的位置分配數字 1,因為數字 1 已經在行/列/3x3 中。
#include <iostream>
#include <vector>
using namespace std;
typedef vector<vector<int>> Matrix;
int charaint(char colum){
return colum-'A' 1;
}
char intachar(int colum){
return char(colum-1 'A');
}
//looking in the row for possible values in a position.
bool en_fila(const Matrix sudoku, int fil, int i){
bool trobat = false;
for (int col = 0; col < 9 and not trobat; col ){
if ((int)sudoku[fil][col] == i)
trobat = true;
}
return trobat;
}
//looking in the column for possible values in a position.
bool en_columna(const Matrix sudoku, int col, int i){
bool trobat = false;
for (int fil = 0; fil < 9 and not trobat; fil ){
if ((int)sudoku[fil][col] == i)
trobat = true;
}
return trobat;
}
//looking in the 3x3 for possible values in a position.
bool en_quadrat(const Matrix sudoku, int inifil, int inicol, int i){
bool trobat = false;
for (int fil = 0; fil < 3 and not trobat; fil ){
for (int col = 0; col < 3 and not trobat; col ){
if (sudoku[fil inifil][col inicol] == i)
trobat = true;
}
}
return trobat;
}
//looking if a value is possible in a position
bool es_possible(const Matrix sudoku, int fil, int col, int i)
{
return !en_fila(sudoku, fil, i) and !en_columna(sudoku, col, i) and !en_quadrat(sudoku, fil-fil%3, col-col%3, i);
}
//creating a vector with the possible values in a position
vector<int> possibles_en_casella(const Matrix sudoku, int fil, int col){
vector<int> p;
for (int i = 1; i <= 9; i ){
if (es_possible(sudoku, fil, col, i))
p.push_back(i);
}
return p;
}
//comparing vectors of the diferent positions of a row with a the vector of possibilities in a single position
bool possible_en_fila(const Matrix sudoku, int fil, int col, vector<int> p, int i){
bool trobat = false;
vector<int> paux;
for (int c = 0; c < 9 and not trobat; c ){
if ((col != c) and (sudoku[fil][c] == 0)){
paux = possibles_en_casella(sudoku, fil, c);
for (int j = 0; j < int(paux.size()); j ){
if (paux[j] == p[i]){
trobat = true;
}
}
}
}
return trobat;
}
//comparing vectors of the diferent positions of a column with a the vector of possibilities in a single position
bool possible_en_columna(const Matrix sudoku, int fil, int col, vector<int> p, int i){
bool trobat = false;
vector<int> paux;
for (int f = 0; f < 9 and not trobat; f ){
if ((fil != f) and (sudoku[f][col] == 0)){
paux = possibles_en_casella(sudoku, f, col);
for (int j = 0; j < int(paux.size()); j ){
if (paux[j] == p[i]){
trobat = true;
}
}
}
}
return trobat;
}
//comparing vectors of the diferent positions of a 3x3 with a the vector of possibilities in a single position
bool possible_en_quadrat(const Matrix sudoku, int fil, int col, vector<int> p, int i){
bool trobat = false;
int inicol = (col-col%3);
int inifil = (fil-fil%3);
vector<int> paux;
for (int f = 0; f < 3 and not trobat; f ){
for (int c = 0; c < 3 and not trobat; c ){
if ((f inifil != fil) and (c inicol != col) and (sudoku[f inifil][c inicol] == 0)){
paux = possibles_en_casella(sudoku, f inifil, col inicol);
for (int j = 0; j < int(paux.size()); j ){
if (paux[j] == p[i]){
trobat = true;
}
}
}
}
}
return trobat;
}
//comparing vectors of the diferent positions with a the vector of possibilities in a single position
vector<int> vector_possibles_en_casella(const Matrix sudoku, int fil, int col){
vector<int> p = possibles_en_casella(sudoku, fil, col);
vector<int> paux;
if ((int(p.size())) == 1 and (p[0] != 0)) return p;
else{
for (int i = 0; i < int(p.size()); i ){
if (!possible_en_fila(sudoku, fil, col, p, i) and !possible_en_columna(sudoku, fil, col, p, i) and !possible_en_quadrat(sudoku, fil, col, p, i)){
paux.push_back(i);
}
}
return paux;
}
}
//print the sudoku
void imprimir_sudoku(Matrix sudoku){
cout << " A B C D E F G H I" << endl;
for (int fil = 0; fil < 9; fil ){
if (fil == 3 or fil == 6){
cout << " ------- ------- -------" << endl;
}
cout << fil 1 << " ";
for (int col = 0; col < 9; col ){
if (col == 2 or col == 5){
if (sudoku[fil][col] == 0) cout << " . |";
else cout << " " << sudoku[fil][col] << " |";
}
else{
if (sudoku[fil][col] == 0) cout << " .";
else cout << " " << sudoku[fil][col];
}
}
cout << endl;
}
cout << endl;
}
int main(){
int fil, val;
char op, columna;
Matrix sudoku(9, vector<int> (9));
for (int f = 0; f < 9; f ){
for (int c = 0; c < 9; c ){
cin >> sudoku[f][c];
}
}
Matrix sudokuinicial = sudoku;
while (cin >> op and op != 'Z'){
//prints the vector of possible values in a given position
if (op == 'A'){
cin >> fil >> columna;
int col = charaint(columna);
if (sudoku[fil-1][col-1] != 0) cout << fil << columna << ": []";
else{
vector<int> p = possibles_en_casella(sudoku, fil-1, col-1);
cout << fil << columna << ": [" << p[0];
if (p.size() > 1){
for (unsigned int j = 1; j < p.size(); j ){
cout << ", " << p[j];
}
}
cout << "]";
}
cout << endl;
}
//looks if the given number is vaild in the given position
if (op == 'B'){
cin >> fil >> columna >> val;
int col = charaint(columna);
if (sudokuinicial[fil-1][col-1] != 0) cout << fil << columna << ": Casella no modificable" << endl;
else {
Matrix sudokuaux = sudoku;
sudokuaux[fil-1][col-1] = 0;
if (not hi_es(val,possibles_en_casella(sudokuaux, fil-1, col-1))){
cout << fil << columna << ": " << val << " es un valor no possible" << endl;
}
else{
sudoku[fil-1][col-1]=val;
}
}
}
//prints actual sudoku
if (op == 'C'){
imprimir_sudoku(sudoku);
}
if (op == 'R'){
Matrix sudokurini;
while (sudokurini != sudoku){
sudokurini = sudoku;
for (int i = 0; i < 9; i ){
for (int j = 0; j < 9; j ){
if (sudoku[i][j] == 0){
vector<int> paux = vector_possibles_en_casella(sudoku, i, j);
if ((int(paux.size()) == 1) /*and (paux[0] != 0)*/){
sudoku[i][j] = paux[0];
char columna = intachar(j 1);
cout << "A la casella (" << i 1 << "," << columna << ") hi ha d'anar un " << paux[0] << endl;
}
}
}
}
imprimir_sudoku(sudoku);
}
}
}
}
我只是復制了完整的代碼,但我的問題是當“R”是輸入(解決數獨的指令)時,選項 A、B 和 C 都可以。我認為問題出在這個函式上:
//comparing vectors of the diferent positions with a the vector of possibilities in a single position
vector<int> vector_possibles_en_casella(const Matrix sudoku, int fil, int col){
vector<int> p = possibles_en_casella(sudoku, fil, col);
vector<int> paux;
if ((int(p.size())) == 1 and (p[0] != 0)) return p;
else{
for (int i = 0; i < int(p.size()); i ){
if (!possible_en_fila(sudoku, fil, col, p, i) and !possible_en_columna(sudoku, fil, col, p, i) and !possible_en_quadrat(sudoku, fil, col, p, i)){
paux.push_back(i);
}
}
return paux;
}
}
抱歉,這些功能使用我的語言(加泰羅尼亞語)。
uj5u.com熱心網友回復:
這個回圈中有一個小但很重要的錯誤:
for (int i = 0; i < int(p.size()); i ){
if (!possible_en_fila(sudoku, fil, col, p, i) and !possible_en_columna(sudoku, fil, col, p, i) and !possible_en_quadrat(sudoku, fil, col, p, i)){
paux.push_back(i);
}
}
您正在遍歷一個 vector p,但不是從pto添加一個專案,而是paux為該向量分配一個索引。正確的版本是:
paux.push_back(p[i]);
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/393537.html
