回溯法------ 解数独游戏(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: $

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值