现在的位置: 首页 > 综合 > 正文

青蛙过河 C# 求解

2012年12月22日 ⁄ 综合 ⁄ 共 8042字 ⁄ 字号 评论关闭

青蛙过河游戏和C#算法

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 
 10 namespace 青蛙过河
 11 {
 12     public partial class Form1 : Form
 13     {
 14         public Form1()
 15         {
 16             InitializeComponent();
 17         }
 18 
 19 
 20         private void button1_Click(object sender, EventArgs e)
 21         {
 22             //调用算法类
 23             FrogOverRiver FrogOverRiver = new FrogOverRiver();
 24             this.textBox1.Text = FrogOverRiver.getFrogJumpOrder();
 25         }
 26     }
 27 
 28     //青蛙过河类
 29     class FrogOverRiver
 30     {
 31 
 32         //获取青蛙的跳动顺序
 33         public string getFrogJumpOrder()
 34         {
 35             List<Frog> frogQueue = this.initializeFrogQueue();
 36             return this.frogJump(frogQueue, 3);

 37         }

 38 
 39 
 40         #region 辅助函数
 41 
 42         //初始化青蛙队列
 43         private List<Frog> initializeFrogQueue()
 44         {
 45             List<Frog> frogQueue = new List<Frog>();
 46             frogQueue.Add(new Frog(0"左1", frogDirection.向右, false));
 47             frogQueue.Add(new Frog(1"左2", frogDirection.向右, true));
 48             frogQueue.Add(new Frog(2"左3", frogDirection.向右, true));
 49             frogQueue.Add(new Frog(3));
 50             frogQueue.Add(new Frog(4"右1", frogDirection.向左, true));
 51             frogQueue.Add(new Frog(5"右2", frogDirection.向左, true));
 52             frogQueue.Add(new Frog(6"右3", frogDirection.向左, false));
 53             return frogQueue;
 54         }
 55 
 56         //当一个青蛙跳动后,形成一个新的队列
 57         private List<Frog> editFrogQueue(List<Frog> frogQueue, string frogName, int oldEmptyPositonID, int newEmptyPositonID)
 58         {
 59             List<Frog> newFrogQueue = new List<Frog>();
 60             foreach (Frog frog in frogQueue)
 61             {
 62                 Frog newFrog = new Frog(frog);
 63 
 64                 if (newFrog.isEmpty)
 65                     newFrog.position = newEmptyPositonID;
 66 
 67                 if (newFrog.frogName == frogName)
 68                 {
 69                     newFrog.position = oldEmptyPositonID;
 70                 }
 71 
 72                 newFrog.canJump = false;
 73                 if ((newEmptyPositonID - newFrog.position) > 0 && (newEmptyPositonID - newFrog.position) < 3 && newFrog.direction == frogDirection.向右)
 74                     newFrog.canJump = true;
 75 
 76                 if ((newFrog.position - newEmptyPositonID) > 0 && (newFrog.position - newEmptyPositonID) < 3 && newFrog.direction == frogDirection.向左)
 77                     newFrog.canJump = true;
 78 
 79                 newFrogQueue.Add(newFrog);
 80             }
 81             return newFrogQueue;
 82         }
 83 
 84         //是否已经完成位置对换,即前三个青蛙的位置都大于3
 85         private bool isComplete(List<Frog> frogQueue)
 86         {
 87             return (frogQueue[0].position > 3 && frogQueue[1].position > 3 && frogQueue[2].position > 3);
 88         }
 89 
 90         //是否还有可以跳动的青蛙,只要有可以跳动的,就没有达到最后的状态,但都不可以跳动了也不一定对换完了,这里只是控制递归
 91         private bool canFrogJump(List<Frog> frogQueue)
 92         {
 93             foreach (Frog frog in frogQueue)
 94             {
 95                 if (frog.canJump)
 96                     return true;
 97             }
 98             return false;
 99         } 
100         #endregion
101 
102         /// <summary>
103         /// 获取青蛙的跳动步骤
104         /// </summary>
105         /// <param name="frogQueue">当前青蛙队列</param>
106         /// <param name="emptyPositionId">空位置编号</param>
107         /// <returns></returns>
108         public string frogJump(List<Frog> frogQueue, int emptyPositionId)
109         {
110             string frogJumpInfo = "";
111 
112             foreach (Frog frog in frogQueue)
113             {
114                 //是空位置               
115                 if (frog.isEmpty)
116                     continue;
117                 if (!frog.canJump)
118                     continue;
119 
120                 frogJumpInfo = "青蛙" + frog.frogName + " " + frog.direction + "跳到" + (emptyPositionId + 1).ToString() + "\r\n";
121 
122                 int newPositionId = frog.position;
123                 List<Frog> newFrogQueue = this.editFrogQueue(frogQueue, frog.frogName, emptyPositionId, newPositionId);
124 
125                 //只要能继续跳就递归
126                 if (this.canFrogJump(newFrogQueue))
127                 {
128                     frogJumpInfo += this.frogJump(newFrogQueue, newPositionId);
129                 }
130                 else
131                 {
132                     if (this.isComplete(newFrogQueue))
133                     {
134                         frogJumpInfo = frogJumpInfo + "成功";
135                         break;
136                     }
137                 }
138 
139                 if (frogJumpInfo.Contains("成功"))
140                     break;
141             }
142             //循环结束
143             return frogJumpInfo;
144         }
145 
146     }
147 
148     #region 辅助类
149     enum frogDirection { 向左, 向右 };
150     //青蛙类
151     class Frog
152     {
153         public string frogName;//青蛙名称
154         public int position;    //青蛙位置
155         public frogDirection direction; //青蛙跳动的方向
156         public bool canJump;//是否可以跳
157         public bool isEmpty = false//是否是空格
158 
159         public Frog(int positon, string frogName, frogDirection direction, bool canJump)
160         {
161             this.position = positon;
162             this.frogName = frogName;
163             this.direction = direction;
164             this.canJump = canJump;
165         }
166 
167         public Frog(int positon)
168         {
169             this.frogName = "";
170             this.position = positon;
171             this.canJump = false;
172             this.isEmpty = true;
173         }
174         public Frog(Frog frog)
175         {
176             this.position = frog.position;
177             this.frogName = frog.frogName;
178             this.direction = frog.direction;
179             this.canJump = frog.canJump;
180             this.isEmpty = frog.isEmpty;
181         }
182     } 
183     #endregion
184 }
185 

