求用C语言来解决八皇后问题,分递归法和非递归法两种,可以输出所有的情况

mwy 2011-11-28 03:10:41
求用C语言来解决八皇后问题,分别用递归法非递归法两种,最好有思路的注释。并且可以输出所有的情况!谢谢!
...全文
901 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
mosal 2011-12-05
  • 打赏
  • 举报
回复
总结帝 膜拜[Quote=引用 11 楼 qq120848369 的回复:]

C/C++ code
递归就是dfs,非递归就是栈模拟一下dfs。
[/Quote]
qq120848369 2011-12-05
  • 打赏
  • 举报
回复
递归就是dfs,非递归就是栈模拟一下dfs。
mwy 2011-12-05
  • 打赏
  • 举报
回复
2楼的,你那个非递归算法解决八皇后问题能不能修改一下,就是按一个回车输出一种结果,按一个回车输出一种结果,并且能输出种数。把函数的定义也单独的列出,不用define。输出的结果最好呈现正方形/
贪慕 2011-11-30
  • 打赏
  • 举报
回复
上面写的都很详细啊。
孑虫 2011-11-29
  • 打赏
  • 举报
回复
01.#include <stdio.h>
02.#include <stdlib.h>
03.
04.#define FALSE 0
05.#define TRUE 1
06.
07.int flag = 0; //标志:下次递归失败与否;
08.int board[ 8 ][ 8 ];
09.
10.void print_board()
11.{
12. int row;
13. int column;
14. static int n_solutions;
15.
16. n_solutions += 1;
17. printf( "Solution #%d:\n", n_solutions );
18.
19. for( row = 0; row < 8; row ++ )
20. {
21. for( column = 0; column < 8; column ++ )
22. {
23. if( board[ row ][ column ] )
24. printf( " Q");
25. else
26. printf( " +");
27. }
28. putchar( '\n');
29. }
30. putchar( '\n' );
31.}
32.
33.int conflicts( int row, int column )
34.{
35. int i;
36.
37. for( i = 0; i < 8; i++ )
38. {
39. if( row - i >= 0 && board[ row-i ][ column ] )
40. return TRUE;
41. if( column - i >= 0 && board [ row ][ column - i ])
42. return TRUE;
43. if( column + i < 8 && board [ row ][ column + i ] )
44. return TRUE;
45.
46. if( row - i >= 0 && column - i >= 0
47. && board[ row - i ][ column - i ] )
48. return TRUE;
49. if( row - i >= 0 && column + i < 8
50. && board[ row - i ][ column + i ] )
51. return TRUE;
52. }
53.
54. return FALSE;
55.}
56.
57.void place_queen( int row )
58.{
59. int column;
60.
61. for ( column = 0; column < 8; column++ )
62. {
63. if(!conflicts( row, column ) )
64. {
65. board[ row ][ column ] = TRUE;
66. if( row < 7 )
67. {
68. place_queen( row + 1 );
69. if( flag == 1)
70. {
71. board[ row ][ column ] = FALSE;
72. flag = 0;
73. continue;
74. }
75. }
76. else
77. {
78. print_board();
79. board[ row ][ column ] = FALSE;
80. }
81. }
82. }
83. flag = 1;
84.}
85.int main()
86.{
87. int i;
88. int j;
89. for( i = 0; i < 8; i++ )
90. for ( j = 0; j < 8; j++ )
91. board[ i ][ j ] = FALSE;
92. place_queen( 0 );
93. return 0;
94.}
孑虫 2011-11-29
  • 打赏
  • 举报
回复
楼上挺好的啊,我看了一会没看懂呢,继续看
JieTouLangRen 2011-11-29
  • 打赏
  • 举报
回复
算法分析 回溯法
lanqiucoco 2011-11-29
  • 打赏
  • 举报
回复
哇,严慧敏老师的数据结构(c语言版)貌似有
尘缘udbwcso 2011-11-29
  • 打赏
  • 举报
回复

//八皇后问题
#include <stdio.h>
#include <math.h>
int count = 0;
void empress(int *position)
{
int i, j, flag;
while(position[8] != 1)
{
++position[0];
for(i = 0; i < 8; ++i)
{
if(position[i] == 8)
{
position[i] = 0;
++position[i+1];
}
}
flag = 1;
//判断结果是否满足条件
for(i = 0; i < 8; ++i)
{
for(j = 0; j < 8; ++j)
{
if(i != j)
{
if(position[i] == position[j])
flag = 0;
else if(abs(position[i] - position[j]) == abs(i-j))
flag = 0;
}
}
}
if(flag == 1)
{
//输出#代表皇后
for(i = 0; i < 8; ++i)
{
for(j = 0; j < 8; ++j)
{
if(position[i] == j)
printf("# ");
else
printf("0 ");
}
printf("\n");
}
printf("\n");
}
count += flag;
}
}
int main()
{
int position[9] = {0};
empress(position);
printf("%d种解\n", count);
return 0;
}


