10月13日--二维数组多维数组推箱子

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了10月13日--二维数组多维数组推箱子相关的知识,希望对你有一定的参考价值。

数组:相同数据类型的元素按照一定的顺序进行排列的

 

 

二维数组

 int[,] array = new int[3, 2];

int[,] array = new int[3, 4] { { 1, 2, 3, 4 }, { 1, 2, 3, 4 }, { 1, 2, 3, 4 } };

int[,] array = new int[3, 4] {{ 1, 2, 3, 4 },  

                                          { 1, 2, 3, 4 },

                                          { 1, 2, 3, 4 } };

 [3, 2]   3表示有三个一维数组

 [3, 2]   2表示每个一维数组有几个元素

 


打印二维数组

 

 1                 int[,] array = new int[3, 4] { 
 2                                                 { 1, 2, 3, 4 }, 
 3                                                 { 1, 2, 3, 4 }, 
 4                                                 { 1, 2, 3, 4 } };
 5                 for (int i = 0; i < 3; i++)
 6                 {
 7                     for (int j = 0; j < 4; j++)
 8                     {
 9                         Console.Write(array[i, j] + "\t");
10                     }
11                     Console.WriteLine();
12                 }

二维数组赋值

输入班级人数,输入每个人的语数英成绩

技术分享
Console.Write("请输入人数:");
                int renshu = int.Parse(Console.ReadLine());
                double[,] chengji = new double[renshu, 3];
                for (int i = 0; i < renshu; i++)
                {
                    Console.Write("请输入第" + (i + 1) + "个人的语文成绩:");
                    chengji[i, 0] = double.Parse(Console.ReadLine());
                    Console.Write("请输入第" + (i + 1) + "个人的数学成绩:");
                    chengji[i, 1] = double.Parse(Console.ReadLine());
                    Console.Write("请输入第" + (i + 1) + "个人的英语成绩:");
                    chengji[i, 2] = double.Parse(Console.ReadLine());
                }
                for (int i = 0; i < renshu; i++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        Console.Write(chengji[i, j] + "\t");
                    }
                    Console.WriteLine();
                }
示例

 


输入班级人数,输入每个人的语数英成绩
求语文最高分,数学最低分,英语平均分

技术分享

技术分享
                Console.Write("请输入人数:");
                int renshu = int.Parse(Console.ReadLine());
                double[,] chengji = new double[renshu, 3];
                double yingyu = 0;
                for (int i = 0; i < renshu; i++)
                {
                    Console.Write("请输入第" + (i + 1) + "个人的语文成绩:");
                    chengji[i, 0] = double.Parse(Console.ReadLine());
                    Console.Write("请输入第" + (i + 1) + "个人的数学成绩:");
                    chengji[i, 1] = double.Parse(Console.ReadLine());
                    Console.Write("请输入第" + (i + 1) + "个人的英语成绩:");
                    chengji[i, 2] = double.Parse(Console.ReadLine());
                    yingyu += chengji[i, 2];
                }
                for (int i = 0; i < renshu; i++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        Console.Write(chengji[i, j] + "\t");
                    }
                    Console.WriteLine();
                }

                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine();


                for (int i = 0; i < renshu - 1; i++)
                {
                    for (int j = i + 1; j < renshu; j++)
                    {
                        if (chengji[i, 0] < chengji[j, 0])
                        {
                            double zhong = chengji[i, 0];
                            chengji[i, 0] = chengji[j, 0];
                            chengji[j, 0] = zhong;

                            zhong = chengji[i, 1];
                            chengji[i, 1] = chengji[j, 1];
                            chengji[j, 1] = zhong;

                            zhong = chengji[i, 2];
                            chengji[i, 2] = chengji[j, 2];
                            chengji[j, 2] = zhong;
                        }
                    }
                }
                for (int i = 0; i < renshu; i++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        Console.Write(chengji[i, j] + "\t");
                    }
                    Console.WriteLine();
                }
                Console.WriteLine("语文最高分{0},语文最低分{1}", chengji[0, 0], chengji[renshu - 1, 0]);




                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine();


                for (int i = 0; i < renshu - 1; i++)
                {
                    for (int j = i + 1; j < renshu; j++)
                    {
                        if (chengji[i, 1] < chengji[j, 1])
                        {
                            double zhong = chengji[i, 0];
                            chengji[i, 0] = chengji[j, 0];
                            chengji[j, 0] = zhong;

                            zhong = chengji[i, 1];
                            chengji[i, 1] = chengji[j, 1];
                            chengji[j, 1] = zhong;

                            zhong = chengji[i, 2];
                            chengji[i, 2] = chengji[j, 2];
                            chengji[j, 2] = zhong;
                        }
                    }
                }
                for (int i = 0; i < renshu; i++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        Console.Write(chengji[i, j] + "\t");
                    }
                    Console.WriteLine();
                }
                Console.WriteLine("数学最高分{0},数学最低分{1}", chengji[0, 1], chengji[renshu - 1, 1]);
                Console.WriteLine("英语平均分{0}", yingyu / renshu);
