其他分享
首页 > 其他分享> > n皇后递归,帮忙看一下哪里错了,谢谢

n皇后递归,帮忙看一下哪里错了,谢谢

作者:互联网

// n皇后.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <ctime>
using namespace std;
const int MAXN = 1001;
int ChessBoard[MAXN][MAXN];
bool Zdiagonal[MAXN];
bool Xdiagonal[MAXN];
bool Row[MAXN];
int Line[MAXN][MAXN];
int cnt = 0;
void dfs(int FirstQ, int EndQ)
{
	if (FirstQ > EndQ)
	{
		cnt++;
		for (int i = 1; i <= EndQ; i++)
		{
			for (int j = 1; j <= EndQ; j++)
			{
				if (ChessBoard[i][j] != 0)
				{
					Line[cnt][i] = j;
				}
			}
		}
	}
	else
	{
		for (int i = 1; i <= EndQ; i++)
		{
			if (Row[i] == 0 && Zdiagonal[FirstQ - i + EndQ] == 0 && Xdiagonal[FirstQ + i] == 0)
			{
				ChessBoard[i][FirstQ] = 1;
				Row[i] = true;
				Zdiagonal[FirstQ - i + EndQ] = true;
				Xdiagonal[FirstQ + i] = true;
				dfs(FirstQ + 1, EndQ);
				ChessBoard[i][FirstQ] = 0;
				Row[i] = false;
				Zdiagonal[FirstQ - i + EndQ] = false;
				Xdiagonal[FirstQ + i] = false;
			}
		}
	}
}
int main()
{
	int QueenNumber;
	cin >> QueenNumber;
	dfs(1, QueenNumber);
	if (cnt == 0)
	{
		cout << "no solute!" << endl;
		return 0;
	}
	for (int i = 1; i <= cnt; i++)
	{
		for (int j = 1; j <= QueenNumber; j++)
		{
			cout << Line[i][j] << " ";
		}
		cout << endl;
	}
	cout << cnt << endl;
	return 0;
}




//学校写法




//#include <iostream>
//#include <cstring>
//#include <algorithm>
//#include <cstdio>
//#include <cmath>
//using namespace std;
//const int mac = 100;
//int Chess[mac][mac];
//int Line[500][mac];
//bool row[110], lft[20 * 10], rht[20 * 10];
//int cnt = 0;
//void dfs(int k, int n)
//{
//    if (k > n)
//    {
//        cnt++;
//        for (int i = 1; i <= n; i++)
//        {
//            for (int j = 1; j <= n; j++)
//            {
//                if (Chess[i][j] != 0)
//                {
//                    Line[cnt][i] = j;
//                }
//            }
//        }
//    }
//    else
//    {
//        for (int i = 1; i <= n; i++)
//        {
//            if (row[i] == 0 && lft[k - i + n] == 0 && rht[k + i] == 0)
//            {
//                Chess[i][k] = 1;
//                row[i] = lft[k - i + n] = rht[k + i] = true;
//                dfs(k + 1, n);
//                Chess[i][k] = 0;
//                row[i] = lft[k - i + n] = rht[k + i] = false;
//            }
//        }
//    }
//
//}
//int main()
//{
//    int n;
//    cin >> n;
//    dfs(1, n);
//    if (cnt == 0)
//    {
//        cout << "no solute!" << endl;
//        return 0;
//    }
//    for (int i = 1; i <= cnt; i++)
//    {
//        for (int j = 1; j <= n; j++)
//            cout << Line[i][j] << " ";
//        cout << endl;
//    }
//    return 0;
//}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门使用技巧: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件

标签:cnt,递归,谢谢,int,dfs,帮忙,MAXN,bool,include
来源: https://blog.csdn.net/gotstar/article/details/122280632