zoukankan      html  css  js  c++  java
  • 牛客假日团队赛11 H 过河卒(路径条数dp)

    H过河卒

    题目链接:https://ac.nowcoder.com/acm/contest/1077/H

    题目描述

    如图,A 点有一个过河卒,需要走到目标 B 点。卒行走规则:可以向下、或者向右。同时在棋盘上的任一点有一个对方的马(如上图的C点),该马所在的点和所有跳跃一步可达的点称为对方马的控制点。例如上图 C 点上的马可以控制 9 个点(图中的P1,P2 … P8 和 C)。卒不能通过对方马的控制点。
    棋盘用坐标表示,A 点(0,0)、B 点(n,m)(n,m 为不超过 20 的整数,并由键盘输入),同样马的位置坐标是需要给出的(约定: C<>A,同时C<>B)。现在要求你计算出卒从 A 点能够到达 B 点的路径的条数。

    输入描述:

    输入B点的坐标(n,m)以及对方马的坐标(X,Y){不用判错}

    输出描述:

    输出一个整数(路径的条数)。
    示例1

    输入

    复制
    6 6 3 2

    输出

    复制
    17
    思路:dp,将不能走的点为1,其余点为0

    //
    // Created by HJYL on 2019/8/21.
    //
    #include <iostream>
    #include <vector>
    #include <map>
    #include <string>
    #include <queue>
    #include <stack>
    #include <set>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <cstdlib>
    using namespace std;
    typedef long long ll;
    const int maxn=1e6+10;
    int main() {
       int n,m,x,y;
       scanf("%d%d%d%d",&x,&y,&n,&m);
       ll dp[30][30];
       ll a[30][30];
       memset(dp,0,sizeof(dp));
       memset(a,0,sizeof(a));
       if(n>=0&&m>=0)
           a[n][m]=1;
        if (n - 2 >= 0 && m - 1 >= 0) a[n - 2][m - 1] = 1;
        if (n - 2 >= 0 && m + 1 >= 0) a[n - 2][m + 1] = 1;
        if (n - 1 >= 0 && m - 2 >= 0) a[n - 1][m - 2] = 1;
        if (n - 1 >= 0 && m + 2 >= 0) a[n - 1][m + 2] = 1;
        if (n + 1 >= 0 && m - 2 >= 0) a[n + 1][m - 2] = 1;
        if (n + 1 >= 0 && m + 2 >= 0) a[n + 1][m + 2] = 1;
        if (n + 2 >= 0 && m - 1 >= 0) a[n + 2][m - 1] = 1;
        if (n + 2 >= 0 && m + 1 >= 0) a[n + 2][m + 1] = 1;
        for(ll i=0;i<=x;i++)
        {
            dp[i][0]=1;
            if(a[i][0]==1)
                break;
        }
        for(ll i=0;i<=y;i++)
        {
            dp[0][i]=1;
            if(a[0][i]==1)
                break;
        }
        for(ll i=1;i<=x;i++)
        {
            for(ll j=1;j<=y;j++)
            {
                if(a[i][j-1]==0)
                    dp[i][j]+=dp[i][j-1];
                if(a[i-1][j]==0)
                    dp[i][j]+=dp[i-1][j];
            }
        }
        printf("%lld
    ",dp[x][y]);
        return 0;
    }
    
    
  • 相关阅读:
    168. Excel Sheet Column Title
    171. Excel Sheet Column Number
    264. Ugly Number II java solutions
    152. Maximum Product Subarray java solutions
    309. Best Time to Buy and Sell Stock with Cooldown java solutions
    120. Triangle java solutions
    300. Longest Increasing Subsequence java solutions
    63. Unique Paths II java solutions
    221. Maximal Square java solutions
    279. Perfect Squares java solutions
  • 原文地址:https://www.cnblogs.com/Vampire6/p/11390007.html
Copyright © 2011-2022 走看看