2012-09-12 71 views
1

我想爲2d bin包裝做一個FNF算法,當我調用方法時,我得到錯誤的結果。請幫忙,我找不到問題。C#算法難題 - bin包裝

private void FFF() 
{ 
    int xmax = 0; 
    int movex = 0; 
    int movey = 0; 
    int maxH = RectBin.Height; 
    int maxW = RectBin.Width; 
    List<Rectangle> rectsToDraw = new List<Rectangle>(); 
    for (int i = 0; i < Shapes.Count; i++) 
    { 
     int height = Shapes[i].Height; 
     int width = Shapes[i].Width; 
     if ((movey + height) <= maxH) 
     { 
      rectsToDraw.Add(new Rectangle(movex, movey, width, height)); 
      movey = movey + height; 
      if (xmax < movex + width) 
      { 
       xmax = movex + width; 
      } 
     } 
     else if ((xmax + width) <= maxW) 
     { 
      movex = xmax; 
      rectsToDraw.Add(new Rectangle(movex, 0, width, height)); 
      movey = height; 
      xmax = movex + width; 
     } 
     else 
     { 
      Debug.Write("Message1"); 
      break; 
     } 
    } 

    for (int j = 0; j < rectsToDraw.Count; j++) 
    { 
     Debug.Write(rectsToDraw[0]); 
    } 
    r2d = rectsToDraw; 
} 

rectBin是一個公共矩形(0,0,300,190),而Shapes []是一個公共矩形列表。我有點形狀在這裏用它由前:

private void button9_Click(object sender, EventArgs e) 
{ 
    Shapes.Sort((x,y) => ((y.Width.CompareTo(x.Width)))); 
} 

當我開始FNF在調試控制檯我得到重複列表項和MESSAGE1(第一個在形狀,這不應該重複),當我應該得到14個項目。

只是要清楚,我不是在尋找算法,只是有人告訴我在哪裏犯了一個錯誤,以便oblici中的第一項被4次壓縮到rectsToDraw中,循環結束...輸入:{X = 0,Y = 0,寬度= 231,高度= 66} {X = 0,Y = 0,寬度= 167,高度= 61} {X = 0,Y = 0,寬度= 151,高度= 47} {X = 0,Y = 0,寬度= 130,高度= 40} {X = 0,Y = 0,寬度= 119,高度= 39} {X = 0,Y = 0,寬度= 115,高度= 52} {X = 0,Y = 0,寬度= 72,高度= 53} {X = 0,Y = 0,寬度= 58,高度= 46} {X = 0,Y = 0 ,寬度= 47,高度= 32} {X = 0,Y = 0,寬度= 41,高度= 47} {X = 0,Y = 0,寬度= 38,高度= 47} {X = 0,Y = 0,寬度= 33,高度= 45} {X = 0,Y = 0,寬度= 22,高度= 39} {X = 0,Y = 0,寬度= 0,高度= 0}

