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皇后问 ...
随机推荐
- jquery.validate使用 - 1
jquery.validate使用攻略 好几年不写JS了,资料整理起来比较慢,格式也有点乱 主要分几部分jquery.validate 基本用法jquery.validate API说明jquery. ...
- 关于Youtube URL的十个技巧
你一定很熟悉Youtube了,知道它是一个视频分享网站.是的,youtube目前十分流行,你也许会常常访问.这里有一些关于youtube url的技巧,了解了这些技巧,你就可以更好的利用youtube ...
- 后勤数据抽取流程图 Logistic Data Extraction
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- 没有技术说明文档的开源都是耍流氓:微软Roslyn编译即服务在CIIP中具体应用(上)
前段时间我发布了 github开源:企业级应用快速开发框架CIIP WEB+WIN+移动端,很多园友们都表示支持并与我探讨相关技术问题,上篇中我也承诺会写相关的技术文章,本篇就来介绍一下建模模块中使用 ...
- jquery总结05-常用事件05-触发事件
触发事件 .trigger('click')触发浏览器事件,简写等于.click(),还同时支持自定义事件,并且可以传递参数 $('#elem').on('Aaron', function(event ...
- HTTP协议的头信息详解
转载地址:http://blog.csdn.net/guoguo1980/article/details/2649658 HTTP(HyperTextTransferProtocol)是超文本传输协议 ...
- 【Todo】网络编程学习-面向工资编程
https://zhuanlan.zhihu.com/p/20204159 这个系列真的非常好,好好领会学习一下
- AVKit & MediaPlayer简写
@import AVKit; @import AVFoundation; NSString *address = [[NSBundle mainBundle]pathForResource:@&quo ...
- c++ 文件读写模板
#include <fstream> using namespace std; int main() { ifstream fin("in.txt"); ofstrea ...
- Linux vi/vim
所有的 Unix Like 系统都会内建 vi 文书编辑器,其他的文书编辑器则不一定会存在. 但是目前我们使用比较多的是 vim 编辑器. vim 具有程序编辑的能力,可以主动的以字体颜色辨别语法的正 ...