河内塔的递归计划 [英] recursive program for tower of hanoi

查看:85
本文介绍了河内塔的递归计划的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 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);

< span class =code-attribute>
}



Display();

< span class =code-attribute>
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 < span class =code-keyword>> peg)
{
foreach(peg in peg)
{
Console.Write(x + );
}
Console.WriteLine();
}
}
}
< / int > < / int < span class =code-keyword>> < / int > < / int > < / int > < / int > < / int > ; < / int > < /跨度> < / int > < / int >











需要帮助我这个代码如何我可以添加一些其他工作人员

的程序允许用户输入readline,

程序应显示用于解决的磁盘数量,

用户应该输入他们想要使用的磁盘数量,

它应该显示最终解决方案

解决方案

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



你基本上有三个常用选项:



  1. 仅限控制台的应用程序。

    如果您的编程经验有限,则此选项最佳。如果您只能输入一个数字,那么就足以证明您的解决方案了。所有你需要它来使用类 System.Console

    http://msdn.microsoft.com/en-us/library/system.console.aspx [ ^ ]。
  2. WPF。

    这个选项对于这种类型的游戏来说是最合适的,同时不需要太多的体力劳动。你甚至可以在一些矢量编辑器中创建游戏元素(磁盘,钉子等)(我强烈推荐使用开源InkScape,基于SVG),将图像导出为XAML,在资源字典中包含XAML并简单地使用图像进行显示他们。这样的图像将由 Canvas 类的实例表示,所有元素都应该放在另一个 Canvas 上,代表游戏场景。请参阅:

    http://en.wikipedia.org/wiki/Windows_Presentation_Foundation [< a href =http://en.wikipedia.org/wiki/Windows_Presentation_Foundationtarget =_ blanktitle =New Window> ^ ],

    http://msdn.microsoft.com/en-us/library/ms752059.aspx [ ^ ],

    http://en.wikipedia.org/wiki/XAML [ ^ ],

    http://download.microsoft.com/download/0/A/6/0A6F7755-9AF5-448B- 907D-13985ACCF53E /%5BMS-XAML-2009%5D.pdf [ ^ ],

    http://msdn.microsoft.com/en-us/library/system.windows.controls.canvas.aspx [ ^ ],

    http://en.wikipedia.org/wiki/Inkscape [ ^ ],

    http://www.inkscape.org/ [ ^ ]。

  3. System.Windows.Forms 与<结合code> System.Drawing .NET FCL的一部分。

    只有你对这个库有相当多的经验,但是没有WPF,那么这个选项是可以接受的会让你学习l ess(如果这真的是一个好处,请自己思考:-))。在我过去的答案中解释了基本思想:

    在面板上捕捉绘图 [ ^ ],

    什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...)) [ ^ ],

    如何加速我的vb.net应用程序? [ ^ ],

    mdi子表单之间的绘制线 [ ^ ]。



    参见:

    http://msdn.microsoft.com/en-us/library/system.windows.forms.aspx [ ^ ],

    http://msdn.microsoft.com/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:

  1. 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:
    http://msdn.microsoft.com/en-us/library/system.console.aspx[^].
  2. 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:
    http://en.wikipedia.org/wiki/Windows_Presentation_Foundation[^],
    http://msdn.microsoft.com/en-us/library/ms752059.aspx[^],
    http://en.wikipedia.org/wiki/XAML[^],
    http://download.microsoft.com/download/0/A/6/0A6F7755-9AF5-448B-907D-13985ACCF53E/%5BMS-XAML-2009%5D.pdf[^],
    http://msdn.microsoft.com/en-us/library/system.windows.controls.canvas.aspx[^],
    http://en.wikipedia.org/wiki/Inkscape[^],
    http://www.inkscape.org/[^].
  3. 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.net application?[^],
    Drawing Lines between mdi child forms[^].

    See also:
    http://msdn.microsoft.com/en-us/library/system.windows.forms.aspx[^],
    http://msdn.microsoft.com/en-us/library/system.drawing.aspx[^].



Good luck,

—SA


这篇关于河内塔的递归计划的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