这个N皇后问题绝了
PTA上的测试就有一个过不了。
等我有空了再来调试吧。
#include<stdio.h>
#include<math.h>
int max = 0, sum = 0;
int *a = NULL;
int flag = 0;
void show(){ //显示每次成功后整个界面的坐标
/*for(int i = 0; i < max; i++){
printf("(%d, %d)\t", i, a[i]);
}*/
if(flag == 1){
printf("\n");
}
for(int i = 0; i < max; i++){
int flag2 = 0;
for(int j = 0; j < max; j++){
if(flag2 == 1){
printf(" ");
}
if(j == a[i]){
printf("Q");
}else{
printf(".");
}
flag2 = 1;
}
printf("\n");
}
flag = 1;
}
int check(int n){
for(int i = 0; i < n; i++){ //这里只需要比较到已知就行
if(a[i] == a[n] || abs(a[n] - a[i]) == (n-i))//这里比较关键,就是判断现在放下的皇后是否与之前 放下的皇后有冲突(即不同列,不同对角线)
return 0 ;
}
return 1; //之前有调用 eightQueen(n+1); //保证了不同行
}
void eightQueen(int n){
int i;
if(n < max){
for(i = 0; i < max; i++){
a[n] = i;
if(check(n))
eightQueen(n + 1);
}
}
else{
sum++;
show();
}
}
void test(){
sum = 0;
scanf("%d", &max);
a = (int *)malloc(sizeof(int) * max);
if(a != NULL){
eightQueen(0); //从第零行开始
if(sum == 0){
printf("None");
}
}
}
int main(){
test();
}
我把max初始化为0就通过了检验,这是为什么呢?我再仔细看看。
max = 8就不行,很苦恼!!!
旅游规划问题 使用动态规划的方法,画表。思想是这样,但是里面的细节还得自己琢磨。同时声明,这里的代码是从
https://blog.csdn.net/shujian_tianya/article/details/80873892
粘贴过来,仅供参考,后续会加上我自己的理解。若是有疑问,请私聊我!
#include <iostream>
#include <cstdlib>
using namespace std;
const int MVNUM = 1e3;
const int INF = 1e5;
//节点到源节点的最短距离,是否被访问,最小花费
struct{
int visit, len, cost;
}Vertices[MVNUM];
struct {
int len, cost;
}MGraph[MVNUM][MVNUM];
//网的初始权值是无穷
//初始化边的集合
void InitGraph(int N)
{
for (int i = 0; i <= N; i++)
for (int j = 0; j <= N; j++)
{
MGraph[i][j].len = INF;
MGraph[i][j].cost = INF;
}
}
void InputGraph(int M)
{
int st, ed, len, cost;
for (int i = 0; i < M; i++)
{
cin >> st >> ed >> len >> cost;
MGraph[st][ed].len = len;
MGraph[st][ed].cost = cost;
MGraph[ed][st].len = len;
MGraph[ed][st].cost = cost;
}
}
void InitVertices(int N, int S)
{
for (int i = 0; i <= N; i++)
{
Vertices[i].visit = 0;
Vertices[i].len = MGraph[S][i].len;
Vertices[i].cost = MGraph[S][i].cost;
}
Vertices[S].visit = 1;
Vertices[S].len = 0;
Vertices[S].cost = 0;
}
void Dijkstra(int N, int S)
{
InitVertices(N, S);
int cnt = N-1;
//循环N-1次,因为要求N-1个节点到源节点的最短距离
while(cnt--)
{
//更新MinVer
int MinVer = N;
for (int i = 0; i < N; i++)
{
if (!Vertices[i].visit && Vertices[i].len < Vertices[MinVer].len)
MinVer = i;
}
//本题应该不存在这种情况 可不写(写了更逻辑更完整)
if (MinVer == N) break;
Vertices[MinVer].visit = 1;
for (int i = 0; i < N; i++)
{
if (!Vertices[i].visit)
{
if (Vertices[i].len > Vertices[MinVer].len + MGraph[MinVer][i].len){
Vertices[i].len = Vertices[MinVer].len + MGraph[MinVer][i].len;
Vertices[i].cost = Vertices[MinVer].cost + MGraph[MinVer][i].cost;
}
else if(Vertices[i].len == Vertices[MinVer].len + MGraph[MinVer][i].len)
{
if (Vertices[i].cost > Vertices[MinVer].cost + MGraph[MinVer][i].cost)
Vertices[i].cost = Vertices[MinVer].cost + MGraph[MinVer][i].cost;
}
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
int N, M, S, D;
cin >> N >> M >> S >> D;
InitGraph(N);
InputGraph(M);
Dijkstra(N, S);
cout << Vertices[D].len << " " << Vertices[D].cost << endl;
system("pause");
return 0;
}
本文探讨了N皇后问题的解决方法,通过递归和回溯实现棋盘填充,并展示了如何利用动态规划解决旅行规划问题,包括算法实现及具体步骤。
856

被折叠的 条评论
为什么被折叠?



