回溯法------ 解数独游戏(2)

void Print()

{

for (int i=0; i<81; i++)

{

for (int j=0; j<3; j++)

{

cout << StateSort[i][j] << ” “;

}

cout << endl;

}

}

///——————————

void InitState()

{

int i, j;

for (i=0; i<9; i++) //初始化State

{

for (j=0; j<9;j++)

{

State[i][j] = 0;

}

}

for (i=0; i<81; i++) // 初始化StateSort

{

StateSort[i][0] = i / 9 + 1;

StateSort[i][1] = i % 9 + 1;

StateSort[i][2] = 0;

}

// Print();

}

//——————————-

void Load()

{

freopen(“input.txt”,”r”,stdin); //输入从input.txt

int x, y, value;

while (scanf (“%d %d %d”, &x, &y, &value) != EOF)

{

State[x-1][y-1] = value;

}

}

//———————————

void Count()// 算出每一个空元素的所在行和列约束的个数

{

int k;

for (int i=0; i<9; i++)

{

for (int j=0; j<9; j++)

{

if (State[i][j] == 0)

{

int nCount = 0;

for (k=0; k<9; k++)

{

if ( 0 != State[i][k])

{

nCount++;

}

}

for (k=0; k<9; k++)

{

if (0 != State[k][j])

{

nCount++;

}

}

StateSort[i*9+j][2] = nCount;

}

}

}

}

//———————————————

void Sort()

{

int pivotkey[3] ;

for (int i=1; i<=81;i++)

{

if (StateSort[i][2] > StateSort[i-1][2])

{

pivotkey[2] = StateSort[i][2];

pivotkey[1] = StateSort[i][1];

pivotkey[0] = StateSort[i][0];

for (int j=i-1; (pivotkey[2]>StateSort[j][2]) && (j>=0); –j)

{

StateSort[j+1][2] = StateSort[j][2];

StateSort[j+1][1] = StateSort[j][1];

StateSort[j+1][0] = StateSort[j][0];

}

StateSort[j+1][2] = pivotkey[2];

StateSort[j+1][1] = pivotkey[1];

StateSort[j+1][0] = pivotkey[0];

}

}

//Print();

}

//———————————

//检查每一个小区内只能出现一次

bool ChechZone(int x, int y, int i)

{

int xZone = 3 * (x / 3); //找到每个小区的位置

int yZone = 3 * (y / 3);

int j = 0;

int k = 0;

bool flag = true;

for (j=xZone; j

{

for (k=yZone; k

{

if ((x != j || y != k) && State[j][k] == i)

{

flag = false;

goto A1;

}

}

}

A1:

return flag;

}

//——————————–

//检查是否符合条件

bool ChechAssign(int x, int y, int i)

{

bool flag = true;

for (int k=0; k<9; k++)

{

if (k != y && i == State[x][k] )

{

flag = false;

}

if (k != x && i == State[k][y] )

{

flag = false;

}

if (!ChechZone(x, y, i))

{

flag = false;

}

}

return flag;

}

//———————————–

int DCount ()

{

int g_Count = 0;

for (int i=0; i<81; i++)

{

if (0 != StateSort[i][2])

{

g_Count++;

}

}

return g_Count;

}

///——————————-

int Search (int depth)

{

if (depth >= DCount())

{

return 1;

}

int x, y;

x = StateSort[depth][0] – 1;

y = StateSort[depth][1] – 1;

//检查x y有没有被 赋值

if (0 != State[x][y])

{

return Search (depth + 1);

}

else //尝试赋值

{

for (int i=1; i<=9; i++)

{

State[x][y] = i;

//检查是否违反约束条件

if (ChechAssign(x, y, i))

{

if (Search(depth + 1))

{

return 1;

}

}

State[x][y] = 0;

}

return 0;

}

}

///—————————————

void OutputState()

{

for (int i=0; i<9; i++)

{

for (int j=0; j<9; j++)

{

cout << State[i][j] << ” “;

if (0 == (j+1) % 3)

{

cout << ” “;

}

}

if (0 == (i + 1) % 3)

{

cout << endl;

}

cout << endl;

}

cout << endl;

}

//————————————-

int main ()

{

cout << “=======初始化========”<< endl << endl;

InitState();

//输入函数

Load();

OutputState();

//计算每个元素所在行列已有的数字个数 非空元素为0

Count();

//Print();

//cout <<“+++++++++”<

//排序

Sort();

//Print();

cout << “The Depth : ” << DCount() << endl <

cout << “=======结果为=======”<< endl << endl;

//搜索

if (Search(0))

{

//输出

OutputState();

}

else

{

//输出提示信息

cout << “搜索失败!!!”<< endl;

}

cout << “====================”<< endl << endl;

cout << DCount();

return 0;

}

// ————————————————————————-

// $Log: $

    原文作者:回溯法
    原文地址: https://blog.csdn.net/yingchunhua365/article/details/16959645
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