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\n", 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\n", a[x][y2]);
39     }
40     return 0;
41 }

 

posted @ 2020-05-08 00:18  Xxiaoyu  阅读(210)  评论(0编辑  收藏  举报