帮助数独代码 [英] Help with Sudoku code

查看:77
本文介绍了帮助数独代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿那里。我写了一个解决数独谜题的程序,但是我无法正确执行它。我输入后程序没有响应;即程序没有继续。



这是代码:

< pre lang =c ++> #include < < span class =code-leadattribute> stdio.h >

int rowChk( int grid [] [ 9 ], int 行, int val)
{
int x;
for (x = 0 ; x< 9; x ++)
if (grid [row] [x] == val)
return 0 ;
return 1 ;
}

int colChk( int grid [] [< span class =code-digit> 9
], int col, int val)
{
int x;
for (x = 0 ; x< 9; x ++)
if (grid [x] [col] == val)
return 0 ;
return 1 ;
}

int boxChk( int grid [] [< span class =code-digit> 9
], int 行, int col, int val)
{
row =(row / 3)* 3 ;
col =(col / 3)* 3 ;
for (; row<(row + 3); row ++)
for (; col<(col + 3); col ++)
if (grid [row] [col] == val)
返回 0 ;
return 1 ;
}

void solve( int grid [] [< span class =code-digit> 9 ])
{
int i,j,ch1,ch2,ch3, K,状态;
k = 1 ;
status = 0 ;
for (i = 0 ; i< 9; i ++)
{< span class =code-keyword> for (j = 0 ; j< 9; j ++)
{
if (grid [i] [j] == 0 ||!status)
{
if (k> 9)
k = k%9;
grid [i] [j] = k ++;
}
ch1 = rowChk(grid,i,grid [i] [j]);
ch2 = colChk(grid,j,grid [i] [j]);
ch3 = boxChk(grid,i,j,grid [i] [j]);
if (ch1&& ch2&& ch3)
status = 1 ;
else
j--;
}
}
}

int main()
{
int i,j,grid [ 10 ] [ 10 < /跨度>];
printf( \ n .... Sudoku Solver .... \ n);
printf( 输入矩阵:\ n);
for (i = 0 ; i< 9; i ++)
的类=code-keyword>(j = 0 ; j< 9; j ++)
scanf( %d,& grid [i] [j]);
solve(grid);
printf( \ n解决难题:\ n);
for (i = 0 ; i< 9; i ++)
{< span class =code-keyword> for
(j = 0 ; j< 9; j ++)
printf( %d,grid [i] [j]);
printf( \ n);
}
return 0 ;
}





我试图使用gdb识别错误......显然执行在solve()函数处停止。

解决方案

你需要开始收集信息:目前你给我们的只是程序没有进行和显然执行停止在solve()函数 。

这不是很多 - 就像说我的车无法启动 - 这还不足以解决问题,这可能是因为缺乏汽油,通过电池电量不足,发动机在十分钟前爆炸了。



所以...你需要找出它在解决功能中的位置。

因此,在解决的第一个 for 循环中放置一个断点,然后运行你的程序。当它到达断点时,你的程序将停止并等待你继续。

告诉它继续。它是否再次击中断点?如果是这样,它已经通过内循环的第一次迭代,所以继续运行它直到它不回来。现在你知道它是通行证之一 - 你可以杀死它,再次运行它,但这一次跟着它。当第一次没有回来的时候,你会做同样的事情。



当你知道它会停止时,你会开始寻找原因 - 单步执行,使用调试器查看变量并确定它将要执行的代码行将在它执行之前完成。它做了你所期望的吗?如果没有,为什么不呢?



收集信息,告诉你发生了什么。没有它,你根本无法修复你的问题...


解决函数中infinte循环的原因是你的算法有一个主要问题。您采取的方法可以描述如下:



找到下一个免费单元格并尝试将数字1 ... 9输入单元格,直到那里在行,列和框中没有冲突。然后继续下一个免费单元格。