答案

 

 

三维数组

               一维数组是一条线                    一排班
               二维数组是一个面 = 多个一维数组     一层楼
               三维数组一个立方体 = 多个二维数组   一栋楼

   三维数组声明
               
int[, ,] array = new int[3, 4, 2];

 

 


 


 

推箱子

技术分享技术分享

     思路:

       地图:一个三维数组进行原始地图的保存

技术分享
                #region 地图----0表示空地,1表示墙,2表示箱子,3表示箱子空位,4表示光标,5表示放好的箱子
                int[, ,] ditus = new int[3, 10, 10]{
                                {
                                    {0,0,0,0,0,0,0,0,0,0},
                                    {0,1,1,1,0,0,0,0,0,0},
                                    {0,1,3,1,0,0,0,0,0,0},
                                    {0,1,2,1,1,1,0,0,0,0},
                                    {0,1,4,2,3,1,0,0,0,0},
                                    {0,1,2,1,1,1,0,0,0,0},
                                    {0,1,3,1,0,0,0,0,0,0},
                                    {0,1,1,1,0,0,0,0,0,0},
                                    {0,0,0,0,0,0,0,0,0,0},
                                    {0,0,0,0,0,0,0,0,0,0}
                                },
                                {
                                    {0,0,0,0,0,0,0,0,0,0},
                                    {0,1,1,1,0,0,0,0,0,0},
                                    {0,1,3,1,0,0,0,0,0,0},
                                    {0,1,2,1,1,1,0,0,0,0},
                                    {0,1,4,2,3,1,0,0,0,0},
                                    {0,1,2,2,1,1,0,0,0,0},
                                    {0,1,3,3,1,0,0,0,0,0},
                                    {0,1,1,1,1,0,0,0,0,0},
                                    {0,0,0,0,0,0,0,0,0,0},
                                    {0,0,0,0,0,0,0,0,0,0}
                                },
                                {
                                    {0,0,0,0,0,0,0,0,0,0},
                                    {0,1,1,1,0,0,0,0,0,0},
                                    {0,1,3,1,0,0,0,0,0,0},
                                    {0,1,2,1,1,1,1,0,0,0},
                                    {0,1,4,0,2,3,1,0,0,0},
                                    {0,1,2,1,2,1,1,0,0,0},
                                    {0,1,3,1,3,1,0,0,0,0},
                                    {0,1,1,1,1,1,0,0,0,0},
                                    {0,0,0,0,0,0,0,0,0,0},
                                    {0,0,0,0,0,0,0,0,0,0}
                                }
                            };
                #endregion
地图

        打印地图:新建一个二维数组进行指定地图的保存,不修改地图原版

技术分享
            int[,] ditu = new int[10,10];
                #region 输出地图
                switch (guankashu)
                {
                    case 1:
                        tiaojian = 3;
                        break;
                    case 2:
                        tiaojian = 4;
                        break;
                    case 3:
                        tiaojian = 4;
                        break;
                }
                for (int i = 0; i < 10; i++)
                {
                    for (int j = 0; j < 10; j++)
                    {
                        ditu[i, j] = ditus[guankashu - 1, i, j];
                        switch(ditus[guankashu-1,i, j])
                        {
                            case 0:
                                Console.Write(" ");
                                break;
                            case 1:
                                Console.Write("");
                                break;
                            case 2:
                                Console.Write("");
                                break;
                            case 3:
                                Console.Write("×");
                                break;
                            case 4:
                                Console.Write("");
                                renx = i;
                                reny = j;
                                break;
                            case 5:
                                Console.Write("");
                                break;
                        }
                    }
                    Console.WriteLine();
                }
                #endregion
