zoukankan      html  css  js  c++  java
  • 哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级)(F)小乐乐下象棋

    小乐乐下象棋

    链接:https://ac.nowcoder.com/acm/contest/301/F
    来源:牛客网

    题目描述

    小乐乐一天天就知道玩,这一天又想玩象棋。
    我们都知道马走日。
    现在给定一个棋盘,大小是n*m,把棋盘放在第一象限,棋盘的左下角是(0,0),右上角是(n - 1, m - 1);
    小乐乐想知道,一个马从左下角(0, 0)开始,走了k步之后,刚好走到右上角(n - 1, m - 1)的方案数。

    输入描述:

    输入:多组样例输入,每组一行,三个整数n, m, k(1 <= n, m, k <= 200),如题目所示。

    输出描述:

    输出:输出答案 mod 1000000007
    示例1

    输入

    4 4 2

    输出

    2
    题解:emmmm...可以说是一个很经典的dp or 暴搜了 为啥还是不会做呢。。太菜了太菜了
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<queue>
     4 #include<cstring>
     5 #include<algorithm>
     6 #define inf 0x3f3f3f3f
     7 const int mod = 1e9+7;
     8 using namespace std;
     9 int n,m,k;
    10 int dir[8][2]={{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2},{1,-2},{2,-1}};
    11 int dp[210][210][210];
    12 int main()
    13 {
    14     while(~scanf("%d%d%d",&n,&m,&k))
    15     {
    16         memset(dp,0,sizeof(dp));
    17         dp[0][0][0]=1;
    18         for(int t=1;t<=k;t++)
    19         {
    20             for(int i=0;i<=n-1;i++)
    21             {
    22                 for(int j=0;j<=m-1;j++)
    23                 {
    24                     if(i+2>=0&&j+1>=0&&i+2<n&&j+1<m)
    25                         dp[i][j][t]=(dp[i][j][t]+dp[i+2][j+1][t-1])%mod;
    26                     if(i+1>=0&&j+2>=0&&i+1<n&&j+2<m)
    27                         dp[i][j][t]=(dp[i][j][t]+dp[i+1][j+2][t-1])%mod;
    28                     if(i-1>=0&&j+2>=0&&i-1<n&&j+2<m)
    29                         dp[i][j][t]=(dp[i][j][t]+dp[i-1][j+2][t-1])%mod;
    30                     if(i-2>=0&&j+1>=0&&i-2<n&&j+1<m)
    31                         dp[i][j][t]=(dp[i][j][t]+dp[i-2][j+1][t-1])%mod;
    32                     if(i-2>=0&&j-1>=0&&i-2<n&&j-1<m)
    33                         dp[i][j][t]=(dp[i][j][t]+dp[i-2][j-1][t-1])%mod;
    34                     if(i+1>=0&&j-2>=0&&i+1<n&&j-2<m)
    35                         dp[i][j][t]=(dp[i][j][t]+dp[i+1][j-2][t-1])%mod;
    36                     if(i+2>=0&&j-1>=0&&i+2<n&&j-1<m)
    37                         dp[i][j][t]=(dp[i][j][t]+dp[i+2][j-1][t-1])%mod;
    38                     if(i-1>=0&&j-2>=0&&i-1<n&&j-2<m)
    39                         dp[i][j][t]=(dp[i][j][t]+dp[i-1][j-2][t-1])%mod;
    40                 }
    41             }
    42         }
    43         printf("%d
    ",dp[n-1][m-1][k]);
    44     }
    45 }

     有大佬用记忆化搜索写的,真的tql,学习了学习了,感觉和大佬的差距真的不小,一定要好好加油啊。。

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<algorithm>
     5 #define inf 0x3f3f3f3f
     6 using namespace std;
     7 const int maxn=2e5+10;
     8 typedef long long ll;
     9 ll dp[210][210][210];
    10 const ll mod=1000000007;
    11 int dir[8][2]={{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2},{1,-2},{2,-1}};
    12 int n,m,t;
    13 ll dfs(int x,int y,int step)
    14 {
    15     if(x==n&&y==m&&step==t)
    16         return dp[n][m][step]=1;
    17     if(step>=t)
    18         return 0;
    19     if(dp[x][y][step]!=-1)
    20         return dp[x][y][step];
    21     ll sum=0;
    22     for(int i=0;i<8;i++)
    23     {
    24         int tx=x+dir[i][0];
    25         int ty=y+dir[i][1];
    26         if(tx>=1&&tx<=n&&ty>=1&&ty<=m)
    27         {
    28             sum+=dfs(tx,ty,step+1);
    29         //    cout<<sum<<endl;
    30             sum%=mod;
    31         }
    32     }
    33 return    dp[x][y][step]=sum;
    34      
    35 }
    36 int main()
    37 {
    38     while(~scanf("%d%d%d",&n,&m,&t))
    39     {
    40         memset(dp,-1,sizeof(dp));
    41         printf("%lld
    ",dfs(1,1,0)%mod);
    42     }
    43 }
  • 相关阅读:
    1094. Car Pooling
    121. Best Time to Buy and Sell Stock
    58. Length of Last Word
    510. Inorder Successor in BST II
    198. House Robber
    57. Insert Interval
    15. 3Sum java solutions
    79. Word Search java solutions
    80. Remove Duplicates from Sorted Array II java solutions
    34. Search for a Range java solutions
  • 原文地址:https://www.cnblogs.com/1013star/p/10055502.html
Copyright © 2011-2022 走看看