关于八皇后问题
感觉照着视屏跟着打了,调试的时候根本就没有进入程序,萌新不知道咋回事,求大佬帮忙;#include<stdio.h>
int count = 0;
void eight(int row, int line, int(*chess));
int ok(int row, int line, int(*chess));
int main()
{
int chess = { 0 }, i, j;
eight(0, 8, chess);
printf("总共有%d解决方法\n", count);
return 0;
}
int ok(int row, int line, int(*chess))
{
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))//row表示起始行,line表示列数;(*chess)表示指向棋盘每一行指针
{
int chess2, i, j;
for (i = 0; i < 8; i++)
for (j = 0; j < 8; j++)
{
chess2 = chess;
}
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))
{
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))
{
int chess2, i, j;// temp chessboard
for(i = 0; i < 8; i++)
{
for(j = 0; j < 8; j++)
{
chess2 = chess;
}
}
// 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, i, j;
for(i = 0; i < 8; i++)
{
for(j = 0; j < 8; j++)
{
chess = 0;
}
}
EightQueen(0, 8, chess);
printf("There are %d number of solutions in total\n\n", count);
return 0;
} 本帖最后由 qwertyxxl 于 2018-8-20 23:19 编辑
好了 用的是vs2015 qwertyxxl 发表于 2018-8-20 23:17
用的是vs2015
我用 dev c++, dev 行,到哪都行
页:
[1]