#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语言解决八皇后问题的更多相关文章

  1. 回溯算法-C#语言解决八皇后问题的写法与优化

    结合问题说方案,首先先说问题: 八皇后问题:在8X8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行.同一列或同一斜线上,问有多少种摆法. 嗯,这个问题已经被使用各种语言解 ...

  2. 使用穷举法结合numpy解决八皇后问题

    一般说到八皇后问题,最先想到的就是回溯思想,而回溯思想往往是需要递归来实现的. 计算机很善长做重复的事情,所以递归正和它的胃口,而我们人脑更喜观平铺直叙的思维方式.当 我们看到递归时,总想把递归平铺展 ...

  3. Python解决八皇后问题

    最近看Python看得都不用tab键了,哈哈.今天看了一个经典问题--八皇后问题,说实话,以前学C.C++的时候有这个问题,但是当时不爱学,没搞会,后来算法课上又碰到,只是学会了思想,应该是学回溯法的 ...

  4. Python解决八皇后问题的代码【解读】

    八皇后问题 来自于西方象棋(现在叫 国际象棋,英文chess),详情可见百度百科. 在西方象棋中,有一种叫做皇后的棋子,在棋盘上,如果双方的皇后在同一行.同一列或同一斜线上,就会互相攻击. 八皇后问题 ...

  5. 使用java语言实现八皇后问题

    八皇后问题,在一个8X8的棋盘中,放置八个棋子,每个棋子的上下左右,左上左下,右上右下方向上不得有其他棋子.正确答案为92中,接下来用java语言实现. 解: package eightQuen; / ...

  6. Python 解决八皇后问题

    问题介绍 八皇后问题是一个以国际象棋为背景的问题:如何能够在 \(8\times8\) 的国际象棋棋盘上放置八个皇后,使得任何一个皇后都无法直接吃掉其他的皇后?为了达到此目的,任两个皇后都不能处于同一 ...

  7. C语言:试探算法解决“八皇后”问题

    #include <stdio.h> #define N 4 int solution[N], j, k, count, sols; int place(int row, int col) ...

  8. 八行代码解决八皇后问题(c++)

    说的有点夸装,实际上并不只是巴航代码,加上前面的变量声明之类的一共有40多行的样子吧,好像是在知乎上看到的,现在有时间再把它写下来: 其中用到了一些c++11特性,例如lambda 以及给予范围的 f ...

  9. dfs 解决八皇后问题 以及其他图搜索问题

    33. N皇后问题 中文 English n皇后问题是将n个皇后放置在n*n的棋盘上,皇后彼此之间不能相互攻击(任意两个皇后不能位于同一行,同一列,同一斜线). 给定一个整数n,返回所有不同的n皇后问 ...

随机推荐

  1. react native 学习资料汇总

    http://www.ejiakt.com/album/show/252 http://www.cocoachina.com/ios/20150408/11513.html http://www.os ...

  2. java 代理模式一: 静态代理

    代理模式: 代理模式的作用:为其他对象提供一种代理以控制对 特定对象  的访问. 某种情况下,一个客户不想或者直接引用另一个对象,而代理对象可以在客户端和目标对象之间起到中介的作用:通过代理对象引用. ...

  3. GitHub下载的 Git Shell中出现的字符支持警告,将字体修改为新宋体即可

    ——解决办法: 将命令行字体修改为新宋体就可以了. 文章引用:http://www.cnblogs.com/veryinf/archive/2012/10/26/2740899.html

  4. centos7 禁止防火墙

    #CentOS .0默认使用的是firewall作为防火墙,这里改为iptables防火墙. #firewall: systemctl start firewalld.service#启动firewa ...

  5. 判断json数据是否为空

    json数据是没有length这个属性的 ,所以不能直接用.length()方法 我们可以先遍历,然后根据遍历次数求长度 1.在IE上这样遍历json:(js代码) var jsonLength = ...

  6. linux如何修改文件或目录的权限(chmod)

    chmod命令是linux上用于改变权限的命令,-R 是递归遍历子目录,因为你要操作的文件使用的*通配符.777,第一个7代表文件所属者的权限,第二个7代表文件所属者所在组的权限,第三个7代表其它用户 ...

  7. 002_kafka_相关术语详细解析

    参考: http://www.cnblogs.com/likehua/p/3999538.html http://kafka.apache.org/documentation.html#getting ...

  8. 用python的BeautifulSoup分析html 【转】

    原地址:http://www.cnblogs.com/twinsclover/archive/2012/04/26/2471704.html 序言 之前用python爬取网页的时候,一直用的是rege ...

  9. 已知GBK的某段码表,码表对应的字符

    for i in range(0xA1A2,0xA1A5):                                                                       ...

  10. systemctl命令

    systemctl命令是系统服务管理器指令,它实际上将 service 和 chkconfig 这两个命令组合到一起. 任务 旧指令 新指令 使某服务自动启动 chkconfig --level 3 ...