河内塔的递归计划

编程入门 行业动态 更新时间:2024-10-23 10:28:59
本文介绍了河内塔的递归计划的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

view plainprint? 使用System; 使用System.Collections.Generic; 使用System.Linq; 使用System.Text; 命名空间TowersOfHanoiCs { class program { static List < int > peg1; 静态列表< int > peg2; 静态列表< int > peg3; static void Main(string [] args) { peg1 = new List < int > (); peg2 =新列表< int > ; (); peg3 =新列表< int > ; (); for(int i = 1; i < 8; i ++) { peg1.Add(i); } Display(); MoveDisk(7, peg1, peg2, peg3); Console.ReadLine(); } 静态 void MoveDisk(int disk, List< int > 来源,列表< int > 目的地,列表< int > temp) { int position = source.IndexOf(disk); if(position == 0) { source.Remove(disk); destination.Insert(0,disk); Display(); } 其他 { int nextDisk = source [position - 1]; MoveDisk(nextDisk,source,temp,destination); MoveDisk(磁盘,源,目标,临时); MoveDisk(nextDisk,temp,destination,source); } } static void Display() { DisplayPeg(peg1); DisplayPeg(peg2); DisplayPeg(peg3); Console.WriteLine(----); } static void DisplayPeg(List < int > peg) { foreach(peg in peg) { Console.Write(x + ); } Console.WriteLine(); } } } < / int > < / int > < / int > < / int > < / int > < / int > < / int > ; < / int > < / int > < / int >

需要帮助我这个代码如何我可以添加一些其他工作人员 的程序允许用户输入readline, 程序应显示用于解决的磁盘数量, 用户应该输入他们想要使用的磁盘数量, 它应该显示最终解决方案

解决方案

请参阅对问题的评论 - 由ryanb31和我的,他们应该解释你为什么在这个论坛给你指示很难有效并且同时适合论坛格式。所以,我只能给你一般的想法。 你基本上有三个常用选项:

  • 仅限控制台的应用程序。 如果您的编程经验有限,则此选项最佳。如果您只能输入一个数字,那么就足以证明您的解决方案了。所有你需要它来使用类 System.Console : msdn.microsoft/en-us/library/system.console.aspx [ ^ ]。
  • WPF。 这个选项对于这种类型的游戏来说是最合适的,同时不需要太多的体力劳动。你甚至可以在一些矢量编辑器中创建游戏元素(磁盘,钉子等)(我强烈推荐使用开源InkScape,基于SVG),将图像导出为XAML,在资源字典中包含XAML并简单地使用图像进行显示他们。这样的图像将由 Canvas 类的实例表示,所有元素都应该放在另一个 Canvas 上,代表游戏场景。请参阅: en.wikipedia/wiki/Windows_Presentation_Foundation [ ^ ], msdn.microsoft/en-us/library/ms752059.aspx [ ^ ], en.wikipedia/wiki/XAML [ ^ ], download.microsoft/download/0/A/6/0A6F7755-9AF5-448B- 907D-13985ACCF53E /%5BMS-XAML-2009%5D.pdf [ ^ ], msdn.microsoft/en-us/library/system.windows.controls.canvas.aspx [ ^ ], en.wikipedia/wiki/Inkscape [ ^ ], www.inkscape/ [ ^ ]。
  • System.Windows.Forms 与 System.Drawing .NET FCL的一部分。 只有你对这个库有相当多的经验,但是没有WPF,那么这个选项是可以接受的会让你学习l ess(如果这真的是一个好处,请自己思考:-))。在我过去的答案中解释了基本思想: 在面板上捕捉绘图 [ ^ ], 什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...)) [ ^ ], 如何加速我的vb应用程序? [ ^ ], mdi子表单之间的绘制线 [ ^ ]。 参见: msdn.microsoft/en-us/library/system.windows.forms.aspx [ ^ ], msdn.microsoft/en-us/library/system。 drawing.aspx [ ^ ]。
  • 祝你好运,

    -SA

    view plainprint? using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TowersOfHanoiCs { class Program { static List<int> peg1; static List<int> peg2; static List<int> peg3; static void Main(string[] args) { peg1 = new List<int>(); peg2 = new List<int>(); peg3 = new List<int>(); for (int i = 1; i < 8; i++) { peg1.Add(i); } Display(); MoveDisk(7, peg1, peg2, peg3); Console.ReadLine(); } static void MoveDisk(int disk, List<int> source, List<int> destination, List<int> temp) { int position = source.IndexOf(disk); if (position == 0) { source.Remove(disk); destination.Insert(0, disk); Display(); } else { int nextDisk = source[position - 1]; MoveDisk(nextDisk, source, temp, destination); MoveDisk(disk, source, destination, temp); MoveDisk(nextDisk, temp, destination, source); } } static void Display() { DisplayPeg(peg1); DisplayPeg(peg2); DisplayPeg(peg3); Console.WriteLine("———-"); } static void DisplayPeg(List<int> peg) { foreach (int x in peg) { Console.Write(x + " "); } Console.WriteLine(); } } } </int></int></int></int></int></int></int></int></int></int>

    need help with this code on how i can add some other staff for the program to allow the readline input from the user, the program should show the number of disk used to solve, user should input the number of disk they want to use, and it should show the final solution

    解决方案

    Please see the comments to the question — by ryanb31 and mine, they should explain you why giving you the directions in this forum can hardly be efficient and fit the forum format at the same time. So, I can give you only the very general idea. You basically have three commonly used options:

  • Console-only application. This option is the best if you have limited programming experience. If you can input only one number, it will be enough to demonstrate your solution. All you need it to use the class System.Console: msdn.microsoft/en-us/library/system.console.aspx[^].
  • WPF. This option is the most adequate for this very kind of game and at the same time does not require much manual labor. You can even create game elements (disks, pegs, etc) in some vector editor (I would highly recommend open-source InkScape, bases on SVG), export images as XAML, include XAMLs in a resource dictionary and simply use the images for showing them. Such image will be represented by the instance of the Canvas class, and all elements should be placed on another Canvas representing the game scene. Please see: en.wikipedia/wiki/Windows_Presentation_Foundation[^], msdn.microsoft/en-us/library/ms752059.aspx[^], en.wikipedia/wiki/XAML[^], download.microsoft/download/0/A/6/0A6F7755-9AF5-448B-907D-13985ACCF53E/%5BMS-XAML-2009%5D.pdf[^], msdn.microsoft/en-us/library/system.windows.controls.canvas.aspx[^], en.wikipedia/wiki/Inkscape[^], www.inkscape/[^].
  • System.Windows.Forms combined with System.Drawing parts of .NET FCL. This option is only acceptable if you have considerable experience with this library, but not with WPF, then it will allow you to learn less (bit think by yourself if this is really a benefit :-)). The basic ideas are explained in my past answers: capture the drawing on a panel[^], What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^], How to speed up my vb application?[^], Drawing Lines between mdi child forms[^]. See also: msdn.microsoft/en-us/library/system.windows.forms.aspx[^], msdn.microsoft/en-us/library/system.drawing.aspx[^].
  • Good luck,

    —SA

    更多推荐

    河内塔的递归计划

    本文发布于:2023-11-30 03:11:05,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1648528.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:递归   河内   计划

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!