186 

游戏和代码下载

网上找到的一个算法

  private void button1_Click(object sender, EventArgs e)

        {

            int[] begintArray = new int[] { 1, 1, 1, 0, 2, 2, 2 };

            int[] endArray = new int[] { 2, 2, 2, 0, 1, 1, 1 };

            //最后的解,最先移动的排在最后 

            List<string> jumpStep = new List<string>();

            jump(begintArray, endArray, jumpStep);

            //反转一下得到正序的解 

            jumpStep.Reverse();

            foreach (string step in jumpStep)

                Console.WriteLine(step);

        }

        private bool jump(int[] begintArray, int[] endArray, List<string> jumpStep)

        {

            if (compareArray(begintArray, endArray))

                return true;

            int zeroIndex = Array.IndexOf(begintArray, 0);

            int[] moveTo = zeroCanMoveTo(begintArray, zeroIndex);

            //穷举所有可以移动的位置

            foreach (int moveStep in moveTo)

            {

                int[] newBegin = (int[])begintArray.Clone();

                newBegin[zeroIndex] = newBegin[moveStep];

                newBegin[moveStep] = 0;

                if (jump(newBegin, endArray, jumpStep))

                {

                    string steplog = "";

                    for (int i = 0; i < newBegin.Length; i++)

                        steplog += newBegin[i].ToString();

                    jumpStep.Add(steplog);

                    return true;

                }

            }

            return false;

        }

        //判断0可以移动到那些格

        private int[] zeroCanMoveTo(int[] begintArray, int zeroIndex)

        {

            List<int> moveTo = new List<int>();

            if (zeroIndex >= 1 && begintArray[zeroIndex - 1] == 1)

                moveTo.Add(zeroIndex - 1);

            if (zeroIndex >= 2 && begintArray[zeroIndex - 2] == 1)

                moveTo.Add(zeroIndex - 2);

            if (zeroIndex < begintArray.Length - 1 && begintArray[zeroIndex + 1] == 2)

                moveTo.Add(zeroIndex + 1);

            if (zeroIndex < begintArray.Length - 2 && begintArray[zeroIndex + 2] == 2)

                moveTo.Add(zeroIndex + 2);

            return moveTo.ToArray();

        }

        //比较两个数组是否相等

        private bool compareArray(int[] begintArray, int[] endArray)

        {

            for (int i = 0; i < begintArray.Length; i++)

            {

                if (begintArray[i] != endArray[i])

                    return false;

            }

            return true;

        }

 

抱歉!评论已关闭.