-
Notifications
You must be signed in to change notification settings - Fork 0
/
huawei1.cpp
69 lines (68 loc) · 1.56 KB
/
huawei1.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
#include <iostream>
using namespace std;
#include <vector>
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
int sub(int bx, int by, int ex, int ey, int val, int &minval, int &num, vector<vector<int>> &map);
int main()
{
int m = 0;
int n = 0;
cin >> m;
cin >> n;
int beginx = 0;
int beginy = 0;
cin >> beginx;
cin >> beginy;
int endx = 0;
int endy = 0;
cin >> endx;
cin >> endy;
int num = 0;
cin >> num;
vector<vector<int>> map(m, vector<int>(n, 0));
for (int i = 0; i < num; i++)
{
int x = 0;
int y = 0;
cin >> x;
cin >> y;
map[x][y] = 1;
}
int ans = 0;
int minval = 100000;
sub(beginx, beginy, endx, endy, 0, minval, ans, map);
cout << ans << " " << minval;
getchar();
getchar();
return 0;
}
int sub(int bx, int by, int ex, int ey, int val, int &minval, int &num, vector<vector<int>> &map)
{
if (bx == ex && by == ey)
{
if (val < -minval)
{
minval = val;
num = 1;
}
else if (val == minval)
num++;
return 0;
}
for (int i = 0; i < 4; i++)
{
int nx = bx + dx[i];
int ny = by + dy[i];
// cout << nx << " " << ny << endl;
if (nx >= 0 && nx < map.size() && ny >= 0 && ny < map[0].size() && map[nx][ny] != 1)
{
if (val + 1 > minval)
continue;
map[nx][ny] = 1;
sub(nx, ny, ex, ey, val + 1, minval, num, map);
map[nx][ny] = 0;
}
}
return 0;
}