2016-08-02 15 views
0

一直在看這一段時間,無法弄清楚。正如下面的評論所指出的那樣,這些指數在列表中。我如何在這裏獲得超範圍的異常?

using System; 
using System.IO; 
using System.Linq; 
using System.Collections.Generic; 

public class Mine 
{ 
    public int Distance { get; set; } 
    public int Gold { get; set; } 
} 

public class Move 
{ 
    public int SourceIndex { get; set; } 
    public int DestinationIndex { get; set; } 
    public int Cost { get; set; } 
} 

public class Program 
{ 
    public static void Main() 
    { 

     var mines = new List<Mine>() { 
      new Mine() { Distance = 10, Gold = 1 }, 
      new Mine() { Distance = 20, Gold = 2 }, 
      new Mine() { Distance = 25, Gold = 1 } 
     }; 

     // Cost of consolidating the gold from mines[i1] to mines[i2] 
     Func<int,int,int> Cost = (i1, i2) => Math.Abs(mines[i1].Distance - mines[i2].Distance) * mines[i1].Gold; 

     // Number of mines to consolidate the gold into 
     int k = 1; 


     var bestMove = new Move() { SourceIndex = -1, DestinationIndex = -1, Cost = Int32.MaxValue }; 

     // total cost 
     int sum = 0; 

     while(mines.Count != k) 
     { 
      var indices = Enumerable.Range(0, mines.Count).ToArray(); 
      for(int i = 0, j = 1; j < indices.Length; ++i, ++j) 
      { 
       int cost_ij = Cost(i,j); 
       if(cost_ij < bestMove.Cost) 
       { 
        bestMove.SourceIndex = i; 
        bestMove.DestinationIndex = j; 
        bestMove.Cost = cost_ij; 
       } 

       int cost_ji = Cost(j,i); 
       if(cost_ji < bestMove.Cost) 
       { 
        bestMove.SourceIndex = j; 
        bestMove.DestinationIndex = i; 
        bestMove.Cost = cost_ji; 
       } 
      } 
      Console.WriteLine("bestMove.SourceIndex = {0}, bestMove.DestinationIndex = {1}", bestMove.SourceIndex, bestMove.DestinationIndex); // prints "bestMove.SourceIndex = 2, bestMove.DestinationIndex = 1" 
      sum += bestMove.Cost; 
      mines[bestMove.DestinationIndex].Gold += mines[bestMove.SourceIndex].Gold; // this is throwing an exception "Index was out of range. Must be non-negative and less than the size of the collection." 
      mines.RemoveAt(bestMove.SourceIndex); 

     } 
     Console.WriteLine(sum); 
    } 
} 

小提琴:https://dotnetfiddle.net/hYa3A0

這是沒有意義的,因爲在

  Console.WriteLine("bestMove.SourceIndex = {0}, bestMove.DestinationIndex = {1}", bestMove.SourceIndex, bestMove.DestinationIndex); // prints "bestMove.SourceIndex = 2, bestMove.DestinationIndex = 1" 
      sum += bestMove.Cost; 
      mines[bestMove.DestinationIndex].Gold += mines[bestMove.SourceIndex].Gold; // this is throwing an exception "Index was out of range. Must be non-negative and less than the size of the collection." 
      mines.RemoveAt(bestMove.SourceIndex); 

線首次運行時,

bestMove.DestinationIndex = 2 
bestMove.DestinationIndex = 1 

mines.Count = 3 

也許我只是瘋了。

+1

問題看起來驚人的相似這一個:http://stackoverflow.com/questions/38711479/where-is-the-flaw-in -my-algorithm-for-consolidating-gold-mines/38711949 這是轉讓嗎? –

+0

您正在使用bestMove的同一個實例,您需要在每個while循環後重置其字段(我猜)。 – lcastillov

+0

當該行執行時,'bestMove.DestinationIndex'和'bestMove.SourceIndex'的值是什麼?這會告訴你爲什麼你會得到例外。 –

回答

2

索引是基於零的索引。

改變for循環的:

for(int i = 0, j = 1; j < indices.Length-1; ++i, ++j) 
0

問題是與你的計數器j。 j在一個超過礦山集合(3)大小的點上被賦予4。

變化而循環的第一行是:

var indices = Enumerable.Range(0, mines.Count - 1).ToArray();