八皇后

软件工程部编程小结第一期
八皇后的问题,其实是一个回溯法的典型实例,下面就是它的一种实现,纯粹出自个人的想法,但是有点问题:网上说八皇后的解法有92中,我的打印出来有88种,可能哪里错了,还望高手指正!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define EmpCount 8

typedef struct
{
	int x,y;
}
DataType;

struct SeqStack
{
	int MAXNUM;
	int t;
	DataType *s;
};

typedef struct SeqStack *PSeqStack;

//创建一个空栈
PSeqStack createEmptyStack_seq(int m)
{
	PSeqStack pastack = (PSeqStack)malloc(sizeof(struct SeqStack));
	if(pastack != NULL)
	{
		pastack->s = (DataType*)malloc(sizeof(DataType)*m);
		if(pastack->s)
		{
			pastack->MAXNUM = m;
			pastack->t = -1;
			return pastack;
		}
		else free(pastack);
	}
	printf("Out of space!!\n");
	return NULL;
}

//判断栈是否为空
int isEmptyStack_seq(PSeqStack pastack)
{
	return (pastack->t==-1);
}

//压栈操作
void push_seq(PSeqStack pastack,DataType x)
{
	if(pastack->t >= pastack->MAXNUM-1)
	{
		printf("Overflow!\n");
	}
	else
	{
		pastack->t = pastack->t+1;
		pastack->s[pastack->t] = x;
	}
}

//弹栈操作
void pop_seq(PSeqStack pastack)
{
	if(pastack->t == -1)
	{
		printf("Underflow!\n");
	}
	else
		pastack->t = pastack->t - 1;
}

//返回栈顶元素
DataType top_seq(PSeqStack pastack)
{
	if(pastack->t == -1)
	{
		printf("It is empty!\n");
	}
	else
		return pastack->s[pastack->t];
}

void print(int maze[][EmpCount])
{
	printf("\t\t-1\t");
	for(int m=0;m<EmpCount;m++)
	printf("%d ",m);
	printf("\n");
	//maze[5][5] =1;
	for(m=0;m<EmpCount;m++)
	{
		printf("\n\t\t%d\t",m);
		for(int n=0;n<EmpCount;n++)
			if(maze[m][n]==1)
				printf("▲");
			else
				if((m+n)%2==0)
					printf("□");//■");
				else
					printf("□");
	}
	printf("\n\t\t\t");	
}

bool checkEmp(PSeqStack pastack,int i,int j)
{
	if(isEmptyStack_seq(pastack))
		return false;
	if( i>=EmpCount || j>=EmpCount)
		return true;
	DataType element;
	for(int k=0;k!=pastack->t+1;k++)
	{
		element = pastack->s[k];
		if( (i==element.x) || (j==element.y) || (abs(j-element.y)==(i-element.x)) )
		{
			return true;
		}
	}
	return false;
}

void Empress(int maze[][EmpCount])
{
	int allCount = 0;
	int oneEmpress [EmpCount][2];
	int count = 0;
	int test;
	DataType element,element1;
	PSeqStack pastack = createEmptyStack_seq(EmpCount);
	for(int k=0;k!=EmpCount;k++)
	{
		oneEmpress[k][0]=0;
		oneEmpress[k][1]=0;
	}
	int i=0,j=0;
	int g,h;
	element.x = 0;
	element.y = 0;
	push_seq(pastack,element);
	while(!isEmptyStack_seq(pastack))
	{
		element = top_seq(pastack);
		pop_seq(pastack);
		i = element.x;
		j = element.y+1;
		while(i<EmpCount)
		{
			if(!checkEmp(pastack,i,j))
			{
				element.x = i;
				element.y = j;
				push_seq(pastack,element);
				i++;
				j=0;
			}
			else
			{
				if(j<EmpCount-1)
					j++;
				else
				{
					element = top_seq(pastack);
					pop_seq(pastack);
					i = element.x;
					j = element.y;
					if(j<EmpCount-1)
						j++;
					else
						break;
				}
			}
		}
		if(pastack->t==EmpCount-1)
		{
			printf("\n\r第%d个八皇后图\n",++allCount);
			for(int k=0;k<=pastack->t;k++)
			{
				printf("the node is:%d,%d\n",pastack->s[k].x,pastack->s[k].y);
				maze[pastack->s[k].x][pastack->s[k].y]=1;
			}
			print(maze);
			for(int i=0;i!=EmpCount;i++)
			{
				for(int j=0;j<EmpCount;j++)
					maze[i][j] = 0;
			}
			pastack->s[pastack->t].y = pastack->s[pastack->t].y+1;
		}
	}
}

void main()
{
	int maze[EmpCount][EmpCount];
	for(int i=0;i!=EmpCount;i++)
	{
		for(int j=0;j<EmpCount;j++)
			maze[i][j] = 0;
	}
	Empress(maze);
}

你可能感兴趣的:(编程,J#)