打印地图

       难点*判断此方向的下一步是否能走,走后变为什么

技术分享
            while (true)
            {
                Console.Clear();
                #region 地图----0表示空地,1表示墙,2表示箱子,3表示箱子空位,4表示光标,5表示放好的箱子
                int[, ,] ditus = new int[3, 10, 10]{
                                {
                                    {0,0,0,0,0,0,0,0,0,0},
                                    {0,1,1,1,0,0,0,0,0,0},
                                    {0,1,3,1,0,0,0,0,0,0},
                                    {0,1,2,1,1,1,0,0,0,0},
                                    {0,1,4,2,3,1,0,0,0,0},
                                    {0,1,2,1,1,1,0,0,0,0},
                                    {0,1,3,1,0,0,0,0,0,0},
                                    {0,1,1,1,0,0,0,0,0,0},
                                    {0,0,0,0,0,0,0,0,0,0},
                                    {0,0,0,0,0,0,0,0,0,0}
                                },
                                {
                                    {0,0,0,0,0,0,0,0,0,0},
                                    {0,1,1,1,0,0,0,0,0,0},
                                    {0,1,3,1,0,0,0,0,0,0},
                                    {0,1,2,1,1,1,0,0,0,0},
                                    {0,1,4,2,3,1,0,0,0,0},
                                    {0,1,2,2,1,1,0,0,0,0},
                                    {0,1,3,3,1,0,0,0,0,0},
                                    {0,1,1,1,1,0,0,0,0,0},
                                    {0,0,0,0,0,0,0,0,0,0},
                                    {0,0,0,0,0,0,0,0,0,0}
                                },
                                {
                                    {0,0,0,0,0,0,0,0,0,0},
                                    {0,1,1,1,0,0,0,0,0,0},
                                    {0,1,3,1,0,0,0,0,0,0},
                                    {0,1,2,1,1,1,1,0,0,0},
                                    {0,1,4,0,2,3,1,0,0,0},
                                    {0,1,2,1,2,1,1,0,0,0},
                                    {0,1,3,1,3,1,0,0,0,0},
                                    {0,1,1,1,1,1,0,0,0,0},
                                    {0,0,0,0,0,0,0,0,0,0},
                                    {0,0,0,0,0,0,0,0,0,0}
                                }
                            };
                #endregion

                Console.Write("请输入关卡数1~3:");
                int guankashu = int.Parse(Console.ReadLine());
                int[,] ditu = new int[10,10];
                int renx = 0, reny = 0, tiaojian = 0, tiaojianjishu = 0;
                #region 输出地图
                switch (guankashu)
                {
                    case 1:
                        tiaojian = 3;
                        break;
                    case 2:
                        tiaojian = 4;
                        break;
                    case 3:
                        tiaojian = 4;
                        break;
                }
                for (int i = 0; i < 10; i++)
                {
                    for (int j = 0; j < 10; j++)
                    {
                        ditu[i, j] = ditus[guankashu - 1, i, j];
                        switch(ditus[guankashu-1,i, j])
                        {
                            case 0:
                                Console.Write(" ");
                                break;
                            case 1:
                                Console.Write("");
                                break;
                            case 2:
                                Console.Write("");
                                break;
                            case 3:
                                Console.Write("×");
                                break;
                            case 4:
                                Console.Write("");
                                renx = i;
                                reny = j;
                                break;
                            case 5:
                                Console.Write("");
                                break;
                        }
                    }
                    Console.WriteLine();
                }
                #endregion


                while (true)
                {
                    if (tiaojian > tiaojianjishu)
                    {
                        string shuru = Console.ReadKey().Key.ToString().ToLower();
                        if (shuru == "w")
                        {
                            //reny--;
                            if (ditu[renx - 1, reny] != 1 && ditu[renx - 1, reny] != 5)
                            {
                                if (ditu[renx - 1, reny] == 2 && ditu[renx - 2, reny] != 1 && ditu[renx - 2, reny] != 2 && ditu[renx - 2, reny] != 5)
                                {
                                    if (ditu[renx - 2, reny] == 0)//箱子的下一步是空地
                                    {
                                        ditu[renx - 2, reny] = 2;//箱子的下一步变成箱子
                                    }
                                    if (ditu[renx - 2, reny] == 3)//如果箱子下一步是空位
                                    {
                                        ditu[renx - 2, reny] = 5;//箱子下一步变成放好的箱子
                                        tiaojianjishu++;
                                    }
                                    ditu[renx - 1, reny] = 4; 
                                    if (ditu[renx, reny] == 4){ditu[renx, reny] = 0;}//光标在空地上,变成空地
                                    if (ditu[renx, reny] == 7){ditu[renx, reny] = 3;}//光标在目标位上,变成目标位
                                    renx--;
                                }
                                if (ditu[renx - 1, reny] == 0)
                                {
                                    ditu[renx - 1, reny] = 4;
                                    if (ditu[renx, reny] == 4){ditu[renx, reny] = 0;}//光标在空地上,变成空地
                                    if (ditu[renx, reny] == 7){ditu[renx, reny] = 3;}//光标在目标位上,变成目标位
                                    renx--;
                                }//光标下一步是空地//下一步变成光标
                                if (ditu[renx - 1, reny] == 3)
                                {
                                    ditu[renx - 1, reny] = 7; 
                                    if (ditu[renx, reny] == 4){ditu[renx, reny] = 0;}//光标在空地上,变成空地
                                    if (ditu[renx, reny] == 7){ ditu[renx, reny] = 3; }//光标在目标位上,变成目标位
                                    renx--;
                                }//光标下一步是目标位//下一步变成在目标位上的光标
                                //if (ditu[renx, reny] == 4)
                                //{
                                //    ditu[renx, reny] = 0;
                                //}//光标在空地上,变成空地
                                //if (ditu[renx, reny] == 7)
                                //{
                                //    ditu[renx, reny] = 3;
                                //}//光标在目标位上,变成目标位
                                //renx--;
                            }
                        }
                        if (shuru == "a")
                        {
                            //renx--;

                            if (ditu[renx, reny - 1] != 1 && ditu[renx, reny - 1] != 5)
                            {
                                if (ditu[renx, reny - 1] == 2 && ditu[renx, reny - 2] != 1 && ditu[renx, reny - 2] != 2 && ditu[renx, reny - 2] != 5)
                                {
                                    if (ditu[renx, reny - 2] == 0)//箱子的下一步是空地
                                    {
                                        ditu[renx, reny - 2] = 2;//箱子的下一步变成箱子
                                    }
                                    if (ditu[renx, reny - 2] == 3)//如果箱子下一步是空位
                                    {
                                        ditu[renx, reny - 2] = 5;//箱子下一步变成放好的箱子
                                        tiaojianjishu++;
                                    }
                                    ditu[renx, reny - 1] = 4;

                                    if (ditu[renx, reny] == 4) { ditu[renx, reny] = 0; }//光标在空地上,变成空地
                                    if (ditu[renx, reny] == 7) { ditu[renx, reny] = 3; }//光标在目标位上,变成目标位
                                    reny--;
                                }
                                if (ditu[renx, reny - 1] == 0)
                                {
                                    ditu[renx, reny - 1] = 4;
                                    if (ditu[renx, reny] == 4) { ditu[renx, reny] = 0; }//光标在空地上,变成空地
                                    if (ditu[renx, reny] == 7) { ditu[renx, reny] = 3; }//光标在目标位上,变成目标位
                                    reny--;
                                }//光标下一步是空地//下一步变成光标
                                if (ditu[renx, reny - 1] == 3)
                                {
                                    ditu[renx, reny - 1] = 7;
                                    if (ditu[renx, reny] == 4) { ditu[renx, reny] = 0; }//光标在空地上,变成空地
                                    if (ditu[renx, reny] == 7) { ditu[renx, reny] = 3; }//光标在目标位上,变成目标位
                                    reny--;
                                }//光标下一步是目标位//下一步变成在目标位上的光标
                                //if (ditu[renx, reny] == 4) { ditu[renx, reny] = 0; }//光标在空地上,变成空地
                                //if (ditu[renx, reny] == 7) { ditu[renx, reny] = 3; }//光标在目标位上,变成目标位
                                //reny--;
                            }
                        }
                        if (shuru == "s")
                        {
                            //reny++;
                            if (ditu[renx + 1, reny] != 1 && ditu[renx + 1, reny] != 5)
                            {
                                if (ditu[renx + 1, reny] == 2 && ditu[renx + 2, reny] != 1 && ditu[renx + 2, reny] != 2 && ditu[renx + 2, reny] != 5)
                                {
                                    if (ditu[renx + 2, reny] == 0)//箱子的下一步是空地
                                    {
                                        ditu[renx + 2, reny] = 2;//箱子的下一步变成箱子
                                    }
                                    if (ditu[renx + 2, reny] == 3)//如果箱子下一步是空位
                                    {
                                        ditu[renx + 2, reny] = 5;//箱子下一步变成放好的箱子
                                        tiaojianjishu++;
                                    }
                                    ditu[renx + 1, reny] = 4;
                                    if (ditu[renx, reny] == 4) { ditu[renx, reny] = 0; }//光标在空地上,变成空地
                                    if (ditu[renx, reny] == 7) { ditu[renx, reny] = 3; }//光标在目标位上,变成目标位
                                    renx++;
                                }
                                if (ditu[renx + 1, reny] == 0)
                                {
                                    ditu[renx + 1, reny] = 4;
                                    if (ditu[renx, reny] == 4) { ditu[renx, reny] = 0; }//光标在空地上,变成空地
                                    if (ditu[renx, reny] == 7) { ditu[renx, reny] = 3; }//光标在目标位上,变成目标位
                                    renx++;
                                }//光标下一步是空地//下一步变成光标
                                if (ditu[renx + 1, reny] == 3)
                                {
                                    ditu[renx + 1, reny] = 7;
                                    if (ditu[renx, reny] == 4) { ditu[renx, reny] = 0; }//光标在空地上,变成空地
                                    if (ditu[renx, reny] == 7) { ditu[renx, reny] = 3; }//光标在目标位上,变成目标位
                                    renx++;
                                }//光标下一步是目标位//下一步变成在目标位上的光标
                                //if (ditu[renx, reny] == 4) { ditu[renx, reny] = 0; }//光标在空地上,变成空地
                                //if (ditu[renx, reny] == 7) { ditu[renx, reny] = 3; }//光标在目标位上,变成目标位
                                //renx++;
                            }
                        }
                        if (shuru == "d")
                        {
                            //renx++;
                            if (ditu[renx, reny + 1] != 1 && ditu[renx, reny + 1] != 5)
                            {
                                if (ditu[renx, reny + 1] == 2 && ditu[renx, reny + 2] != 1 && ditu[renx, reny + 2] != 2 && ditu[renx, reny + 2] != 5)
                                {
                                    if (ditu[renx, reny + 2] == 0)//箱子的下一步是空地
                                    {
                                        ditu[renx, reny + 2] = 2;//箱子的下一步变成箱子
                                    }
                                    if (ditu[renx, reny + 2] == 3)//如果箱子下一步是空位
                                    {
                                        ditu[renx, reny + 2] = 5;//箱子下一步变成放好的箱子
                                        tiaojianjishu++;
                                    }
                                    ditu[renx, reny + 1] = 4;
                                    if (ditu[renx, reny] == 4) { ditu[renx, reny] = 0; }//光标在空地上,变成空地
                                    if (ditu[renx, reny] == 7) { ditu[renx, reny] = 3; }//光标在目标位上,变成目标位
                                    reny++;
                                }
                                if (ditu[renx, reny + 1] == 0)
                                {
                                    ditu[renx, reny + 1] = 4;
                                    if (ditu[renx, reny] == 4) { ditu[renx, reny] = 0; }//光标在空地上,变成空地
                                    if (ditu[renx, reny] == 7) { ditu[renx, reny] = 3; }//光标在目标位上,变成目标位
                                    reny++;
                                }//光标下一步是空地//下一步变成光标
                                if (ditu[renx, reny + 1] == 3)
                                {
                                    ditu[renx, reny + 1] = 7;
                                    if (ditu[renx, reny] == 4) { ditu[renx, reny] = 0; }//光标在空地上,变成空地
                                    if (ditu[renx, reny] == 7) { ditu[renx, reny] = 3; }//光标在目标位上,变成目标位
                                    reny++;
                                }//光标下一步是目标位//下一步变成在目标位上的光标
                                //if (ditu[renx, reny] == 4) { ditu[renx, reny] = 0; }//光标在空地上,变成空地
                                //if (ditu[renx, reny] == 7) { ditu[renx, reny] = 3; }//光标在目标位上,变成目标位
                                //reny++;
                            }
                        }
                        Console.Clear();
                        Console.WriteLine("请输入关卡数1~2:" + guankashu);
                        #region 输出地图
                        for (int i = 0; i < 10; i++)
                        {
                            for (int j = 0; j < 10; j++)
                            {
                                switch (ditu[i, j])
                                {
                                    case 0:
                                        Console.Write(" ");
                                        break;
                                    case 1:
                                        Console.Write("");
                                        break;
                                    case 2:
                                        Console.Write("");
                                        break;
                                    case 3:
                                        Console.Write("×");
                                        break;
                                    case 4:
                                        Console.Write("");
                                        break;
                                    case 5:
                                        Console.Write("");
                                        break;
                                }
                            }
                            Console.WriteLine();
                        }
                        #endregion
                    }
                    else
                    {
                        Console.WriteLine("请输入回车进入下一关");
                        string shuru = Console.ReadKey().Key.ToString().ToLower();
                        try { 
                        if (shuru == "enter")
                        {
                            tiaojianjishu = 0;
                            guankashu++;
                            Console.Clear();
                            Console.WriteLine("请输入关卡数1~2:" + guankashu);
                            #region 输出地图
                            switch (guankashu)
                            {
                                case 1:
                                    tiaojian = 3;
                                    break;
                                case 2:
                                    tiaojian = 4;
                                    break;
                            }
                            for (int i = 0; i < 10; i++)
                            {
                                for (int j = 0; j < 10; j++)
                                {
                                    ditu[i, j] = ditus[guankashu - 1, i, j];
                                    switch (ditus[guankashu - 1, i, j])
                                    {
                                        case 0:
                                            Console.Write(" ");
                                            break;
                                        case 1:
                                            Console.Write("");
                                            break;
                                        case 2:
                                            Console.Write("");
                                            break;
                                        case 3:
                                            Console.Write("×");
                                            break;
                                        case 4:
                                            Console.Write("");
                                            renx = i;
                                            reny = j;
                                            break;
                                        case 5:
                                            Console.Write("");
                                            break;
                                    }
                                }
                                Console.WriteLine();
                            }
                            #endregion
                        }
                        }
                        catch
                        {
                            Console.Clear();
                            string str = "目前只有" + (guankashu-1) + "关,您已通关!!";
                            string strr = "新关卡正在制作中,敬请期待。";
                            Console.SetCursorPosition((Console.WindowWidth - str.Length * 2) / 2, 0);
                            Console.WriteLine(str);
                            Console.SetCursorPosition((Console.WindowWidth - strr.Length * 2) / 2, 1);
                            Console.WriteLine(strr);
                            guankashu = 1;
                            break;
                        }
                    }
                }

                Console.ReadKey();
            }
完整

 

以上是关于10月13日--二维数组多维数组推箱子的主要内容,如果未能解决你的问题,请参考以下文章

2019年7月6日 数组集合

数组

二维数组的查找

多维数组和矩阵----基础训练

多维数组与指针

10月24日上午PHP面向对象