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

我的马的遍历代码[Teaks & xgluxv]

2012年03月26日 ⁄ 综合 ⁄ 共 2276字 ⁄ 字号 评论关闭
今天在园子首页上看到一篇 马的遍历 的文章 用来比较了一下c++和c#的代码运行速度的问题。本来这两种语言没有什么可比性,但后边看到文章里提到的出结果的时间 都超出了我的想像。因此用vc++2005和vc#2005分别写了一下相同的算法,各位网友可以把代码下载下去 自行运行比较一下。源代码工程文件
我这里把算法代码列出一下 以c#版本为例:
 1    struct CSPoint
 2    {
 3        public int x;
 4        public int y;
 5    }

 6
 7    class othercsHorse
 8    {
 9        int[] DeltaX =-2-11221-1-2 };
10        int[] DeltaY =1221-1-2-2-1 };
11        int[,] Game = new int[1212];
12        CSPoint[] csPoint = new CSPoint[64];
13        int step;
14        int ResultCount;
15
16        public othercsHorse()
17        {
18            for (int i = 0; i < 12; i++)
19            {
20                Game[i, 0= -1;
21                Game[i, 11= -1;
22                Game[0, i] = -1;
23                Game[11, i] = -1;
24                Game[i, 1= -1;
25                Game[i, 10= -1;
26                Game[1, i] = -1;
27                Game[10, i] = -1;
28            }

29            step=0;
30            csPoint[step].x=2;
31            csPoint[step].y=2;
32            Game[csPoint[step].x, csPoint[step].y] = 1;
33        }

34
35        public void OutputResult()
36        {
37            ResultCount++;
38            Console.WriteLine("Result:{0}", ResultCount);
39            for (int i = 2; i < 10; i++)
40            {
41                for (int j = 2; j < 10; j++)
42                {
43                    Console.Write("{0}{1} ", (Game[i, j] < 10)?" ":"", Game[i, j].ToString());
44                }

45
46                Console.WriteLine();
47            }

48            Console.WriteLine();
49        }

50
51        public void Start()
52        {
53            int NextX, NextY;
54            for (int i = 0; i < 8; i++)
55            {
56                NextX = csPoint[step].x + DeltaX[i];
57                NextY = csPoint[step].y + DeltaY[i];
58                if (Game[NextX, NextY] == 0)
59                {
60                    Game[NextX, NextY] = ++step + 1;
61                    csPoint[step].x = NextX;
62                    csPoint[step].y = NextY;
63                    if (step == 63)
64                    {
65                        OutputResult();
66                    }

67
68                    Start();
69                }

70            }

71
72            Game[csPoint[step].x,csPoint[step].y] = 0;
73            --step;
74        }

75
76    }

77
【上篇】
【下篇】

抱歉!评论已关闭.