#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. python pip install

    wget --no-check-certificate https://github.com/pypa/pip/archive/1.5.5.tar.gz https://github.com/pypa ...

  2. (二)Kafka动态增加Topic的副本(Replication)

    (二)Kafka动态增加Topic的副本(Replication) 1. 查看topic的原来的副本分布 [hadoop@sdf-nimbus-perf ~]$ le-kafka-topics.sh ...

  3. iOS - Mac OS X 常用快捷键

    Mac OS X 常用快捷键 1)快捷键图标: Escape 为 esc 键 无 Eject 键时用电源键代替 Space Bar 为空格键 2)基本快捷键: command + x 剪切文字 com ...

  4. idea 工程添加svn关联

    1.想启用idea的SVN插件还需要在idea配置一下(Ctrl + Alt + S),如下图所示: 2.接下来启用idea的版本控制插件(这里当然是启用Subversion了),打开“VCS”菜单项 ...

  5. Android之打log

    Android之打log 1.在代码中加上自己的log 2,模块编译mm -B或者./mk mm/mmm packages/apps/Contacts/ 3编译成功后install或者push生成的a ...

  6. 在与SQL Server建立连接时出现于网络相关的或特定于实例的错误

    客户遇到一个问题,用“服务器名\实例名”远程连接另外一台命名实例的时候连接失败,报“在与SQL Server建立连接时出现于网络相关的或特定于实例的错误,未找到或无法访问服务器.请验证实例名称是否正确 ...

  7. k.NIO方式SSL通道流程

    在看完NIO和SSLEngine集成的例子后,我们了解到并没有提供一个SSLServerSocketChannel,在SelectionKey事件发生后,通过SSLEngine的wrap和unwrap ...

  8. rebuild new environment for DW step

    Steps to rebuild PPE environment: (CTS) 1, Disable both CTS Daily Job (Daily) and CTS Daily Job (Sta ...

  9. Python爬虫:一些常用的爬虫技巧总结

    爬虫在开发过程中也有很多复用的过程,这里总结一下,以后也能省些事情. 1.基本抓取网页 get方法 import urllib2 url = "http://www.baidu.com&qu ...

  10. 004_kafka_安装运行

    1.下载和安装 目前kafka的稳定版本为0.10.0.0 下载地址:http://kafka.apache.org/downloads.html 下载后解压缩安装包到系统即可完成安装 > ta ...