SWJTUOJ-2366 A chess Problem (easy)

来源:互联网 发布:形容网络喷子 编辑:程序博客网 时间:2024/06/08 14:35

A chess Problem (easy)

发布时间: 2017年5月7日 17:10   最后更新: 2017年5月7日 17:13   时间限制: 2000ms   内存限制: 128M

 In chinese chess, "马" is an interesting piece, when it is at position (x,y), it can move to(x2,y1),(x2,y+1),(x1,y2),(x1,y+2),(x+1,y2),(x+1,y+2),(x+2,y1),(x+2,y+1),

Now the problem is, when a “马” is at position (x,y), how many ways does it have for he to move to (x0,y0).

To simplify this problem, when our "马" is at (x,y), it can only move to (x+1,y+2) or (x+2,y+1)



The first line: the number of case T(T100)
Then T lines follow, each line contains four integers: x,y,x0,y0(0x,y,x0,y08) - the position the "马" at and the position it want's to go.

For each case, out put an integer ans one line -- the number of the ways to go from (x,y) to (x0,y0)

 复制
30 0 3 30 0 6 63 3 6 6
262

一道跳马题,求路径数,很容易想到DFS,数据不大,可以直接深搜。

当然在此基础上可以用DP,因为只有两种跳法,也可以用数学排列组合的方法,计算出终点和起点的横纵坐标之差的绝对值,可以算出(2,1)和(1,2)两种走法各自的步数,然后用排列组合解决,注意判断到不了的情况,输出0。

因为我用的DFS,剩下两种方法没写,就不贴了_(:з」∠)_。

#include "iostream"#include "cstdlib"#include "cstdio"#include "cmath"#include "cstring"using namespace std;int sx,sy,ex,ey;int ans;void dfs(int x,int y){    if(x>8 || y>8 || x<0 || y<0)        return ;    if(x==ex && y==ey)    {        ans++;        return ;    }    else    {        dfs(x+2,y+1);        dfs(x+1,y+2);    }}int main(){    int t;    cin >> t;    while(t--)    {        cin >> sx >> sy >> ex >> ey;        ans=0;        dfs(sx,sy);        cout << ans << endl;    }    return 0;}