vijos P1121 马拦过河卒

题意:中国象棋中,一只兵在不被马吃的情况下有多少种方法走到终点。

链接:https://vijos.org/p/1121

思路:动态规划,注意不通路就行,在转化为数塔问题,状态转移方程dp[i][j]=dp[i-1][j]+dp[i][j-1]

注意点:不通点 的判断(包括自身)

以下为AC代码:

评测状态Accepted
题目P1121 马拦过河卒
递交时间2014-11-06 22:34:50
代码语言C++
评测机上海红茶馆
消耗时间15 ms
消耗内存568 KiB
评测时间2014-11-06 22:34:51
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <deque>
#include <list>
#include <cctype>
#include <algorithm>
#include <climits>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

int dir[8][2] = { 1, 2, 1, -2, -1, 2, -1, -2, 2, 1, 2, -1, -2, 1, -2, -1 };
bool map[20][20];
int dp[20][20];
int main()
{
    int m, n;
    int tar_x, tar_y;
    while ( cin >> tar_x >> tar_y >> m >> n )
    {
        tar_x++;
        tar_y++;
        m++;
        n++;
        map[m][n] = true;
        memset ( dp, 0, sizeof ( dp ) );
        for ( int i = 0; i < 8; i ++ )
        {
            if ( m + dir[i][0] >= 0 && n + dir[i][1] >= 0 )
            {
                map[m + dir[i][0]][n + dir[i][1]] = true;
            }
        }
        dp[1][1] = 1;
        for ( int i = 1; i <= tar_x; i ++ )
        {
            for ( int j = 1; j <= tar_y; j ++ )
            {
                if ( ! map[i][j] )
                {
                    dp[i][j] += ( dp[i-1][j] + dp[i][j-1] );
                }
            }
        }
        cout << dp[tar_x][tar_y] << endl;
    }
    return 0;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值