-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path[课设]test.cpp
105 lines (95 loc) · 1.83 KB
/
[课设]test.cpp
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <stdio.h>
using namespace std;
int is_on[400][400];
int hourse(int board[400][400], int n, int m, int x, int y)
{
if (board[x][y] == -1)
return -1;
if (board[x][y] != 99999)
return board[x][y];
if (is_on[x][y] == 1)
return 99999;
is_on[x][y] = 1;
int distance[8];
int i;
for (i = 0; i < 8; i++)
distance[i] = 99999;
int is
if (0 <= x - 1 && 0 <= y - 2)
distance[0] = hourse(board, n, m, x - 1, y - 2);
if (0 <= x - 1 && y + 2 < m)
distance[1] = hourse(board, n, m, x - 1, y + 2);
if (0 <= x - 2 && 0 <= y - 1)
distance[2] = hourse(board, n, m, x - 2, y - 1);
if (0 <= x - 2 && y + 1 < m)
distance[3] = hourse(board, n, m, x - 2, y + 1);
if (x + 1 < n && 0 <= y - 2)
distance[4] = hourse(board, n, m, x + 1, y - 2);
if (x + 1 < n && y + 2 < m)
distance[5] = hourse(board, n, m, x + 1, y + 2);
if (x + 2 < n && 0 <= y - 1)
distance[6] = hourse(board, n, m, x + 2, y - 1);
if (x + 2 < n && y + 1 < m)
distance[7] = hourse(board, n, m, x + 2, y + 1);
for (i = 0; i < 8; i++)
{
if (distance[i] == -1)
distance[i] = 99999;
}
int min = distance[0];
for (i = 0; i < 8; i++)
{
if (distance[i] < min)
{
min = distance[i];
}
}
if (min < 99999)
board[x][y] = min + 1;
else
board[x][y] = -1;
for (i = 0; i < n; i++)
{
int j = 0;
for (j = 0; j < m; j++)
{
printf("%d ", board[i][j]);
}
printf("\n");
}
is_on[x][y] = 0;
return board[x][y];
}
int main()
{
int n = 4;
int m = 4;
int x = 2;
int y = 2;
int board[400][400];
int i, j;
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
board[i][j] = 99999;
}
}
board[x - 1][y - 1] = 0;
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
hourse(board, n, m, i, j);
}
}
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
printf("%d ", board[i][j]);
}
printf("\n");
}
}