輸出: {X = 0,Y = 0,寬度= 231,高度= 66} {X = 0,Y = 0,寬度= 231,高度= 66} {X = 0,Y = 0,寬度= 167,高度= 61 } {X = 0,Y = 0,寬度= 231,高度= 66} {X = 0,Y = 0,寬度= 167,高度= 61} {X = 0,Y = 0,寬度= 151,高度= 47} {X = 0,Y = 0,寬度= 231,高度= 66} {X = 0,Y = 0,寬度= 167,高度= 61} {X = 0,Y = 0,寬度= 151,高度= 47} {X = 0,Y = 0,寬度= 130,高度= 40} {X = 0,Y = 0,寬度= 231,高度= 66} {X = 0,Y = 0,寬度= 167,高度= 61} {X = 0,Y = 0,寬度= 151,高度= 47} {X = 0,Y = 0,寬度= 130,高度= 40} {X = 0,Y = 0,寬度= 119,高度= 39} {X = 0,Y = 0,寬度= 231,高度= 66} {X = 0,Y = 0,寬度= 167,高度= 61} {X = 0,Y = 0,寬度= 151 ,高度= 47} {X = 0,Y = 0,寬度= 130,高度= 40} {X = 0,Y = 0,寬度= 119,高度= 39} {X = 0,Y = 0,寬度= 115,高度= 52} {X = 0,Y = 0,寬度= 231,高度= 66} {X = 0,Y = 0,寬度= 167,高度= 61} {X = 0,Y = 0,寬度= 151,高度= 47} {X = 0,Y = 0,寬度= 130,高度= 40} {X = 0,Y = 0,寬度= 119,高度= 39} {X = 0,Y = 0,寬度= 115,高度= 52} {X = 0,Y = 0,寬度= 72,高度= 53} {X = 0,Y = 0,寬度= 231,高度= 66} {X = 0,Y = 0 ,寬度= 167,高度= 61} {X = 0,Y = 0,寬度= 151,高度= 47} {X = 0,Y = 0,寬度= 130,高度= 40} {X = 0,Y = 0,寬度= 119,高度= 39} {X = 0,Y = 0,寬度= 115,高度= 52} {X = 0,Y = 0,寬度= 72,高度= 53} {X = 0,Y = 0,寬度= 58,高度= 46} {X = 0,Y = 0,寬度= 231,高度= 66} {X = 0,Y = 0,寬度= 167,高度= 61} {X = 0, Y = 0,寬度= 151,高度= 47} {X = 0,Y = 0,寬度= 130,高度= 40} {X = 0,Y = 0,寬度= 119,高度= 39} {X = 0 ,Y = 0,寬度= 115,高度= 52} {X = 0,Y = 0,寬度= 72,高度= 53} {X = 0,Y = 0,寬度= 58,高度= 46} {X = 0,Y = 0,寬度= 47,高度= 32} {X = 0,Y = 0,寬度= 231,高度= 66} {X = 0,Y = 0,寬度= 1 67,高度= 61} {X = 0,Y = 0,寬度= 151,高度= 47} {X = 0,Y = 0,寬度= 130,高度= 40} {X = 0,Y = 0,寬度= 119,高度= 39} {X = 0,Y = 0,寬度= 115,高度= 52} {X = 0,Y = 0,寬度= 72,高度= 53} {X = 0,Y = 0,寬度= 58,高度= 46} {X = 0,Y = 0,寬度= 47,高度= 32} {X = 0,Y = 0,寬度= 41,高度= 47} {X = 0,Y = 0 ,寬度= 231,高度= 66} {X = 0,Y = 0,寬度= 167,高度= 61} {X = 0,Y = 0,寬度= 151,高度= 47} {X = 0,Y = 0,寬度= 130,高度= 40} {X = 0,Y = 0,寬度= 119,高度= 39} {X = 0,Y = 0,寬度= 115,高度= 52} {X = 0,Y = 0,寬度= 72,高度= 53} {X = 0,Y = 0,寬度= 58,高度= 46} {X = 0,Y = 0,寬度= 47,高度= 32} {X = 0, Y = 0,寬度= 41,高度= 47} {X = 0,Y = 0,寬度= 38,高度= 47} {X = 0,Y = 0,寬度= 231,高度= 66} {X = 0 ,Y = 0,寬度= 167,高度= 61} {X = 0,Y = 0,寬度= 151,高度= 47} {X = 0,Y = 0,寬度= 130,高度= 40} {X = 0,Y = 0,寬度= 119,高度= 39} {X = 0,Y = 0,寬度= 115,高度= 52} {X = 0,Y = 0,寬度= 72,高度= 53} {X = 0,Y = 0,寬度= 58,高度= 46} {X = 0,Y = 0,寬度= 47,高度= 32} {X = 0,Y = 0,寬度= 41,高度= 47} { X = 0,Y = 0,寬度= 38,高度= 47} {X = 0,Y = 0,寬度= 33,高度= 45} {X = 0,Y = 0,寬度= 231,高度= 66} {X = 0,Y = 0,寬度= 167,高度= 61} {X = 0,Y = 0,寬度= 151,高度= 47} {X = 0,Y = 0,寬度= 1 30,高度= 40} {X = 0,Y = 0,寬度= 119,高度= 39} {X = 0,Y = 0,寬度= 115,高度= 52} {X = 0,Y = 0,寬度= 72,高度= 53} {X = 0,Y = 0,寬度= 58,高度= 46} {X = 0,Y = 0,寬度= 47,高度= 32} {X = 0,Y = 0,寬度= 41,高度= 47} {X = 0,Y = 0,寬度= 38,高度= 47} {X = 0,Y = 0,寬度= 33,高度= 45} {X = 0,Y = 0 ,寬度= 22,高度= 39} {X = 0,Y = 0,寬度= 231,高度= 66} {X = 0,Y = 0,寬度= 167,高度= 61} {X = 0,Y = 0,寬度= 151,高度= 47} {X = 0,Y = 0,寬度= 130,高度= 40} {X = 0,Y = 0,寬度= 119,高度= 39} {X = 0,Y = 0,寬度= 115,高度= 52} {X = 0,Y = 0,寬度= 72,高度= 53} {X = 0,Y = 0,寬度= 58,高度= 46} {X = 0, Y = 0,寬度= 47,高度= 32} {X = 0,Y = 0,寬度= 41,高度= 47} {X = 0,Y = 0,寬度= 38,高度= 47} {X = 0 ,Y = 0,寬度= 33,高度= 45} {X = 0,Y = 0,寬度= 22,高度= 39} {X = 0,Y = 0,寬度= 0,高度= 0}

