王岐9.13-回溯:N皇后;着色问题-西工大算法

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");
}

















没有数据,我就自己变了个,看着挺对的。

为表明回溯的身份,每一次我都重置了(虽然毫无必要。。。)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值