Chessboard Traveling

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值