forked from jyx-fyh/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhorseWalk.cpp
More file actions
63 lines (61 loc) · 1.94 KB
/
horseWalk.cpp
File metadata and controls
63 lines (61 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
// Created by ButcherX on 23-11-28.
//
#include<vector>
#include<iostream>
using std::vector;
//y轴0~9,x轴0~8
//暴力递归,和robotWalk完全类似
int process(int x, int y, int rest, int a, int b)
{
if (x < 0 || x > 9 || y < 0 || y > 8)
return 0;
if (rest == 0)
return (x == a && y == b) ? 1 : 0;
int ways = 0;
ways += process(x + 2, y + 1, rest - 1, a, b);
ways += process(x + 1, y + 2, rest - 1, a, b);
ways += process(x - 1, y + 2, rest - 1, a, b);
ways += process(x - 2, y + 1, rest - 1, a, b);
ways += process(x - 2, y - 1, rest - 1, a, b);
ways += process(x - 1, y - 2, rest - 1, a, b);
ways += process(x + 1, y - 2, rest - 1, a, b);
ways += process(x + 2, y - 1, rest - 1, a, b);
return ways;
}
//====================================================================
//动态规划
int pick(vector<vector<vector<int>>> &dp, int x, int y, int rest)
{
if (x < 0 || x > 9 || y < 0 || y > 8)
return 0;
return dp[x][y][rest];
}
int dp(int a, int b, int k) {
vector<vector<vector<int>>> dp(10, vector<vector<int>>(9, vector<int>(k + 1, 0)));//注意是k+1
dp[a][b][0] = 1;
for (int rest = 1; rest <= k; rest++) {
for (int x = 0; x < 10; x++) {
for (int y = 0; y < 9; y++) {
int ways = pick(dp, x + 2, y + 1, rest - 1);
ways += pick(dp, x + 1, y + 2, rest - 1);
ways += pick(dp, x - 1, y + 2, rest - 1);
ways += pick(dp, x - 2, y + 1, rest - 1);
ways += pick(dp, x - 2, y - 1, rest - 1);
ways += pick(dp, x - 1, y - 2, rest - 1);
ways += pick(dp, x + 1, y - 2, rest - 1);
ways += pick(dp, x + 2, y - 1, rest - 1);
dp[x][y][rest] = ways;
}
}
}
return dp[0][0][k];
}
int horseWalk(int x, int y, int steps)
{
return dp(x, y, steps);
}
int main()
{
std::cout << horseWalk(4, 5, 5);
}