| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1091 人关注过本帖, 1 人收藏
标题:八皇后的问题
只看楼主 加入收藏
小可儿
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-6-20
收藏(1)
 问题点数:0 回复次数:6 
八皇后的问题

#include<stdio.h>
void search(int n);
int canplace(int n,int i);
void place(int n,int i);
void replace(int n,int i);
void print();
int a[8]={0},b[8]={0},c[15]={0},d[15]={0};
void main()
{
search(0);
}
void search(int n)
{
int i;
if(n>=8)
{
print();
printf("\n");
return;
}
for(i=0;i<8;i++)
if(canplace(n,i))
{
place(n,i);
search(n+1);
replace(n,i);
}
}
int canplace(int n,int i)
{
if(b[i]==1||c[i+n]==1||d[n-i+7]==1)
return(0);
else return(1);
}
void place(int n,int i)
{
a[n]=i;
b[i]=1;
c[n+i]=1;
d[n-i+7]=1;
}
void replace(int n,int i)
{
a[n]=0;
b[i]=0;
c[n+i]=0;
d[n-i+7]=0;
}

void print()
{
int i,j;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
if(a[i]==j)
printf(" Y");
else
printf(" *");
}
printf("\n");
}
}

这个是在置定的帖子里找到的程序
运行结果好像没有92种吧

搜索更多相关主题的帖子: 皇后 
2006-06-20 18:41
SunShining
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:31
帖 子:2215
专家分:0
注 册:2006-2-17
收藏
得分:0 
你搜索帖子.有92种的

[glow=255,violet,2]闭关修炼ing...[/glow] [FLASH=360,180]http://www./chinaren.swf[/FLASH]
2006-06-20 22:12
小可儿
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-6-20
收藏
得分:0 
#include<iostream.h>
#include<iomanip.h>
#include<fstream.h>
//用类定义一个节点,包含x坐标和y坐标以及该节点的值value
class CNode
{
public:
int x;
int y;
int value;
void SetValue(){value=1;}
void ClearValue(){value=0;}
};
CNode array[8][8];//定义一个8*8的节点矩阵
int cnt=0;//定义一个计数器
//打印函数,打印此时的矩阵数据
void PrintArray(ofstream& out)
{
int i,j;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
out<<setw(3)<<array[i][j].value;
}
out<<endl;
}
return;
}
//判断a节点是否与位于a节点之前已存在所有任一节点位于同一直线上
int InSameLine(CNode a)
{
int i,j;
for(i=0;i<=a.y;i++)
for(j=0;j<(i<a.y?8:a.x);j++)
{
if(array[i][j].value==1)
{
if(a.x==array[i][j].x||a.y==array[i][j].y||
a.x-a.y==array[i][j].x-array[i][j].y||
a.x+a.y==array[i][j].x+array[i][j].y)
return 1;
}
}
return 0;
}
//依次产生所有的节点
void CreateNextNode(int row,ofstream out)
{
if(row==8)
{
out<<"第"<<cnt+1<<"种解法"<<endl;
PrintArray(out);
cnt++;
out<<endl;
return;
}
for(int j=0;j<8;j++)
{
if(!InSameLine(array[row][j]))
{
array[row][j].SetValue();
CreateNextNode(++row,out);
array[--row][j].ClearValue();
}
}
}
//主函数main()
void main()
{
ofstream outfile("result.txt");
if(!outfile)
{
cout<<"打开文件失败!"<<endl;
return;
}
int i,j;
for(i=0;i<8;i++)
for(j=0;j<8;j++)
{
array[i][j].x=j;
array[i][j].y=i;
array[i][j].value=0;
}
CreateNextNode(0,outfile);
outfile<<"总共有"<<cnt<<"种解法"<<endl;
outfile.close();
cout<<"传输文件完毕!"<<endl;
return;
}




