| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 520 人关注过本帖
标题:栈中的地图四染色问题(请问这个程序的错误所在)
只看楼主 加入收藏
cytnm
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2012-12-29
结帖率:0
收藏
已结贴  问题点数:5 回复次数:2 
栈中的地图四染色问题(请问这个程序的错误所在)
#include<iostream>
#include<cstring>
using namespace std;
#define MAP EType

struct MAP
{
    int AreaIndex;
    char color[5];
    char place[3];
};

struct SType
{
    int AreaIndex;
    int ColorIndex;
};

struct Stack
{
    SType *element;
    int top;
    int maxsize;
};

void CreatStack(Stack &S,int m)
{
   S.maxsize=m;
    S.top=-1;
}

void Push(Stack &S,SType &x)
{
    S.top++;
    S.element[S.top]=x;
}

bool IsEmpty(Stack &S)
{
    if(S.top==-1) return false;
    return true;
}

bool Pop(Stack &S,SType &x)
{
    if(S.top==-1) return false;
    x=S.element[S.top];
    S.top--;
    return true;
}



Stack MapColor(int r[8][8],int n)
{
    int currentArea=1;
    int currentColor=1;
    Stack S;
    int m=100;
    SType x;
    CreatStack(S,m);
    x.AreaIndex=currentArea;
    x.ColorIndex=currentColor;
    Push(S,x);
    int topkeep;
    currentArea++;
    while(currentArea<=n)
    {
        topkeep=S.top;
        bool flag=true;
        while(IsEmpty(S)&&flag)
        {
             if(Pop(S,x))
             {
                 if(x.ColorIndex==currentColor&&r[currentArea][x.AreaIndex])
                 flag=false;
             }
        }
        if((!IsEmpty(S)&&x.ColorIndex!=currentColor)||(!IsEmpty(S)&&!r[currentArea][x.AreaIndex]))
        {
            x.AreaIndex=currentArea;
            x.ColorIndex=currentColor;
            S.top=topkeep;
            Push(S,x);
                currentArea++;
                currentColor=1;
        }
        else
        {
            currentColor++;
            S.top=topkeep;
            while(currentColor>4)
            {
                Pop(S,x);
                currentColor=x.ColorIndex+1;
                currentArea=x.AreaIndex;
            }
        }
        flag=true;
    }
    return S;
}

int main()
{
      cout<<"共有7个区域,四种颜色(红黄蓝绿)"<<endl;
      char place[7][3]={"D1","D2","D3","D4","D5","D6","D7"};
      int r[8][8]={{0, 1, 1, 1, 1, 1, 0},
    {1 ,0 ,0, 0, 0, 1, 0},
    {1, 0, 0, 1, 1, 0, 0},
    {1, 0, 1, 0, 1, 1, 0},
    {1, 0, 1, 1, 0, 1, 0},
    {1, 1, 0, 1, 1, 0, 0},
    {0, 0, 0, 0, 0, 0, 0}};
      int num=7;
      Stack S=MapColor(r,num);
      MAP inform[7];
      for(int i=0;i<=S.top;i++)
      {
          inform[i].AreaIndex=i+1;
           strcpy(inform[i].place,place[i]);
          if(S.element[i].ColorIndex==1)
                strcpy(inform[i].color,"红色");
          else
          {
              if(S.element[i].ColorIndex==2)
                   strcpy(inform[i].color,"黄色");
              else
              {
                    if(S.element[i].ColorIndex==3)
                       strcpy(inform[i].color,"蓝色");
                    else   strcpy(inform[i].color,"绿色");
              }
          }
         
      }
      cout<<"编号\t\t\t\t\t"<<"区域\t\t\t\t\t"<<"颜色\t\t\t\t\t"<<endl;
      for(int j=0;j<=S.top;j++)
      {
          cout<<inform[j].AreaIndex<<"\t\t\t\t\t"<<inform[j].place<<"\t\t\t\t\t"<<inform[j].color<<"\t\t\t\t\t"<<endl;
      }
      return 0;
}
搜索更多相关主题的帖子: void element include color 
2013-04-13 21:15
lintaoyn
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:4
帖 子:606
专家分:2499
注 册:2009-4-8
收藏
得分:3 
程序代码:
void CreatStack(Stack &S,int m)
{
    S.element = new SType[m]; // 为element分配内存
    S.maxsize=m;
    S.top=-1;
}

迭代的是人,递归的是神。
2013-04-14 12:53
邓士林
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:淮河河畔
等 级:贵宾
威 望:61
帖 子:2392
专家分:13384
注 册:2013-3-3
收藏
得分:3 
栈要分配内存空间才能运行;
void CreatStack(Stack &S,int m)
{
    S.element=new SType[S.maxsize];
   S.maxsize=m;
    S.top=-1;
}

Maybe
2013-04-14 15:21
快速回复:栈中的地图四染色问题(请问这个程序的错误所在)
数据加载中...
 
   



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

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