1

基本的に、私の最初のタスクは、「0」の位置を整数で保存することでした。標準配列で本当にシンプル。このコードは、0 が見つかるまで配列 (サイズ: 8) をループし、それを位置として保存します。以下のコードを参照してください。

ps: n は別の場所に保存された配列への参照です。

int position = 0;
        this.nodesExpanded++;
        // Loop through the array to get the position of where '0' is
        for (int i = 0; i < n.getPuzzle().length; i++){
            if (n.getPuzzle()[i] == 0){
                position = i;
                break;
            }
        }

私の最終的な仕事は、これを多次元配列 (サイズ: [3, 3]) で可能にすることでした。それで、これまでに作成したものは次のとおりです。

for (int x = 0; x < 3; x++)
        {
            for (int y = 0; y < 3; y++)
            {
                if (n.getPuzzle()[x,y] == 0)
                {
                    **position = ;**
                    break;
                }
            }//end y loop
        }//end x loop

では、ある場所への配列参照を値に保存するにはどうすればよいでしょうか。「位置」は、私が推測しているint以外のものである必要があります..

さらに明確にする必要がある場合は、必ずコメントしてください。

4

3 に答える 3

2

a を使用しTupleてその位置を保存できます。または、独自のデータ構造を作成することもできます。

例: 最後に、タプル項目にアクセスする方法を確認できます。

var positions = new List<Tuple<int, int>>();

            for (int x = 0; x < 3; x++)
            {
                for (int y = 0; y < 3; y++)
                {
                    if (n.getPuzzle()[x,y] == 0)
                    {
                        positions.Add(new Tuple<int, int>(x,y));
                        break;
                    }
                }//end y loop
            }//end x loop

            if(positions.Any())
            {
                var xpos = positions[0].Item1;
                var ypos = positions[0].Item2;
            }
于 2013-04-06T16:12:55.863 に答える
0

I find a natural way to store a multidimensional array index is to use a single dimensional array whose size is the number of dimensions.

So if you have a object[,,] A and index int[] i you would index into A with the expression A[i[0],i[1],i[2]].

于 2013-04-06T16:26:53.830 に答える