# 每日一道算法题 - KaprekarsConstant(hard-2)

## 规则

Using the JavaScript 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.

``````function ChessboardTraveling(str) {

// code goes here
return str;
}
``````

## 测试用例

``````Input:"(1 1)(3 3)"
Output:6

Input:"(1 1)(2 2)"
Output:2

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

## my code

``````function ChessboardTraveling(str) {
var strArr = str.match(/([0-9]+\s+[0-9]+)/g)
var minArr = strArr[0].split(' ')
var maxArr = strArr[1].split(' ')
var xDiff = maxArr[0] - minArr[0]
var yDiff = maxArr[1] - minArr[1]

return Steps(xDiff, yDiff);
}

function Steps(x, y) {
if (x < 0 || y < 0)
return 0;
if (x == 0 && y == 1)
return 1;
if (x == 1 && y == 0)
return 1;

return Steps(x - 1, y) + Steps(x, y - 1)
}

console.log(ChessboardTraveling("(1 1)(3 3)"));
``````

## 思路

1. 8*8在本题中只做了数值的大小限制，无其他作用
2. 把最小点（如（2 2））作为方格的最左下角，最大点（如 （4 3））作为方格的右上角，构成一个3*2的方格，实质上就是求从方格最左下方到方格最右上方有多少条路径。
3. 使用递归函数去解决，需要清楚判断的临界点，比如`（x === 0 && y === 1）``（x === 1 && y === 0）`时，只有一种选择。

2014年最流行前端开发框架对比评测

2015-11-12
2015年3月国内浏览器市场份额概括，chrome占32.97

2015-11-12
CSS2.0帮助文档（参考手册）chm下载

2015-11-12
ASP.NET 2.0 AJAX应用程序设计
ASP.NET Aiax技术是一种实现异步（Asynchronous）网络应用的技术，它被整合在ASP.NET 2.0之中，是As P.NET的一种扩展技术。通过ASENETAjax技术，开发人员或程序员可以将Web服务器控件和客户端脚本结...
2015-11-14
HTML5游戏2015年的开发趋势

2015-11-12
2015 Web 2.0和AJAX如何做好优化
2015如何让做好web2.0和ajax的优化？JavaScript中文网总结当下提出以下四大优化意见，旨在帮助W前端开发人员有效利用APM解决上述问题。 随着Web应用程序速度与效率快速增长，网站已经成为企业与其客户进行交互的第一途径——...
2015-11-12
2015年将会有大量基于HTML5和JS的WEB应用

2015-11-12
HTML5移动应用开发的12大特性