zoukankan      html  css  js  c++  java
  • The King's Walk(DP)

    Chess is a game in which two sides control pieces in an attempt to capture each other’s king. The pieces vary in mobility. At the beginning of a game the kings are rather vulnerable. They are less mobile than most other pieces and they tend to hide behind their pawns. Like in real life, as soon as both queens have left the game it is time for the kings to come into action. Because there is little threat left for the king, he can now move safely around the board. Indeed his mobility seems to be quite strong at this stage making him one of the more dangerous pieces. Your task is to measure the mobility of the king in the endgame.

    Consider a chess board of N×NN×N squares. The king is the only piece on the board. He starts at a given position and wants to go to another given position in the minimum number of moves. The king can move to any adjacent square in any orthogonal or diagonal direction.

    Input

    The input starts with a line containing an integer TT (1T1001≤T≤100), the number of test cases. Then for each test case:

    • One line with a single integer NN, the size of the board, where 2N50002≤N≤5000.

    • One line with four space-separated integers X1,Y1,X2,Y2X1,Y1,X2,Y2, such that 1X1,Y1,X2,Y2N1≤X1,Y1,X2,Y2≤N, where (X1,Y1X1,Y1) is the square on which the king starts and (X2,Y2X2,Y2) is the square the king wants to go to (different from his starting position).

    Output

    For each test case, output one line with a single integer: the number of ways by which the king can reach the destination square in the minimum number of moves. As this number can be very large, you must reduce your answer modulo 53180085318008.

    Sample Input 1Sample Output 1
    2
    3
    1 2 3 2
    8
    2 2 7 7
    
    3
    1

    题意:

    在一个n*n棋盘上,从(x1,y1)到(x2,y2),能直着走,斜着走,在最少步数的前提下,有几种方案?

    思路:

    x坐标y坐标分别作差,大的那个即为最少步数,

    假设大的是x,可以把这看做x层,每层在左右方向上可以不走,可以左走一步,可以右走一步,就酱

     1 #include <bits/stdc++.h>
     2 
     3 #define mod 5318008
     4 
     5 using namespace std;
     6 
     7 int a[5005][5005];
     8 
     9 int main()
    10 {
    11     int t, n, x1, x2, y1, y2, i, j, x, y;
    12     scanf("%d", &t);
    13     while(t--)
    14     {
    15         scanf("%d", &n);
    16         scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
    17         x = abs(x1-x2);
    18         y = abs(y1-y2);
    19         if(x < y)
    20         {
    21             swap(x1, y1);
    22             swap(x2, y2);
    23             swap(x, y);
    24         }
    25         memset(a, 0, sizeof(a));
    26       //  printf("x=%d y1=%d y2=%d
    ", x, y1, y2);
    27         a[0][y1] = 1;
    28         for(i=1;i<=x;i++)
    29         {
    30             for(j=1;j<=n;j++)
    31             {
    32                 a[i][j] += a[i-1][j];
    33                 if(j-1>=1) a[i][j] += a[i-1][j-1];
    34                 if(j+1<=n) a[i][j] += a[i-1][j+1];
    35                 a[i][j] %= mod;
    36             }
    37         }
    38         printf("%d
    ", a[x][y2]);
    39     }
    40     return 0;
    41 }
  • 相关阅读:
    redis单机安装以及简单redis集群搭建
    Linux中JDK安装教程
    微信公众号开发(一)
    easyui多图片上传+预览切换+支持IE8
    mybatis动态sql之foreach标签
    java List递归排序,传统方式和java8 Stream优化递归,无序的列表按照父级关系进行排序(两种排序类型)
    java钉钉通讯录同步
    java使用poi生成导出Excel(新)
    java 图片转base64字符串、base64字符串转图片
    Spring事务mysql不回滚:mysql引擎修改
  • 原文地址:https://www.cnblogs.com/0xiaoyu/p/12846858.html
Copyright © 2011-2022 走看看