棋盘型动态规划 之 CODE[VS] 1219 骑士游历 1997年

/*
dp[i][j] := 从出发点(x1, y1)到点(i, j)的合法路径条数
初始化:
	dp[][] = {0}
	dp[x1][y1] = 1
状态方程:
	根据马跳的位置推出:
	dp[i][j] = dp[i-1][j+2] + dp[i-1][j-2] + dp[i-2][j+1] + dp[i-2][j-1]
答案:
dp[x2][y2]
*/
 1 #define _CRTDBG_MAP_ALLOC
 2 #include <stdlib.h>
 3 #include <crtdbg.h>
 4 #define _CRT_SECURE_NO_WARNINGS
 5 #define HOME
 6 
 7 #include <iostream>
 8 #include <cstdlib>
 9 #include <cstdio>
10 #include <cstddef>
11 #include <iterator>
12 #include <algorithm>
13 #include <string>
14 #include <locale>
15 #include <cmath>
16 #include <vector>
17 #include <cstring>
18 using namespace std;
19 const int INF = 0x3f3f3f3f;
20 const int MaxN = 30;
21 const int Max = 55;
22 
23 int n, m;
24 int a, b, c, d;
25 int dp[Max][Max];
26 int step[4][2] = { {-1, 2}, {-1, -2}, {-2, 1}, {-2, -1} };
27 
28 bool Check(int x, int y)
29 {
30     if ((x<1) || (x>n) || (y<1) || (y>m))
31     {
32         return false;
33     }
34     return true;
35 }
36 
37 void Solve()
38 {
39     dp[a][b] = 1;
40     int x, y;
41     for (int i = 1; i <= n; ++i)
42     {
43         for (int j = 1; j <= m; ++j)
44         {
45             for (int p = 0; p < 4; ++p)
46             {
47                 x = i + step[p][0];
48                 y = j + step[p][1];
49                 if (Check(x, y))
50                 {
51                     dp[i][j] += dp[x][y];
52                 }
53             }
54         }
55     }
56 
57     /*for (int i = 1; i <= n; ++i)
58     {
59         for (int j = 1; j <= m; ++j)
60         {
61             cout << "(" << i << "," << j << ")" << " : " << dp[i][j] << endl;
62         }
63     }*/
64     cout << dp[c][d] << endl;
65 }
66 
67 int main() 
68 {
69 #ifdef HOME
70     freopen("in", "r", stdin);
71     //freopen("out", "w", stdout);
72 #endif
73 
74     cin >> n >> m;
75     cin >> a >> b >> c >> d;
76     memset(dp, 0, sizeof(dp));
77     Solve();
78 
79 
80 #ifdef HOME
81     cerr << "Time elapsed: " << clock() / CLOCKS_PER_SEC << " ms" << endl;
82     _CrtDumpMemoryLeaks();
83     system("pause");
84 #endif
85     return 0;
86 }

 

 
posted @ 2015-11-09 15:55  JmingS  阅读(323)  评论(0编辑  收藏  举报