递归经典例题--N皇后问题--C语言

问题描述:

        在n*n的放个棋盘上放置n个皇后棋子,要求每个皇后要不同行、不同列、不同对角线。

求解:

        根据他的要求,首先是不同行,那么就是一行最多只能有一个皇后,这个条件可以作为递归的参数。

        其次,不同列好判断,遍历已经放置皇后的格子的y值与当前的y值不等即可。

        关键在于不同对角线,画个图好理解,假设我们要放置的点为(3,3)。

        以上为(3,3)部分对角线的元素,我们观察坐标可以发现这样一个关系。

        (3,3)所有对角线的元素(i,j),|3-i|总是等于|3-j|,即构成一个等腰直角三角成。

         如上,所有判断条件得到解决,现在设计递归。

        递归需要一个二维数组为棋盘,n为棋盘边长,i为当前递归到的行数。

        函数dis用于输出棋盘,place用于判断当前(i,j)可否放皇后。

完整代码:

#include<stdio.h>
#include<iostream>
using namespace std;
#define max 5
void dis(int chess[max][max],int n) {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++)
			cout << chess[i][j] << " ";
		cout << endl;
	}
	cout << endl;
}
bool place(int chess[max][max],int n, int x, int y) {
	if (x == 0)
		return true;
	else {
		for (int i = 0; i < x; i++) {
			for (int j = 0; j < n; j++) {
				if (chess[i][j] == 1) {
					if (j == y || abs(i - x) == abs(j - y))
						return false;
				}
			}
		}
	}
	return true;
}
void queen(int chess[max][max],int n,int i) {
	if (i == n)
		dis(chess, n);
	for (int j = 0; j < n; j++) {
		if (place(chess, n, i, j)) {
			chess[i][j] = 1;
			queen(chess, n, i + 1);
			chess[i][j] = 0;
		}
	}
}
int main() {
	int chess[max][max];
	memset(chess, 0, sizeof(chess));
	queen(chess, max, 0);
	return 0;

}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
n皇后问题是一个经典问题,目标是在一个n×n的棋盘上放置n个皇后,使得每个皇后都不能互相攻击,即任意两个皇后都不能处于同一行、同一列或同一斜线上。下面是两个C语言归实现n皇后问题的代码: 引用: ```c #include<stdio.h> #define MAX 10 #define n 4 //4个皇后 int a[MAX];//第i个皇后在a[i]上 int sum = 0; int detection(int x,int y) {//判断是否能放 for (int i = 1; i <= x; i++) { if (a[i] == y)return 0;//|有皇后 if (i + a[i] == x + y)return 0;//斜边右边有皇后 if (i - a[i] == x - y)return 0;//斜边左边有皇后 } return 1;//可以放皇后 } void queen(int row) {//第row个皇后在? if (row == n + 1) { sum++;//完成了一组解答 printf("解法%d:",sum); for (int k = 1; k < n; k++) { printf("%d", a[k]); } printf("\n"); return; } for (int i = 1; i <= n; i++) { if (detection(row, i)) { a[row] = i; queen(row + 1); a[row] = 0;//回溯 } } } int main() { queen(1); printf("有%d种解法\n", sum); return 0; } ``` 引用: ```c #include<iostream> using namespace std; #define max 5 void dis(int chess[max][max],int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cout << chess[i][j] << " "; cout << endl; } cout << endl; } bool place(int chess[max][max],int n, int x, int y) { if (x == 0) return true; else { for (int i = 0; i < x; i++) { for (int j = 0; j < n; j++) { if (chess[i][j] == 1) { if (j == y || abs(i - x) == abs(j - y)) return false; } } } } return true; } void queen(int chess[max][max],int n,int i) { if (i == n) dis(chess, n); for (int j = 0; j < n; j++) { if (place(chess, n, i, j)) { chess[i][j] = 1; queen(chess, n, i + 1); chess[i][j] = 0; } } } int main() { int chess[max][max]; memset(chess, 0, sizeof(chess)); queen(chess, max, 0); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值