N皇后:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void findQueenPosition (int head);
int isInTheRightPosition (int layer, int j);
void exchange (int a, int b);
void outPut ();
int A[100], n, no = 1;
int main()
{
int i;
scanf ("%d", &n);
for (i = 1; i <= n; i++)
{
A[i] = i;
}
findQueenPosition(1);
return 0;
}
void findQueenPosition (int head)
{
int i;
if (head == n + 1)
{
outPut();
}
else
{
for (i = head; i <= n; i++)
{
if (isInTheRightPosition (head, A[i]))
{
exchange(head, i);
findQueenPosition (head + 1);
exchange(head, i);
}
}
}
}
int isInTheRightPosition (int layer, int j)
{
int i;
for (i = 1; i < layer; i++)
{
if (abs (j - A[i]) == abs (layer - i))
{
return 0;
}
}
return 1;
}
void exchange (int a, int b)
{
A[0] = A[a];
A[a] = A[b];
A[b] = A[0];
}
void outPut ()
{
int i, j;
printf ("NO : %d\n", no++);
for (i = 1; i <= n; i++)
{
for (j =1; j <= n; j++)
{
if (A[i] == j)
{
printf ("@");
}
else
{
printf (".");
}
}
printf ("\n");
}
printf ("\n");
}
着色问题:
#include <stdio.h>
#include <stdlib.h>
struct edgeNode
{
int headVex;
struct edgeNode *next;
};
struct vexNode
{
int vex;
struct edgeNode *head;
};
struct graphList
{
struct vexNode vex[3000];
int vexNum;
int edgeNum;
};
void run ();
void createNewGraphList (struct graphList *gList);
void clearVexNodes (struct graphList *gList);
int findVex (int vex,struct graphList *gList);
void createNewEdgeNode (int n,struct graphList *gList);
void getColor (struct graphList *gList, int curVex);
int isRightColor (struct graphList *gList, int curVex, int i);
void outPut (struct graphList *gList);
int colorN;
int color[3000];
int no = 1;
int main()
{
struct graphList gList;
createNewGraphList (&gList);
scanf ("%d", &colorN);
getColor (&gList, 0);
return 0;
}
void createNewGraphList (struct graphList *gList)
{
scanf ("%d%d",&(gList->vexNum),&(gList->edgeNum));
clearVexNodes (gList);
int i;
for (i=0;i<gList->vexNum;i++)
{
scanf ("%d",&(gList->vex[i].vex));
}
int vex,n;
for (i=0;i<gList->edgeNum;i++)
{
scanf ("%d",&vex);
n=findVex (vex,gList);
createNewEdgeNode (n,gList);
}
}
void clearVexNodes (struct graphList *gList)
{
int i;
for (i=0;i<gList->vexNum;i++)
{
gList->vex[i].vex=0;
gList->vex[i].head=NULL;
}
}
int findVex (int vex,struct graphList *gList)
{
int i;
for (i=0;i<gList->vexNum;i++)
{
if (vex==gList->vex[i].vex)
{
return i;
}
}
return -1;
}
void createNewEdgeNode (int n,struct graphList *gList)
{
struct edgeNode *p,*q;
int vex;
p=(struct edgeNode *)malloc(sizeof(struct edgeNode));
scanf ("%d",&vex);
p->headVex=findVex (vex,gList);
p->next=NULL;
if (gList->vex[n].head==NULL)
{
gList->vex[n].head=p;
}
else
{
q=gList->vex[n].head;
while (q->next)
{
q=q->next;
}
q->next=p;
}
}
void getColor (struct graphList *gList, int curVex)
{
int i;
if (curVex == gList->vexNum)
{
outPut (gList);
}
else
{
for (i = 1; i <= colorN; i++)
{
if (isRightColor (gList, curVex, i))
{
color[curVex] = i;
getColor (gList, curVex + 1);
color[curVex] = 0;
}
}
}
}
int isRightColor (struct graphList *gList, int curVex, int i)
{
struct edgeNode *q;
q = gList->vex[curVex].head;
while (q)
{
if (i == color[q->headVex])
{
return 0;
}
q = q->next;
}
return 1;
}
void outPut (struct graphList *gList)
{
int i;
printf ("No:%d\n", no++);
for (i = 0; i < gList->vexNum; i++)
{
printf ("vex%d : color%d\n", i + 1, color[i]);
}
printf("\n\n");
}
没有数据,我就自己变了个,看着挺对的。
为表明回溯的身份,每一次我都重置了(虽然毫无必要。。。)