背景信息:我正在编写一个C ++程序来解决数独难题,而且我遇到了一个重要的障碍。该计划的一般流程如下:
我在第二步遇到问题,我会将整个程序发布在底部,但这里只提供相关代码。
int* check_v(string g, size_t x, size_t y){
int *ans = new int[9]; //array of possible ints
memset(ans, 0, sizeof(ans)); //set array to all 0s
size_t size = 0;
for(int i = 1; i < 10; i++){ //iterate through 1-10
//check if i is in the col, if it isn't then add i to ans
bool placeable = true;
for(size_t j = 0; j < 9; j++){ //iterate through ints in the col
size_t r = (j + x) % 9; //the string is a 9x9 grid of numbers
cout << get(g,r,y) << " == " << i << " is " << (get(g,r,y) - 0 == i - 0);
//this is my debug statement, because the if below isn't working.
if(get(g,r,y) - 0 == i - 0){ //if i is equal to a num in the grid,
placeable = false;//we know it can't be that number
}
}
if(placeable) ans[size++] = i; //only add i if we didn't find it in the grid
}
return ans;
}
这是检查每个数字列的方法之一,以查看尚未存在的数字。
这是相关的get()方法:
char get(string g, size_t x, size_t y){
return g.at(x * 9 + y);
}
其中g是一串数字0-9 81个字母长。这是一个9x9网格,但是放入一个长字符串。
所以get(g,r,y)返回一个像'6'的字符,而我是一个int。我做'6' - 0使它们都是整数,然后比较它们。但是,它总是错误的!即使i = 6并得到(g,r,y)='6'。我比较错了吗?我必须在某个地方有一个错字,我只是没有看到它。这是来自该cout调用的一些示例输出,我将发布整个文件以供上下文使用。
//output
0 == 1 is 0
3 == 1 is 0
8 == 1 is 0
2 == 1 is 0
5 == 1 is 0
7 == 1 is 0
4 == 1 is 0
9 == 1 is 0
6 == 1 is 0 //this is all right, there aren't any 1s in the col
0 == 2 is 0
3 == 2 is 0
8 == 2 is 0
2 == 2 is 0 //but this is wrong! why isn't this true?
5 == 2 is 0
7 == 2 is 0
4 == 2 is 0
9 == 2 is 0
6 == 2 is 0
现在这里是整个文件,为您提供全貌。
using namespace std;
#include <iostream>
#include <cstring>
void print(string g){
for(size_t i = 0; i < 9; i++){
for(size_t j = 0; j < 9; j++){
cout << g.at(i * 9 + j);
}
cout << endl;
}
}
void set(string & g, size_t x, size_t y, char z){
size_t i = x * 9 + y;
string beg = g.substr(0,i);
string end = g.substr(i+1,g.length());
g = beg + z + end;
}
char get(string g, size_t x, size_t y){
return g.at(x * 9 + y);
}
int* check_v(string g, size_t x, size_t y){
int *ans = new int[9];
memset(ans, 0, sizeof(ans));
size_t size = 0;
for(int i = 1; i < 10; i++){
bool placeable = true;
for(size_t j = 0; j < 9; j++){
size_t r = (j + x) % 9;
cout << get(g,r,y) << " == " << i << " is " << (get(g,r,y) - 0 == i - 0) << endl;
if(get(g,r,y) - 0 == i - 0){
placeable = false;
}
}
if(placeable) ans[size++] = i;
}
return ans;
}
int* check_b(string g, size_t x, size_t y){
int *ans = new int[9];
memset(ans, 0, sizeof(ans));
size_t size = 0;
x = x / 3 * 3;
y = y / 3 * 3;
for(size_t i = 0; i < 3; i++){
bool placeable = true;
for(size_t j = 0; j < 3; j++)
if(get(g,x + i, y + j) == static_cast<char>(i))
placeable = false;
if(placeable) ans[size++] = i;
}
return ans;
}
int* check_h(string g, size_t x, size_t y){
int *ans = new int[9];
memset(ans, 0, sizeof(ans));
size_t size;
for(size_t i = 1; i < 10; i++){
bool placeable = true;
for(size_t j = 0; j < 9; j++){
cout << get(g,x,(j + y) % 9) << " == " << i << endl;
if(get(g,x,(y + j) % 9) == static_cast<char>(i)){
placeable = false;
}
}
if(placeable) ans[size++] = i;
}
return ans;
}
void check(string g, size_t x, size_t y){
int *n_v = check_v(g, x, y);
int *n_h = check_h(g, x, y);
int *n_b = check_b(g, x, y);
int n_y[9] = {0};
int n;
size_t size = 0;
cout << "vert: ";
for (int i = 0; i < 9; i++)
cout << n_v[i];
cout << endl << "hor: ";
for (int i = 0; i < 9; i++)
cout << n_h[i];
cout << endl << "box: ";
for (int i = 0; i < 9; i++)
cout << n_b[i];
cout << endl;
if(n_v[0] == 0 || n_h[0] == 0 || n_b[0] == 0)
cout << "Error, no number works in slot " << x << ", " << y << endl;
else{
if(n_v[1] == 0)
n = n_v[0];
else if(n_h[1] == 0)
n = n_h[0];
else if(n_b[1] == 0)
n = n_b[0];
}
for(size_t i = 0; i < 9; i++){
bool possible = true;
for(size_t j = 0; possible && j < 9; j++){
if(n_h[j] != n_v[i])
possible = false;
}
for(size_t j = 0; possible && j < 9; j++){
if(n_b[j] != n_v[i])
possible = false;
}
if(possible)
n_y[size++] = n_v[i];
}
if(n_y[1] == 0)
n = n_y[0];
if(n != 0){
char c = n;
set(g,x,y,c);
}
}
int main(){
//initializations
size_t dim = 9;
string data = "";
string one_row;
for (size_t r = 0; r < dim ; r = r + 1) {
cin >> one_row;
data += one_row;
}
//start solving
bool cont = true;
while(cont){
cont = false;
for(size_t i = 0; i < data.length(); i ++){
if(data.at(i) == '0'){
cont = true;
cout << "Checking at point " << i / 9 << ", " << i % 9 << endl;
string old = data;
check(data, i / 9, i % 9);
if(old.compare(data) != 0)
print(data);
}
}
}
print(data);
}
答案 0 :(得分:1)
此:
if(get(g,r,y) - 0 == i - 0)
不起作用。如果get返回一个char,它是表示数字CODE的字符串中的一个字符。你从中减去一个整数0,这不会减去任何东西。你想要的是
if(get(g,r,y) - '0' == i)
或者
if(get(g,r,y) == i + '0')
假设ascii(不是unicode或其他东西)。
您尝试做的是&#34;转换&#34;在ascii CHARACTER和一个整数之间。你可以转换到其中一个,而不是两个。 0和&#39; 0&#39;之间的差异是第一个是整数零,第二个是字符串中的0字符。
答案 1 :(得分:0)
(get(g,r,y) - 0 == i - 0);
这是错误的。改为使用它。
(get(g,r,y) - '0' == i - 0);