这种方法的问题在于你用这种方式找到的数字可能不是这个单元格唯一的无冲突解决方案。在以这种方式输入几个数字之后,您可能已经创建了一个明显错误的情况。如果没有产生冲突,您将无法找到下一个空闲单元格的数字,这是您的程序开始循环的时候。



另外:处理你的状态变量是错误的。它永远不会被重置。


Hey there. I wrote a program that solves Sudoku puzzles but I am not able to execute it properly. The program does not respond after I've fed it input;i.e, the program does not proceed.

Here's the code:

#include<stdio.h>

int rowChk(int grid[][9],int row,int val)
{
	int x;
	for(x=0;x<9;x++)
		if(grid[row][x]==val)
			return 0;
	return 1;
}

int colChk(int grid[][9],int col,int val)
{
	int x;
        for(x=0;x<9;x++)
                if(grid[x][col]==val)
                        return 0;
        return 1;
}

int boxChk(int grid[][9],int row,int col,int val)
{
	row=(row/3)*3;
	col=(col/3)*3;
	for(;row<(row+3);row++)
		for(;col<(col+3);col++)
			if(grid[row][col]==val)
				return 0;
	return 1;
}

void solve(int grid[][9])
{
	int i,j,ch1,ch2,ch3,k,status;
	k=1;
	status=0;
	for(i=0;i<9;i++)
	{	for(j=0;j<9;j++)
		{
			if(grid[i][j]==0 || !status)
			{
				if(k>9)
					k=k%9;
				grid[i][j]=k++;
			}
			ch1=rowChk(grid,i,grid[i][j]);
			ch2=colChk(grid,j,grid[i][j]);
			ch3=boxChk(grid,i,j,grid[i][j]);
			if(ch1&&ch2&&ch3)
				status=1;
			else
				j--;
		}
	}
}		

int main()
{
	int i,j,grid[10][10];
	printf("\n....Sudoku Solver....\n");
	printf("Enter the matrix:\n");
	for(i=0;i<9;i++)
		for(j=0;j<9;j++)
			scanf("%d",&grid[i][j]);
	solve(grid);
	printf("\nSolved puzzle:\n");
	for(i=0;i<9;i++)
	{	for(j=0;j<9;j++)
			printf("%d ", grid[i][j]);
		printf("\n");
	}
	return 0;
}



I tried to identify the bug using gdb...apparently the execution halts at solve() function.

解决方案

You need to start gathering information: at present all you have given us is "the program does not proceed" and "apparently the execution halts at solve() function".
This isn't a lot - it's like saying "my car won't start" - it isn't enough to solve the problem, which could be anything from a lack of petrol, through a flat battery, to the engine having exploded ten minutes earlier.

So... you need to work out where it is sitting in the solve function.
So, put a breakpoint on the first for loop in solve, and run your program. When it hits the breakpoint, your program will stop and wait for you before continuing.
Tell it to continue. Does it hit the breakpoint again? If so, it has got through the first iteration of the inner loop, so keep running it until it doesn't come back. Now you know it's one of the passes - and you can kill it, run it again and this time follow it though. You do the same thing when it doesn't come back the first time.

When you know it's going to stop, you start looking for why not - single step through, using the debugger to look at the variables and working out what the line of code it is about to execute will do before it does it. Did it do what you expected? If not, why not?

Gather info, and that tells you what is going on. Without it, you can't fix your problem at all...


The reason for the infinte loop in the solve function is that your algorithm has a principal problem. The approach you have take could be described as follows:

Find the next free cell and try entering the digits 1 ... 9 into the cell until there is no conflict in its row, column, and box. Then continue with the next free cell.


The problem with this approach is that the digit you find in this way might be not the only conflict free solution for this cell. And after you have entered a couple of digits in this manner you might have created a situation that is plainly wrong. You won't be able to find a digit for the next free cell without creating a conflict and this is when your program starts to loop.

Also: The handling of your status variable is wrong. It is never being reset.


这篇关于帮助数独代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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