|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
感觉照着视屏跟着打了,调试的时候根本就没有进入程序,萌新不知道咋回事,求大佬帮忙;
#include<stdio.h>
int count = 0;
void eight(int row, int line, int(*chess)[8]);
int ok(int row, int line, int(*chess)[8]);
int main()
{
int chess[8][8] = { 0 }, i, j;
eight(0, 8, chess);
printf("总共有%d解决方法\n", count);
return 0;
}
int ok(int row, int line, int(*chess)[8])
{
int i, j, flag1 = 0, flag2 = 0, flag3 = 0, flag4 = 0, flag5 = 0;
for (i = 0; i < 8; i++)//判断列方向
{
if (*(*(chess + i) + line) != 0)
{
flag1 = 1;
break;
}
}
for (i = row, j = line; i >= 0 && j >= 0; i--, j--)
{
if (*(*(chess + i) + j) != 0)
{
flag2 = 1;
break;
}
}
for (i = row, j = line; i<8 && j <8; i++, j++)
{
if (*(*(chess + i) + j) != 0)
{
flag3 = 1;
break;
}
}
for (i = row, j = line; i >= 0 && j <8; i--, j++)
{
if (*(*(chess + i) + j) != 0)
{
flag4 = 1;
break;
}
}
for (i = row, j = line; i >= 0 && j <8; i--, j++)
{
if (*(*(chess + i) + j) != 0)
{
flag4 = 1;
break;
}
}
for (i = row, j = line; i <8 && j >= 0; i++, j--)
{
if (*(*(chess + i) + j) != 0)
{
flag5 = 1;
break;
}
}
if (flag1 || flag2 || flag3 || flag4 || flag5)
{
return 0;
}
else
{
return 1;
}
}
void eight(int row, int line, int(*chess)[8])//row表示起始行,line表示列数;(*chess)[8]表示指向棋盘每一行指针
{
int chess2[8][8], i, j;
for (i = 0; i < 8; i++)
for (j = 0; j < 8; j++)
{
chess2[i][j] = chess[i][j];
}
if (8 == row)
{
printf("第 %d 种", count + 1);
for (i = 0; i < 8; i++)
{
for (j = 0; j < 8; j++)
{
printf("%d ", *(*(chess2 + i) + j));
}
printf("\n");
}
printf("\n");
count++;
}
else
{
for (j = 0; j < line; j++)
{
if (ok(row, line, chess))//判断是否危险
{
for (i = 0; i < 8; i++)
{
*(*(chess2 + row) + i) = 0;
}
*(*(chess2 + row) + j) = 1;
eight(row + 1, line, chess2);
}
}
}
#include <stdio.h>
int count = 0;
int notDanger(int row, int j, int (*chess)[8])
{
int i, k, flag1 = 0, flag2 = 0, flag3 = 0, flag4 = 0, flag5 = 0;
// check column
for(i = 0; i < 8; i++)
{
if( *(*(chess+i)+j) != 0)
{
flag1 = 1;
break;
}
}
// check diagonal left-up
for(i = row, k = j; i >= 0 && k >= 0; i--, k--)
{
if( *(*(chess+i)+k) != 0 )
{
flag2 = 1;
break;
}
}
// check diagonal right-down
for(i = row, k = j; i < 8 && k < 8; i++, k++)
{
if( *(*(chess+i)+k) != 0 )
{
flag3 = 1;
break;
}
}
// check diagonal right-up
for(i = row, k = j; i >= 0 && k < 8; i--, k++)
{
if( *(*(chess+i)+k) != 0 )
{
flag4 = 1;
break;
}
}
// check diagonal left-down
for(i = row, k = j; i < 8 && k >= 0; i++, k--)
{
if( *(*(chess+i)+k) != 0 )
{
flag5 = 1;
break;
}
}
if( flag1 || flag2 || flag3 || flag4 || flag5 )
{
return 0;
}
else
{
return 1;
}
}
// n is col
// row pointer
void EightQueen(int row, int n, int (*chess)[8])
{
int chess2[8][8], i, j; // temp chessboard
for(i = 0; i < 8; i++)
{
for(j = 0; j < 8; j++)
{
chess2[i][j] = chess[i][j];
}
}
// Core recursion
if(row == 8)
{
printf("%d-th solution\n", count+1);
for(i = 0; i < 8; i++)
{
for(j = 0; j < 8; j++)
{
printf("%d ", *(*(chess2+i)+j));
}
printf("\n");
}
printf("\n");
count++;
}
else
{
// Is this a valid position?
// If yes, continue...
for(j = 0; j < n; j++)
{
if( notDanger(row, j, chess) ) // valid position?
{
for(i = 0; i < 8; i++)
{
*(*(chess2+row)+i) = 0;
}
*(*(chess2+row)+j) = 1;
EightQueen(row+1, n, chess2);
}
}
}
}
int main(void)
{
int chess[8][8], i, j;
for(i = 0; i < 8; i++)
{
for(j = 0; j < 8; j++)
{
chess[i][j] = 0;
}
}
EightQueen(0, 8, chess);
printf("There are %d number of solutions in total\n\n", count);
return 0;
}
|
|