Chessboard Traveling

版权声明:本文为博主原创文章,采用“署名-非商业性使用-禁止演绎 2.5 中国大陆”授权。欢迎转载,但请注明作者姓名和文章出处。 https://blog.csdn.net/njit_77/article/details/79747526

Challenge

Using the C# language, have the function  ChessboardTraveling(str) read  str which will be a string consisting of the location of a space on a standard 8x8 chess board with no pieces on the board along with another space on the chess board. The structure of  str will be the following: "(x y)(a b)" where (x y) represents the position you are currently on with x and y ranging from 1 to 8 and (a b) represents some other space on the chess board with a and b also ranging from 1 to 8 where a > x and b > y. Your program should determine how many ways there are of traveling from (x y) on the board to (a b) moving only up and to the right. For example: if  str is  (1 1)(2 2) then your program should output  2 because there are only two possible ways to travel from space (1 1) on a chessboard to space (2 2) while making only moves up and to the right. 

Hard challenges are worth  15 points and you are not timed for them.
Sample Test Cases

Input:"(1 1)(3 3)"

Output:6


Input:"(2 2)(4 3)"

Output:3

Chessboard Traveling算法

1、一个8 X 8的棋盘格;

2、输入一个字符串"(x y)(a b)",(x,y)是当前位置,(a,b)是移动终点位置;

3、每次只能移动(1,0)或者(0,1);

当初做这道题目的时候想了很久,没有思路,后来发现可以用数学的排列方法。

4、计算处x、y轴分别需要移动的次数A,B;

5、算法所求次数就是AB不同的排列了;

(A+B)!/A!/B!  (A+B)!->总共有的次数再除以A!,B!重合的次数

道理想透了,代码就可以写出来了,看起来要把数学知识捡起来了偷笑

        public static int ChessboardTraveling(string str)
        {
            int x = 0, y = 0, a = 0, b = 0;

            var test = str.Split(new char[] { '(', ')' }, StringSplitOptions.RemoveEmptyEntries);
            if (test.Length == 2)
            {
                char[] empty = new char[] { ' ' };
                var xy = test[0].Split(empty);
                if (xy.Length == 2)
                {
                    int.TryParse(xy[0], out x);
                    int.TryParse(xy[1], out y);
                }
                else
                {
                    return -1;
                }
                var ab = test[1].Split(empty);
                if (ab.Length == 2)
                {
                    int.TryParse(ab[0], out a);
                    int.TryParse(ab[1], out b);
                }
                else
                {
                    return -1;
                }
            }
            else
            {
                return -1;
            }

            int A = a - x;
            int B = b - y;
            int Length = (a + b) - (x + y);

            long sum1 = 1;
            while (Length > 0)
            {
                sum1 *= Length;
                Length--;
            }

            long sum2 = 1;
            while (A > 0)
            {
                sum2 *= (A--);
            }

            long sum3 = 1;
            while (B > 0)
            {
                sum3 *= (B--);
            }
            return (int)(sum1 / sum2 / sum3);
        }



猜你喜欢

转载自blog.csdn.net/njit_77/article/details/79747526
今日推荐