+1

'visina'的價值是什麼? 'maxV'? 'max?'? – rsbarro

+0

visina是當前矩形的高度,maxV = 190,maxŠ= 350 – user1649498

+1

如果在代碼中不將英語與克羅地亞/塞爾維亞語混合,也許會更容易回答你的問題:) –

回答

2

可能有其他問題,但我認爲這個代碼:

for (int j = 0; j < rectsToDraw.Count; j++) 
{ 
    Debug.Write(rectsToDraw[0]); 
} 

應該是:

for (int j = 0; j < rectsToDraw.Count; j++) 
{ 
    Debug.Write(rectsToDraw[j]); 
} 

(變化Debug.Write(rectsToDraw[0]);Debug.Write(rectsToDraw[j]);)。

+0

thx,我確實得到了一個錯誤,但我現在改變了它,仍然得到錯誤的數據。 – user1649498

+0

數據如何錯誤?你能提供輸入和輸出嗎? – rsbarro

+0

添加了輸入,輸出列表到我的問題。此外,我認爲這可能類似於http://stackoverflow.com/questions/271440/c-sharp-captured-variable-in-loop – user1649498

0

沒關係,我設法解決它...代碼如下:

private void FNF() 
    { 
     int XMX = 0; 
     int moveX= 0; 
     int moveY = 0; 
     int MH = RectBin.Height; 
     int MW = RectBin.Width; 
     int c1 = Items.Count; 
     rectsToDraw = new List<Rectangle>(); 

     for(int i = 0; i < c1; i++) 
     { 
      int height = Oblici[i].Height; 
      int width = Oblici[i].Width; 
      if(((moveY + height) <= MV)&&((moveX+width)<=MW)) 
      { 
       rectsToDraw.Add(new Rectangle(moveX, moveY, width, height)); 
       moveY = moveY + height; 
       if (XMX < moveX + width) 
       { 
        XMX = moveX + width; 
       } 
      } 
      else if((XMX + width) <= MW) 
      { 
       moveX = XMX; 
       rectsToDraw.Add(new Rectangle(moveX, 0, width, height)); 
       moveY = height; 
       XMX = moveX + width; 
      } 
      else 
      { 
       Debug.Write("Doesn't fit."); 
       break; 
      } 
     } 
     for (int j = 0; j < rectsToDraw.Count; j++) 
     { 
      Debug.Write(rectsToDraw[j]); 
     } 
    } 

又寫了一封信,並改變了一些名字(無故)....反正感謝名單....