コード例 #1
0
        public static PyramidNode Load(string content)
        {
            Contract.Requires <ArgumentNullException>(content != null);

            var lines = content.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

            var values = lines.Select(l => l.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(nb => int.Parse(nb)));

            var valuesArr = values.Select(x => x.ToArray()).ToArray();
            var nodes     = new PyramidNode[valuesArr.Length][];

            for (int row = 0; row < nodes.Length; row++)
            {
                nodes[row] = new PyramidNode[valuesArr[row].Length];
                for (int col = 0; col < nodes[row].Length; col++)
                {
                    var p = new PyramidNode();
                    nodes[row][col] = p;
                    p.Row           = row;
                    p.Col           = col;

                    p.Value = valuesArr[row][col];

                    if (row > 0 && col < nodes[row].Length - 1)
                    {
                        p.Father          = nodes[row - 1][col];
                        p.Father.LeftNode = p;
                    }
                    if (row > 0 && col > 0)
                    {
                        p.Mother           = nodes[row - 1][col - 1];
                        p.Mother.RigthNode = p;
                    }
                    nodes[row][col] = p;
                }
            }
            return(nodes[0][0]);
        }
コード例 #2
0
        public static PyramidNode Load(string content)
        {
            Contract.Requires<ArgumentNullException>(content != null);

            var lines = content.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

            var values = lines.Select(l => l.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(nb => int.Parse(nb)));

            var valuesArr = values.Select(x => x.ToArray()).ToArray();
            var nodes = new PyramidNode[valuesArr.Length][];
            for (int row = 0; row < nodes.Length; row++)
            {
                nodes[row] = new PyramidNode[valuesArr[row].Length];
                for (int col = 0; col < nodes[row].Length; col++)
                {
                    var p = new PyramidNode();
                    nodes[row][col] = p;
                    p.Row = row;
                    p.Col = col;

                    p.Value = valuesArr[row][col];

                    if (row > 0 && col < nodes[row].Length -1)
                    {
                        p.Father = nodes[row - 1][col];
                        p.Father.LeftNode = p;
                    }
                    if (row > 0 && col > 0)
                    {
                        p.Mother = nodes[row - 1][col - 1];
                        p.Mother.RigthNode = p;
                    }
                    nodes[row][col] = p;
                }
            }
            return nodes[0][0];
        }