栈中的地图四染色问题(请问这个程序的错误所在)
#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;
}