排序上的問題 |
答題得分者是:jow
|
timshadow
一般會員 發表:7 回覆:3 積分:2 註冊:2007-08-23 發送簡訊給我 |
請問各位大大
假設已經判斷好停車位哪裡有空位,並可以求出空位離出口的距離, 我想用排序方法來判斷哪個空位離出口最近, 可是我的問題是用任何排序方法只能知道最短路徑, 卻不知道要如何把空位的編號一起存起來, 舉例來說: 已知路口Road 13內有 編號13, 編號15, 編號50 有空位,其相對距離為20,70,30 對距離做排序後可得知 20,30,70 , 但是我最終目的是要得出哪個停車位編號才是最近的, 所以問題是如何將距離排序出來後,編號有跟著移動? (註: 停車格編號跟距離沒有絕對的關係,不是停車格號碼越大,距離越長) [code cpp] int i,j,parkinglot[120][10],sink_1,sink_2; ifstream in("parkinglot.txt"); if(in.fail()) {exit(0);} for(i=0;i<120;i ) { for(j=0;j<8;j ) { in>>parkinglot[i][j];//第1列:停車格編號 //第2,3列:相鄰node //第4,5列:第1node座標 //第6,7列:第2node座標 //第8列:road } } for(i=0;i<17;i ) for(j=0;j<7;j ) in>>B[i][j]; //在B表格中 點1,點2,點1and點2之距離,x1座摽,y1座摽,x2座摽,y2座摽 int road; for(i=1;i<=120;i ) { if(((TShape *)FindComponent("Shape" IntToStr(i)))->Brush->Color == clLime) //若有綠色,則停車位為空,可以計算 { int x_1=((TShape *)FindComponent("Shape" IntToStr(i)))->Left; int y_1=((TShape *)FindComponent("Shape" IntToStr(i)))->Top; int road_sink=parkinglot[i-1][7]; //位於哪個road Form1->Memo3->Lines->Add("在Road[" String(road_sink) "]"); int sink_1_x=parkinglot[i-1][3]-(x_1); int sink_1_y=parkinglot[i-1][4]-(y_1); int sink_2_x=(x_1)-parkinglot[i-1][5]; int sink_2_y=(y_1)-parkinglot[i-1][6]; int distance_sink_1=hypot(sink_1_x,sink_1_y); //計算sink1的距離 int distance_sink_2=hypot(sink_2_x,sink_2_y); //計算sink2的距離 sink_A[i]=distance_sink_1; //將計算好的sink1距離存起來 sink_B[i]=distance_sink_2; //將計算好的sink2距離存起來 Form1->Memo3->Lines->Add("第" String(i) "停車位" String(distance_sink_1)); Form1->Memo3->Lines->Add("第" String(i) "停車位" String(distance_sink_2)); int k=0; switch(road_sink) { case 10: k ; sink_10[k]=parkinglot[i-1][0]; Form1->Memo4->Lines->Add(String(sink_10[k])); break; case 11: k ; sink_11[k]=parkinglot[i-1][0]; Form1->Memo4->Lines->Add(String(sink_11[k])); break; case 12: k ; sink_12[k]=parkinglot[i-1][0]; Form1->Memo4->Lines->Add(String(sink_12[k])); break; case 13: k ; sink_13[k]=parkinglot[i-1][0]; Form1->Memo4->Lines->Add(String(sink_13[k])); break; case 14: k ; sink_14[k]=parkinglot[i-1][0]; Form1->Memo4->Lines->Add(String(sink_14[k])); break; case 15: k ; sink_15[k]=parkinglot[i-1][0]; Form1->Memo4->Lines->Add(String(sink_15[k])); break; case 16: k ; sink_16[k]=parkinglot[i-1][0]; Form1->Memo4->Lines->Add(String(sink_16[k])); break; case 17: k ; sink_17[k]=parkinglot[i-1][0]; Form1->Memo4->Lines->Add(String(sink_17[k])); break; default: break; } } [/code] |
jow
尊榮會員 發表:66 回覆:751 積分:1253 註冊:2002-03-13 發送簡訊給我 |
以下是個人的建議:
(1)資料組織上應該再緊密一點, 因為過於分散以致於需要做某些動作 的時候, 因為各個資料間沒有太多關聯, 資料處理上比較麻煩 (2)以下附帶測試程式碼, 供你參考... TSink - 封裝與停車格有關的資訊, 如你程式中定義的資料. TParkingLot - 封裝與停車場相關的資訊, 其中當然要建構停車格, 以及定義一些需要的功能, 如 找距離某點最近的停車位, 查詢停車 場狀況等等 程式畫面: [code cpp] //--------------------------------------------------------------------------- #ifndef fMainH #define fMainH //--------------------------------------------------------------------------- #include #include #include #include #include <Forms.hpp><br />#include <Buttons.hpp><br />//--------------------------------------------------------------------------- class TSink : public TLabel { private: AnsiString __fastcall GetKey(); void __fastcall SetEmpty(int Value); public: int FSinkInfo[10]; __fastcall TSink(Classes::TComponent* AOwner, int Index); float __fastcall Distance(int X, int Y); __property AnsiString Key = {read=GetKey}; __property int Index = {read=FSinkInfo[0]}; __property int Node1 = {read=FSinkInfo[1]}; __property int Node2 = {read=FSinkInfo[2]}; __property int NodeX1 = {read=FSinkInfo[3]}; __property int NodeY1 = {read=FSinkInfo[4]}; __property int NodeX2 = {read=FSinkInfo[5]}; __property int NodeY2 = {read=FSinkInfo[6]}; __property int Road = {read=FSinkInfo[7]}; __property int Empty = {read=FSinkInfo[8], write=SetEmpty}; }; //--------------------------------------------------------------------------- class TParkingLot : public TCustomControl { private: AnsiString FFileName; TSink* FSinks[120]; TSink* __fastcall GetSinks(int Index); void __fastcall ConstructSinks(); void __fastcall DestroySinks(); protected: bool IsConstructed; void __fastcall Paint(void); public: __fastcall TParkingLot(Classes::TComponent* AOwner); __fastcall ~TParkingLot(void); __property TSink* Sinks[int Index]={read=GetSinks}; }; //--------------------------------------------------------------------------- class TForm1 : public TForm { __published: TBitBtn *BitBtn1; TPanel *Car; TBitBtn *BitBtn2; TLabel *Label1; void __fastcall BitBtn1Click(TObject *Sender); void __fastcall BitBtn2Click(TObject *Sender); private: int BestSinkIndex; protected: TParkingLot* ParkingLot; public: __fastcall TForm1(TComponent* Owner); }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif [/code] [code cpp] //--------------------------------------------------------------------------- #include #pragma hdrstop #include "fMain.h" #include "math.h" #include "algorithm.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- //{ TSink }------------------------------------------------------------------ //--------------------------------------------------------------------------- __fastcall TSink::TSink(Classes::TComponent* AOwner, int Index) : TLabel(AOwner) { Color = clLime; } //--------------------------------------------------------------------------- AnsiString __fastcall TSink::GetKey() { AnsiString ret; return ret.sprintf("%3.3d", Index); } //--------------------------------------------------------------------------- void __fastcall TSink::SetEmpty(int Value) { if(Value!=FSinkInfo[8]){ FSinkInfo[8] = Value; if (FSinkInfo[8] == 0) Color = clLime; else Color = clRed; } } //--------------------------------------------------------------------------- float __fastcall TSink::Distance(int X, int Y) { float X1 = Left; float Y1 = Top; float V = (X-X1)*(X-X1) (Y-Y1)*(Y-Y1); if(V!=0) return sqrt(V); return 0.0; } //--------------------------------------------------------------------------- //{ TParkingLot }------------------------------------------------------------ //--------------------------------------------------------------------------- __fastcall TParkingLot::TParkingLot(Classes::TComponent* AOwner) : TCustomControl(AOwner) { Color = clGray; ConstructSinks(); } //--------------------------------------------------------------------------- __fastcall TParkingLot::~TParkingLot(void) { DestroySinks(); } //--------------------------------------------------------------------------- void __fastcall TParkingLot::ConstructSinks() { for(int i=0; i<120; i ){ FSinks[i] = new TSink(this, i); FSinks[i]->Parent = this; FSinks[i]->FSinkInfo[0] = i; } IsConstructed = true; } //--------------------------------------------------------------------------- void __fastcall TParkingLot::Paint(void) { if(IsConstructed){ int w = (Width-20) / 15; int h = (Height-20) / 8; for(int i=0; i<120; i ){ int c = i % 15; int r = i / 15; TRect rect = Rect(w*c, h*r, w*(c 1), h*(r 1)); OffsetRect(&rect, 10, 10); InflateRect(&rect, -1, -10); FSinks[i]->BoundsRect = rect; } } } //--------------------------------------------------------------------------- void __fastcall TParkingLot::DestroySinks() { for(int i=0; i<120; i ){ delete FSinks[i]; } } //--------------------------------------------------------------------------- TSink* __fastcall TParkingLot::GetSinks(int Index) { if(Index>-1&&Index<120) return (TSink*) FSinks[Index]; return NULL; } //--------------------------------------------------------------------------- //{ TForm1 }----------------------------------------------------------------- //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { Randomize(); BestSinkIndex = -1; ParkingLot = new TParkingLot(this); ParkingLot->Parent = this; ParkingLot->BoundsRect = Rect(10,10,340, 440); BitBtn1->BoundsRect = Rect(350, 20, 450, 45); BitBtn2->BoundsRect = Rect(350, 50, 450, 75); Width = BitBtn2->BoundsRect.Right 20; Height = ParkingLot->BoundsRect.Bottom 60; } //--------------------------------------------------------------------------- void __fastcall TForm1::BitBtn1Click(TObject *Sender) { for(int i=0; i<120; i ){ ParkingLot->Sinks[random(120)]->Empty = random(MaxInt) % 2; } } //--------------------------------------------------------------------------- void __fastcall TForm1::BitBtn2Click(TObject *Sender) { Car->Left = max(0, random(Width-Car->Width*3)); Car->Top = max(0, random(Height-Car->Height*4)); Car->BringToFront(); TStringList *L = new TStringList(); try{ L->Sorted = true; for(int i=0; i<120; i ){ TSink *O = ParkingLot->Sinks[i]; if(O->Empty==0){ AnsiString SortKey; SortKey.sprintf(".4f", O->Distance(Car->Left, Car->Top)); L->AddObject(SortKey, O); } } if(L->Count==0)Label1->Caption = "停車場客滿 !!!"; else{ AnsiString S; TSink* O = (TSink*)L->Objects[0]; if(O){ if(O->Index!=BestSinkIndex){ if(BestSinkIndex!=-1) ParkingLot->Sinks[BestSinkIndex]->Color=clLime; O->Color=clYellow; BestSinkIndex = O->Index; Label1->Caption = S.sprintf("最佳位置是: %d", O->Index); } } } } __finally{ delete L; } } //--------------------------------------------------------------------------- [/code] 測試程式碼下載: http://delphi.ktop.com.tw/board.php?cid=31&fid=79&tid=90892 |
timshadow
一般會員 發表:7 回覆:3 積分:2 註冊:2007-08-23 發送簡訊給我 |
|
jow
尊榮會員 發表:66 回覆:751 積分:1253 註冊:2002-03-13 發送簡訊給我 |
===================引 用 timshadow 文 章=================== 真是太厲害了,才短短的一天就可以把程式寫出來,是我要多多學習的地方 不過我的程式能力沒有那麼強,所以有幾個問題想要問您: (1)您在選有空位停車格and最佳位置為什麼是亂數選擇? 只是以亂數來模擬設定車子的位置, 然後搜尋距離車子最近的距離.(模擬測試) (2)parkinglot.txt是亂碼? 原本讓 TParkingLot 具有LoadFromFile()與SaveToFile()的功能, parkinglot.txt就是用來儲存各個TSink 中 FSinkInfo[10]的資料, 總共 4 * 10 * 120 = 4800(byte), 後來測試時沒有用才拿掉的, 讓程式精簡些. (3)判別最佳位置不是只有一個位置嗎? 我只是以TPanel的 Left, Top來和所有空的停車位(TLabel)的Left, Top 的取距離最短的一個, 是利用TStringList 物件排序功能來處理. 另外距離的計算採用 function的方式計算. 不好意思,能否解惑?謝謝~~ |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |