[杂项项目]纯C语言实现随机生成数独九宫格

一年前的程序,可以生成一个数独的九宫格。当时想这种方法想了3天,终于实现了。由于当时刚熟悉指针,因此指针用的比较多。本人比较懒,先把代码贴出来,后序补充具体实现方法。

#include 
#include 
#include 

void main()
{
	int a[9][9] = { 0 };
	int *p[3][3], *m[3][6], *n[3][6], *r[9][4], *aa[9][9];
	int **q[9];
	int i, j, k, l, h, t, s = 0;
	void rank(int *x, int *y);
	bool check(int a[9][9], int n);
	srand((int)time(0));

	for (i = 0; i < 3; i++)
		for (j = 0; j < 3; j++)
			p[i][j] = &a[i * 3 + 1][j * 3 + 1];
	for (k = 0; k < 9; k++)
		q[k] = &p[k / 3][k % 3];
	for (i = 0; i < 3; i++)
		for (j = 0; j < 6; j++)
		{
			m[i][j] = &a[i * 3 + 1][2 * j - j / 2];
			n[i][j] = &a[2 * j - j / 2][i * 3 + 1];
		}
	for (k = 0; k < 9; k++)
		for (l = 0; l < 4; l++)
			r[k][l] = &a[(k / 3) * 3 + (l / 2) * 2][(k % 3) * 3 + (l % 2) * 2];
	for (i = 0; i < 9; i++)
		for (j = 0; j < 9; j++)
			aa[i][j] = &a[i][j];
step1:
	for (i = 0; i < 9; i++)
	{
	next1:
		**q[i] = rand() % 10;
		if (**q[i] == 0)
			goto next1;
		for (j = 0; j < i; j++)
			if (**q[j] == **q[i])
				goto next1;
	}
step2:
	for (i = 0; i < 3; i++)
		for (j = 0; j < 6; j++)
		{
		next2:
			*m[i][j] = rand() % 10;
			if (*m[i][j] == 0)
				goto next2;
			for (k = 0; k < 3; k++)
				if (*m[i][j] == *p[i][k])
					goto next2;
			for (k = 0; k < j; k++)
				if (*m[i][j] == *m[i][k])
					goto next2;
			for (k = 0; k < i; k++)
				if (*m[i][j] == *m[k][j])
					goto step2;
		}
step3:
	for (i = 0; i < 3; i++)
	{
	next4:
		for (j = 0; j < 6; j++)
		{
		next3:
			*n[i][j] = rand() % 10;
			if (*n[i][j] == 0)
				goto next3;
			for (k = 0; k < 3; k++)
				if (*n[i][j] == *p[k][i])
					goto next3;
			for (k = 0; k < j; k++)
				if (*n[i][j] == *n[i][k])
					goto next3;
			for (l = 0; l < 2; l++)
			{
				if (*n[i][j] == *m[j / 2][i * 2 + l])
					goto next4;
			}
			for (k = 0; k < i; k++)
				if (*n[i][j] == *n[k][j])
					goto step3;
		}
	}
step4:
	for (i = 0; i < 3; i++)
	{
		for (h = 0; h < 2; h++)
		{
		next6:
			for (l = 0; l < 3; l++)
				for (j = 0; j < 2; j++)
				{
				next5:
					*r[i * 3 + l][h * 2 + j] = rand() % 10;
					if (*r[i * 3 + l][h * 2 + j] == 0)
						goto next5;
					if (*r[i * 3 + l][h * 2 + j] == **q[i * 3 + l])
						goto next5;
					for (k = 0; k < h * 2 + j; k++)
						if (*r[i * 3 + l][h * 2 + j] == *r[i * 3 + l][k])
							goto next5;
					for (k = 0; k < 2; k++)
					{
						if (*r[i * 3 + l][h * 2 + j] == *m[i][l * 2 + k])
							goto next5;
						if (*r[i * 3 + l][h * 2 + j] == *n[l][i * 2 + k])
							goto next5;
					}
					for (k = 0; k <= l * 3 + j * 2; k++)
						for (t = 0; t < k; t++)
							if (a[i * 3 + h * 2][k] == a[i * 3 + h * 2][t])
								goto next6;
				}
		}
	}
step5:
	for (i = 0; i < 3; i++)
	{
		if (check(a, i) == false)
		{
			for (j = 0; j < 9; j++)
			{
				rank(aa[j][i * 3], aa[j][i * 3 + 2]);
				if (check(a, i) == false)
					rank(aa[j][i * 3], aa[j][i * 3 + 2]);
				else if (check(a, i) == true)
					break;
			}
		}
		if (check(a, i) == false)
		{
			for (j = 0; j < 9; j++)
			{
				for (k = 8; k > j; k--)
				{
					rank(aa[j][i * 3], aa[j][i * 3 + 2]);
					rank(aa[k][i * 3], aa[k][i * 3 + 2]);
					if (check(a, i) == false)
					{
						rank(aa[j][i * 3], aa[j][i * 3 + 2]);
						rank(aa[k][i * 3], aa[k][i * 3 + 2]);
					}
					else if (check(a, i) == true)
						break;
				}
				if (check(a, i) == true)
					break;
			}
		}
		if (check(a, i) == false)
		{
			for (l = 0; l < 9; l++)
			{
				for (j = l + 1; j < 9; j++)
				{
					for (k = 8; k > j; k--)
					{
						rank(aa[l][i * 3], aa[l][i * 3 + 2]);
						rank(aa[j][i * 3], aa[j][i * 3 + 2]);
						rank(aa[k][i * 3], aa[k][i * 3 + 2]);
						if (check(a, i) == false)
						{
							rank(aa[l][i * 3], aa[l][i * 3 + 2]);
							rank(aa[j][i * 3], aa[j][i * 3 + 2]);
							rank(aa[k][i * 3], aa[k][i * 3 + 2]);
						}
						else if (check(a, i) == true)
							break;
					}
					if (check(a, i) == true)
						break;
				}
				if (check(a, i) == true)
					break;
			}
		}
		if (check(a, i) == false)
		{
			for (l = 0; l < 6; l++)
			{
				for (h = l + 1; h < 7; h++)
				{
					for (j = h + 1; j < 8; j++)
					{
						for (k = 8; k > j; k--)
						{
							rank(aa[l][i * 3], aa[l][i * 3 + 2]);
							rank(aa[h][i * 3], aa[h][i * 3 + 2]);
							rank(aa[j][i * 3], aa[j][i * 3 + 2]);
							rank(aa[k][i * 3], aa[k][i * 3 + 2]);
							if (check(a, i) == false)
							{
								rank(aa[l][i * 3], aa[l][i * 3 + 2]);
								rank(aa[h][i * 3], aa[h][i * 3 + 2]);
								rank(aa[j][i * 3], aa[j][i * 3 + 2]);
								rank(aa[k][i * 3], aa[k][i * 3 + 2]);
							}
							else if (check(a, i) == true)
								break;
						}
						if (check(a, i) == true)
							break;
					}
					if (check(a, i) == true)
						break;
				}
				if (check(a, i) == true)
					break;
			}
		}
		if (check(a, i) == false)
			goto step4;
	}

	printf("+---------------+---------------+---------------+\n");
	printf("|               |               |               |\n");
	for (i = 0; i < 9; i++)
	{
		printf("|   ");
		for (j = 0; j < 9; j++)
		{
			printf("%-4d", a[i][j]);
			if ((j + 1) % 3 == 0)
				printf("|   ");
		}
		printf("\n");
		printf("|               |               |               |\n");
		if ((i + 1) % 3 == 0)
		{
			printf("+---------------+---------------+---------------+\n");
			if (i < 8)
				printf("|               |               |               |\n");
		}
	}

	system("pause");
}

void rank(int *x, int *y)
{
	int t;
	t = *x;
	*x = *y;
	*y = t;
}

bool check(int a[9][9], int n)
{
	int i, j;
	for (i = 0; i < 9; i++)
		for (j = 0; j < i; j++)
			if (a[i][n * 3] == a[j][n * 3])
				return false;
	return true;
}

你可能感兴趣的:(C,小项目,指针)