一种比较笨的方法,可以输出所有情况
wang7535067 2011-11-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 nibiewuxuanze 的回复:]

再发一个递归写法的:
C/C++ code

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define swap(a, b) temp = a; a = b; b = temp;
#define pop(i) swap(pos[i], pos[top]); top--;
#define push(i) ……
[/Quote]
正解
建鼎呓语 2011-11-29
  • 打赏
  • 举报
回复
再发一个递归写法的:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define swap(a, b) temp = a; a = b; b = temp;
#define pop(i) swap(pos[i], pos[top]); top--;
#define push(i) top++; swap(pos[i], pos[top]);
int pos[8] = {7, 0, 1, 2, 3, 4, 5, 6};
int top = 7;
int temp;

void queen(int n)
{
int i;
char row[] = " ";

if(n == 1)
{
for(i = 0; i < 8; i++)
{
printf("%d", pos[i]);
}
printf("\n");
for(i = 0; i < 8; i++)
{
memset(row, ' ', 8);
row[pos[i]] = '*';
printf("%s\n", row);
}
printf("\n");
}
else
{
for(i = 0; i < n; i++)
{
pop(i);
queen(n - 1);
push(i);
}
}
}

int main()
{
queen(8);

return 0;
}

代码输出与楼上相同。
建鼎呓语 2011-11-29
  • 打赏
  • 举报
回复
先发一个很不优雅的代码,嘿嘿

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
#define swap(a, b) temp = a; a = b; b = temp;
#define pop(i) swap(pos[i], pos[top]); top--;
#define push(i) top++; swap(pos[i], pos[top]);
int pos[8] = {7, 0, 1, 2, 3, 4, 5, 6};
int top = 7;
int temp;

char row[] = " ";
int a, b, c, d, e, f, g, h, i;

for(a = 0; a < 8; a++)
{
pop(a);
for(b = 0; b < 7; b++)
{
pop(b);
for(c = 0; c < 6; c++)
{
pop(c);
for(d = 0; d < 5; d++)
{
pop(d);
for(e = 0; e < 4; e++)
{
pop(e);
for(f = 0; f < 3; f++)
{
pop(f);
for(g = 0; g < 2; g++)
{
pop(g);
for(h = 0; h < 1; h++)
{
for(i = 0; i < 8; i++)
{
printf("%d", pos[i]);
}
printf("\n");
for(i = 0; i < 8; i++)
{
memset(row, ' ', 8);
row[pos[i]] = '*';
printf("%s\n", row);
}
printf("\n");
}
push(g);
}
push(f);
}
push(e);
}
push(d);
}
push(c);
}
push(b);
}
push(a);
}

return 0;
}

最前面的几行输出:
01234567
*
*
*
*
*
*
*
*

10234567
*
*
*
*
*
*
*
*

12034567
*
*
*
*
*
*
*
*

21034567
*
*
*
*
*
*
*
*

02134567
*
*
*
*
*
*
*
*


1) 本套课程针对高校大学生系统学习C语言而录制,从0基础入门讲起,循序渐进,通俗易懂,同时适用于计算机系及计算机系的同学。通过学习可以帮助大家掌握C语言本质,轻松面对C语言全国二级考试,并达到能独立完成中型C项目、C游戏的水平;2) 多数高校都开设了C语言课程,网上C语言课程也很多,但普遍存在两个问题: (1) 授课方式单一,大多是照着代码念一遍, 对刚刚接触编程的同学来说,感觉晦涩难懂 (2) 授课内容过度注重语法,没有项目实战支撑,造成课程枯燥无趣。本课程针对上述问题进行了改进 (1) 授课方式采用语法讲解+内存布局示意图+项目的方式,让课程生动有趣好理解 (2) 系统全面的讲解了C语言的核心技术点,还配套录制了《全国计算机二级C语言真题精讲》,让学员在掌握C语言编程的同时,还能轻松面对二级考试;3) 课程详细内容: 常量-变量、分支语句、循环语句、操作符和表达式、函数(库函数、自定义函数、归调用 )、数组(一维数 组、二维数组、数组作为函数参数、指针数组)、指针(指针和指针类型、二级指针和多级指针、指针表达式解析、指针运算、数 组指针、函数指针、回调函数)、调试技巧、程序环境和预处理(翻译环境、运行环境、预定义符号、#define和#undef、宏和函 数、条件编译、文件包含)、 数据在内存中的存储、内存块分配、static、字符函数和字符串函数、自定义类型(结构体、枚举 、联合)、动态内存管理、文件操作(i/o常量、i/o函数、流)。

69,394

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