C语言解决八皇后问题
#include <stdio.h>
#include <stdlib.h> /* this code is used to cope with the problem of the eight queens.
* array borad[9][9] is a virtual borad.
* line 0 and volumn 0 is ignored.
* at first we find a place to set a queen on it, then mark this queen's
* domain. the way of marking domain is add 1 on the board[line][volumn],
* so the number of board[line][volumn] means this place can attacked by
* how many queens. after we mark this queen's domain, we move on to next line.
* if there is no next line, we got a solution */
/* 这些代码主要用来解决八皇后问题
* 数组borad[9][9]是一个虚拟的棋盘。
* 第0行跟第0列被忽略
* 在最开始时,我们在某一行上找到一个位置,放上皇后后,我们把这个皇后能
* 攻击的范围都标志起来。我们标志的方法是在board[行][列]上加1,
* 所以board[行][列]上的值意味着这个位置能被多少个皇后攻击
* 当我们标志好这个皇后的范围后,我们就跳到下一行去。
* 如果已经没有下一行了,那么我们就找到一个放置方法了 */ /* if a place on board have been marked by NONE
* it means no queen can attack this place, it is a potential place to set a queen */
/* 如果棋盘上一个位置被设置为NONE,意味着没有皇后能攻击这个位置,这个位置可以被设置为皇后 */
#define NONE (0)
#define QUEEN (-1) #define OCCUPY (1)
#define DISOCCUPY (0) void Compute(int line) ;
void PrintBorad(void) ;
void operate_borad(int line, int volumn, int function) ; int main(void)
{
Compute() ;
return ;
} /* ignored the first line and the first volumn */
int borad[][] ; /* this is a recursive function.
* it will find a queen on this line,
* then fine next one of next line by call itself */
/* 这是一个递归函数。在某一行上找到皇后的位置,然后调用自己找下一行的皇后 */
void Compute(int line)
{
int i ;
static int num_of_solution = ; for(i = ; i <= ; i++)
if(borad[line][i] == NONE)
{
operate_borad(line, i, OCCUPY) ;
if(line == ) // find a solution
{
printf("%d\n", ++num_of_solution) ;
PrintBorad() ;
}
else Compute(line +) ; /* contine to next line */ /* 查找一行的皇后 */
operate_borad(line, i, DISOCCUPY) ;
}
} /* function:
* if function is OCCUPY, then set a flag of queen on borad[line][volumn]
* and set a flag on the domain of this queen
* if function is DISOCCUPY, then clear flag of queen on borad[line][volumn]
* and clear the flag on the domain of this queen */
/* 功能:
* 如果function变量是OCCUPY,那么在board[line][volumn]上放上皇后,
* 并设置这个皇后的攻击范围
* 如果function变量是DISOCCUPY,那么把皇后从board[line][volumn]上清除
* 并清除这个皇后在其攻击范围上所设置的标志 */
void operate_borad(int line, int volumn, int function)
{
int i, j, *temp, nl, nv ; borad[line][volumn] = (function == OCCUPY ? QUEEN : NONE) ; /* i and j are used to decide the direction*/
for(i = -; i <= ; i++)
for(j = -; j <= ; j++)
if(i == && j == ) continue ;
else
for(nl = line +i, nv = volumn +j ;
nl >= && nl <= && nv >= && nv <= ;
nl += i, nv += j)
{
temp = &borad[nl][nv] ;
/* add one means this place can be attack by another queen */
function == OCCUPY ? (*temp)++ : (*temp)-- ;
}
}
/* function: print the board on the screen */
/* 打印棋盘 */
void PrintBorad(void)
{
int x, y, chess ; for(x = ; x <= ; x++)
{
for(y = ; y <= ; y++)
{
chess = borad[x][y] ;
if(chess != QUEEN)
putchar('*') ;
else
putchar('Q') ;
}
putchar('\n') ;
}
}
C语言解决八皇后问题的更多相关文章
- 回溯算法-C#语言解决八皇后问题的写法与优化
结合问题说方案,首先先说问题: 八皇后问题:在8X8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行.同一列或同一斜线上,问有多少种摆法. 嗯,这个问题已经被使用各种语言解 ...
- 使用穷举法结合numpy解决八皇后问题
一般说到八皇后问题,最先想到的就是回溯思想,而回溯思想往往是需要递归来实现的. 计算机很善长做重复的事情,所以递归正和它的胃口,而我们人脑更喜观平铺直叙的思维方式.当 我们看到递归时,总想把递归平铺展 ...
- Python解决八皇后问题
最近看Python看得都不用tab键了,哈哈.今天看了一个经典问题--八皇后问题,说实话,以前学C.C++的时候有这个问题,但是当时不爱学,没搞会,后来算法课上又碰到,只是学会了思想,应该是学回溯法的 ...
- Python解决八皇后问题的代码【解读】
八皇后问题 来自于西方象棋(现在叫 国际象棋,英文chess),详情可见百度百科. 在西方象棋中,有一种叫做皇后的棋子,在棋盘上,如果双方的皇后在同一行.同一列或同一斜线上,就会互相攻击. 八皇后问题 ...
- 使用java语言实现八皇后问题
八皇后问题,在一个8X8的棋盘中,放置八个棋子,每个棋子的上下左右,左上左下,右上右下方向上不得有其他棋子.正确答案为92中,接下来用java语言实现. 解: package eightQuen; / ...
- Python 解决八皇后问题
问题介绍 八皇后问题是一个以国际象棋为背景的问题:如何能够在 \(8\times8\) 的国际象棋棋盘上放置八个皇后,使得任何一个皇后都无法直接吃掉其他的皇后?为了达到此目的,任两个皇后都不能处于同一 ...
- C语言:试探算法解决“八皇后”问题
#include <stdio.h> #define N 4 int solution[N], j, k, count, sols; int place(int row, int col) ...
- 八行代码解决八皇后问题(c++)
说的有点夸装,实际上并不只是巴航代码,加上前面的变量声明之类的一共有40多行的样子吧,好像是在知乎上看到的,现在有时间再把它写下来: 其中用到了一些c++11特性,例如lambda 以及给予范围的 f ...
- dfs 解决八皇后问题 以及其他图搜索问题
33. N皇后问题 中文 English n皇后问题是将n个皇后放置在n*n的棋盘上,皇后彼此之间不能相互攻击(任意两个皇后不能位于同一行,同一列,同一斜线). 给定一个整数n,返回所有不同的n皇后问 ...
随机推荐
- String和string的区别
(1)从位置讲 1.string是c#中的的 2.String是 .Net Framework的一个函数名(类),基于using.System的引用 (2)从性质讲 1.string是关键字,Stri ...
- Ubuntu 16.04 + Caffe
主要参考: https://github.com/BVLC/caffe/wiki/Ubuntu-16.04-or-15.10-Installation-Guide http://caffe.berke ...
- linux 知识汇总
1 ) linux下文件系统类型的学习 2 )深入理解linux i节点(inode) 3 )RAID系列
- supervisor安装和配置
直接命令 easy_install supervisor 如果报错先安装 yum install python-setuptools,再上面一条命令: 安装成功后显示finished,我们再次进行py ...
- 用友华表Cell一些用法小结(cs.net版本)
//从Color类型得到RGB类型,也可以用ColorTranslator.ToOle()方法 public int GetRGBFromColor(Color color) { byte r = c ...
- (五)AOS编程
一.LOG AOS_LOG(index) //断言,会打印出断言传进来的值 AOS_ASSERT(0); //只会打印断言位置 return AOS_FAIL; //返回错误,函数 ...
- java.lang.ClassNotFoundException和java.lang.NoClassDefFoundError的区别
java里生成对象有如下两种方式: 1: Object obj = new ClassName(); 直接new一个对象 2: Class clazz = Class.forName(ClassNam ...
- zoj 2833 friendship
zoj 2833这次真的很顺利了..居然是因为数组的大小没有符合要求,瞎折腾了很久..没有注意到要求范围,真是该死! 想法很简单,就是定义一个父结点数组,下标 i 表示这个元素,初始化为 -1表示 这 ...
- [转]jQuery.validate插件在失去焦点时执行验证代码
转:http://my.oschina.net/enyo/blog/311566 关于 jquery.validate.js 表单验证插件如何在失去焦点时做验证.看手册后发现默认是在表单提交时执行验证 ...
- 51nod 1264 线段相交(几何)
题目链接:51nod 1264 线段相交 如果两条线段相交,则需满足一条线段的一个端点在另一条线段上,或者 两条线段都分别跨越另一条线段延伸的直线上.(如果点p1位于直线p3p4的一边,而点p2位于该 ...