Nadesłany przez Marek Rudolf, 02 listopada 2005 01:00
Kod przedstawiony poniżej przedstawia główną część rozwiązania problemu.Pobierz pełne rozwiązanie.
Jeżeli nie odpowiada Ci sposób formatowania kodu przez autora skorzystaj z pretty printer'a i dostosuj go automatycznie do siebie.
KG/Unit1.cpp:
//Kolorowanie grafu
//Autor: Piramix (Marek Rudolf)
//WWW: http://www.algorytm.org
// http://www.piramix.prv.pl
// http://zse.ids.bielsko.pl/~acid/
//---------------------------------------------------------------------------
#include <vcl.h>
#include <list>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
using namespace std;
TForm1 *Form1;
int size;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
size=StrToInt(Edit1->Text);
StringGrid1->ColCount=1+size;
StringGrid1->RowCount=1+size;
for(int i=0;i<=size;i++)
for(int j=0;j<=size;j++)
{
if(i<1)
StringGrid1->Cells[i][j]=j;
if(j<1)
StringGrid1->Cells[i][j]=i;
if(i>0&&j>0)
StringGrid1->Cells[i][j]="0";
}
}
bool TForm1::istnieje_niepokolorowany(){
bool istnieje=false;
for(int i=1;i<=size;i++)
if(StringGrid1->Cells[i][i]<1) {
istnieje=true;
break;
}
return istnieje;
}
int TForm1::niepokolorowany(int v){
int z;
z=0;
for(int i=1;i<=size;i++)
if(StringGrid1->Cells[i][i]<1&&i>v)
{
z=i;
break;
}
return z;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int i,v,w;
int kolor;
bool jest;
list<int> nowy_kolor;
list<int>::iterator nowy_ki;
i=1;
kolor=0;
for(int i=1;i<=size;i++)
StringGrid1->Cells[i][i]=0;
while(istnieje_niepokolorowany())
{
nowy_kolor.clear();
kolor=kolor+1;
v=niepokolorowany(0);
while(v>0){
jest=false;
if(!nowy_kolor.empty())
{
nowy_ki=nowy_kolor.begin();
w=(*nowy_ki);
}
else
w=0;
while(w>0){
if(StringGrid1->Cells[v][w]>0&&StringGrid1->Cells[w][v]>0)
{
jest=true;
}
if(nowy_ki!=nowy_kolor.end())
{
nowy_ki++;
w=(*nowy_ki);
}
else
w=0;
}
if(jest==false)
{
StringGrid1->Cells[v][v]=kolor;
nowy_kolor.push_back(v);
}
v=niepokolorowany(v);
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
int ARow, TRect &Rect, TGridDrawState State)
{
int offset=10;
if(ARow==ACol&&ARow>0){
StringGrid1->Canvas->Brush->Style=bsSolid;
if(StringGrid1->Cells[ACol][ARow]!=""){
offset=StrToInt(StringGrid1->Cells[ACol][ARow]);
switch(offset){
case 0: offset=clWhite;break;
case 1: offset=clRed;break;
case 2: offset=clGreen;break;
case 3: offset=clYellow;break;
case 4: offset=clBlue;break;
case 5: offset=clTeal;break;
case 6: offset=clBlack;break;
case 7: offset=clLime;break;
default:
if (offset<40)
offset=offset*5;
offset=RGB(offset,45,130);
}
StringGrid1->Canvas->Brush->Color=offset;
}
// StringGrid1->Canvas->Brush->Color=clRed;
StringGrid1->Canvas->FillRect(Rect);
//DrawText(StringGrid1->Canvas->Handle,StringGrid1->Cells[ACol][ARow].AnsiLastChar(),StringGrid1->Cells[ACol][ARow].Length(),&Rect,DT_LEFT);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::StringGrid1KeyPress(TObject *Sender, char &Key)
{
if(StringGrid1->Col==StringGrid1->Row)
Key=0;
else
if(Key!='0')
{
Key=0;
StringGrid1->Cells[StringGrid1->Col][StringGrid1->Row]="1";
StringGrid1->Cells[StringGrid1->Row][StringGrid1->Col]="1";
}
else
{
StringGrid1->Cells[StringGrid1->Col][StringGrid1->Row]="0";
StringGrid1->Cells[StringGrid1->Row][StringGrid1->Col]="0";
}
}
//---------------------------------------------------------------------------