这个是92种的,可是运行的时候会生成一个TXT的文件,请问怎么不生成文件,直接在屏幕上显示结果呢?
2006-06-21 15:52
小可儿
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-6-20
收藏
得分:0 
请问怎么才能用递归并用栈作为辅助结构来实现呢?
谢谢高手指教!
2006-06-21 16:13
wzsxm_2
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2006-6-7
收藏
得分:0 
请问怎么才能用递归并用栈作为辅助结构来实现呢?
谢谢高手指教!

2006-06-22 21:33
cedricporter
Rank: 1
等 级:新手上路
帖 子:49
专家分:3
注 册:2007-2-6
收藏
得分:0 

#include "SStack.h"
using namespace std;

const int N = 8;

struct Position
{
int x, y;
};
Stack<Position*> s;
Position *p = new Position[N];

// Test Function
int test();

int main()
{
p->x = p->y = 1;
int count = 1;

s.push(p);
bool done = true;
while (1)
{
if (s.top()->y > N)
{
if (s.top()->x == 1)
break;
if (s.top()->x == 2)
{
s.pop();
s.pop();
(p->y)++;
s.push(p);
continue;
}
s.pop();
s.pop();
((p + s.top()->x)->y)++;
s.push((p + s.top()->x));
continue;
}

if (test() && s.top()->x == N)
{
cout << "第" << count++ << "种: ";
for (int i = 0; i < N; i++)
cout /*<< (p + i)->x << ' '*/ << (p + i)->y /*<< endl*/;
cout << endl;
//system("pause");
}

if (test() && s.top()->x != N)
{
(p + s.top()->x)->x = s.top()->x + 1;
(p + s.top()->x)->y = 1;
s.push((p + s.top()->x));
}
else
{
s.pop();
((p + s.top()->x)->y)++;
s.push((p + s.top()->x));
}

}

system("pause");
return 0;
}

int test()
{
if (s.top()->x == 1)
return 1;
for (int i = 0; i < s.top()->x - 1; i++)
{
if ((p+i)->y == s.top()->y)
return 0;
else if (s.top()->x - (p+i)->x == s.top()->y - (p+i)->y)
return 0;
else if (s.top()->x - (p+i)->x == (p+i)->y - s.top()->y)
return 0;
}
return 1;
}



清脆的口琴聲﹏悠揚的旋律﹏然而︵每個音符︵?°都充滿了悲傷︵?°~↘
2007-04-01 10:26
cedricporter
Rank: 1
等 级:新手上路
帖 子:49
专家分:3
注 册:2007-2-6
收藏
得分:0 

#ifndef SStack_H
#define SStack_H
// 版权没有。。。。欢迎盗版.... -_-ii
// Made By [Stupid.ET] Cedric Porter
#include <iostream>
#include <cassert>
using namespace std;

template<class T>
class Stack;

template<class T>
class Node
{
T data;
Node *next;
public:
Node(T d, Node<T>* n) { data = d; next = n;}
friend class Stack<T>;
};

template<class T>
class Stack
{
Node<T> *head, *iter;
unsigned long sz;
public:
Stack() {head = iter = NULL; sz = 0;}
~Stack() {empty();}
void empty();
void push(T);
void pop();
T top();
unsigned long size();
};

template<class T>
void Stack<T>::empty()
{
while (head != NULL)
{
iter = head;
head = head->next;
delete head;
}
sz = 0;
}
template<class T>
void Stack<T>::push(T d)
{
if (head == NULL)
head = new Node<T>(d, NULL);
else
head = new Node<T>(d, head);
sz++;
}

template<class T>
void Stack<T>::pop()
{
assert(head != NULL);
sz--;
if (head->next == NULL)
{
delete head;
head = NULL;
return;
}

iter = head;
head = head->next;
delete iter;
}

template<class T>
T Stack<T>::top()
{
assert(head != NULL);
return head->data;
}

template<class T>
unsigned long Stack<T>::size()
{
return sz;
}
#endif



清脆的口琴聲﹏悠揚的旋律﹏然而︵每個音符︵?°都充滿了悲傷︵?°~↘
2007-04-01 10:27
快速回复:八皇后的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012327 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved