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皇后问 ...
随机推荐
- (转载)U-boot启动完全分析
1.1 U-Boot工作过程 U-Boot启动内核的过程可以分为两个阶段,两个阶段的功能如下: (1)第一阶段的功能 Ø 硬件设备初始化 Ø 加载U-Boot第二阶段代码到RAM空间 Ø 设置好栈 Ø ...
- HTTP中的摘要认证机制
引子: 指定和服务器端交互的HTTP方法,URL地址,即其他请求信息: Method:表示http请求方法,一般使用"GET","POST". url:表示请求 ...
- 关于 RxJava 技术介绍
Awesome-RxJava RxJava resources Blog 给 Android 开发者的 RxJava 详解 -强烈推荐 扔物线的文章 讲解非常详细 NotRxJava懒人专用指南 -这 ...
- Spring+Mybatis 手动控制事务
public boolean testDelete(String jobCode) throws Exception { boolean flag = false; //1.获取事务控制管理器 Dat ...
- C++ Primer 第九章 顺序容器
由于书籍上写的已经很经典了,故大部分用图片的形式来阐述概念,代码纯手打进行验证. 1.顺序容器类型:vector.deque.list.forword_list.array.string. 2.顺序容 ...
- Xcode集成开发环境的安装
Xcode是苹果官方提供的iOS开发环境,安装方式如下: 安装过程需要登录AppleID账号,如果你没有苹果账号,可以免费注册一个.根据提示就能完成Xcode安装.
- UNIX-LINUX编程实践教程->第八章->实例代码注解->写一个简单的shell
一 分析 要实现一个shell,需包含3个步骤 1)读入指令 2)指令解析 3)执行指令 1 从键盘读入指令 从键盘读入指令的几个要点: 1)调用getc函数等待并获取用户键盘输入. 2)每一行命令的 ...
- csu 1809 Parenthesis
题目见此 分析,把'('当成1, ')'当成-1, 计算前缀和sum. 记交换括号左边的序号为u, 右边为v,讨论左右括号: 1.s[u] == '(' && s[v] == ')' ...
- 用纯Css作三角形
<style> //向上三角形 .triangle_up{ width:0; height:0; border-left:30px solid transparent; border-ri ...
- gz
不准备的话,是真的会滚粗的. leetcode 还是重新做起来叭. 那么就开始咯 8.22 leetcode 144 Binary Tree Preorder Traversal 二叉树的前序遍历 ...