棋盘型动态规划-codevs-1219骑士游历

来源:互联网 发布:朴正花网络剧 编辑:程序博客网 时间:2024/04/26 18:47

1219 骑士游历 1997年
时间限制: 1 s
空间限制: 128000 KB
题目等级 : 黄金 Gold
题解
题目描述 Description
设有一个n*m的棋盘(2≤n≤50,2≤m≤50),如下图,在棋盘上有一个中国象棋马。
这里写图片描述
规定:

1)马只能走日字

2)马只能向右跳

问给定起点x1,y1和终点x2,y2,求出马从x1,y1出发到x2,y2的合法路径条数。

输入描述 Input Description
第一行2个整数n和m

第二行4个整数x1,y1,x2,y2

输出描述 Output Description
输出方案数

样例输入 Sample Input
30 30

1 15 3 15

样例输出 Sample Output
2

数据范围及提示 Data Size & Hint
2<=n,m<=50

////  main.cpp//  1219 骑士游历////  Created by 袁子涵 on 15/6/18.//  Copyright (c) 2015年 袁子涵. All rights reserved.//#include <iostream>#include <string.h>using namespace std;int n,m;unsigned long long int s[51][51];int book[51][51];struct  place{    int x;    int y;}hourse,final;unsigned long long int dp(int x,int y){    if (x==hourse.x && y==hourse.y) {        return 1;    }    if(x<hourse.x || y<1 || x>final.x || y>m)        return 0;    if (book[x][y]) {        return s[x][y];    }    s[x][y]=dp(x-1,y-2)+dp(x-1,y+2)+dp(x-2,y-1)+dp(x-2,y+1);    book[x][y]=1;    return s[x][y];}int main(int argc, const char * argv[]) {    cin>>n>>m;    cin>>hourse.x>>hourse.y>>final.x>>final.y;    memset(s, 0, sizeof(s));    memset(book, 0, sizeof(book));    dp(final.x, final.y);    cout<<s[final.x][final.y]<<endl;    return 0;}
0 0