我需要帮助找出错误以及如何解决问题。我试图创建一个程序来验证数独板是否有效。我检查了一行是否有效,但是方格验证是我挣扎的地方。这是我的代码:
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <pthread.h>
using namespace std;
int *board[9];
int row, col;
void *is_row_ok(void *param);
void *is_col_ok(void *param);
void *is_square_ok(void *param);
int main()
{
for (int i = 0; i < 9; ++i)
{
board[i] = new int[9];
}
string line;
ifstream myFile("Testfile3.txt");
for (int row = 0; row < 9; ++row)
{
string line;
getline(myFile, line);
stringstream iss(line);
cout << endl;
for (int col = 0; col < 9; ++col)
{
string val;
getline(iss, val, ',');
if (!iss.good())
break;
stringstream convertor(val);
convertor >> board[row][col];
cout << board[row][col] << " ";
}
}
pthread_t thread1;
pthread_t thread2;
pthread_t thread3;
pthread_create(&thread1, NULL, is_row_ok, NULL);
pthread_create(&thread2, NULL, is_col_ok, NULL);
pthread_create(&thread3, NULL, is_square_ok, NULL);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
pthread_join(thread3, NULL);
cout << endl;
return 0;
}
void *is_col_ok(void *param)
{
int element_count = 0;
char element_value;
for (int i = 0; i < 9; ++i)
{
for (int j = 0; j < 9; ++j)
{
element_count = 0;
element_value = board[i][j];
if (element_value != ' ')
{
for (int k = 0; k < 9; ++k)
{
if (board[k][j] == element_value)
element_count++;
}
}
if (element_count >= 2)
{
cout << "Column " << j << " is invalid." << endl;
break;
}
else
{
cout << "Column " << j << " is valid." << endl;
}
}
}
pthread_exit(NULL);
}
void *is_square_ok (void *param)
{
int square = 0;
int element_count = 0;
char element_value;
for (int m = 0; m < 9; m+3)
{
for (int n = 0; n < 9; n+3)
{
for (int i = m; i < m+3; i++)
{
for (int j = n; j < n+3; j++)
{
element_count = 0;
element_value = board[i][j];
if (element_value != ' ')
{
for (int k = m; k < m+3; k++)
{
for (int l = n; l < n+3; l++)
{
if (board[k][l] == element_value)
element_count++;
}
}
}
}
}
}
square++;
if (element_count >= 2)
{
cout <<"Square " << square << " is invalid." << endl;
}
else
{
cout <<"column " << square << " is valid." << endl;
}
}
pthread_exit(NULL);
}
我故意省略了行检查功能,因为它工作正常。如果有人可以帮我弄清楚如何修复我的方形检查功能,我将不胜感激。
P.S。如果有人可以查看我的列检查功能,看看我怎么能只打印出一次列验证,那也非常有帮助。目前,由于外循环,它打印出我认为9次的每列的验证。
提前谢谢。
答案 0 :(得分:2)
for (int m = 0; m < 9; m+3)
在这里,&#34; m + 3&#34;什么都不做。它应该是m + = 3。