Skip to content

Commit 3ddf254

Browse files
committed
重做了C适配
1 parent e972cf3 commit 3ddf254

File tree

1 file changed

+114
-30
lines changed

1 file changed

+114
-30
lines changed

src/algorithm_module/c_adapt.cpp

+114-30
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,120 @@
11
#include "algorithm_module.h"
2-
#define C_PUBLIC extern "C" __declspec(dllexport) //CÓïÑԵĵ¼³ö±ê¼Ç
3-
namespace GraphAlgorithm
2+
#define C_PUBLIC extern "C" __declspec(dllexport) //C export
3+
namespace Algorithm
44
{
5-
typedef int (*_FirstEdgeAdapter)(int idx_v);
6-
typedef int (*_NextEdgeAdapter)(int idx_v, int idx_e);
7-
typedef int (*_EdgeToAdapter)(int idx_v, int idx_e);
8-
PathType bfsResult;
9-
std::vector<int> pointVector, edgeVector;
10-
C_PUBLIC void RunBfs(int idx_start_v, int idx_dest_v,
11-
_FirstEdgeAdapter firstOf,
12-
_NextEdgeAdapter nextOf,
13-
_EdgeToAdapter destOf)
5+
namespace Graph
146
{
15-
bfsResult = bfs(idx_start_v, idx_dest_v, FirstEdgeAdapter(firstOf), NextEdgeAdapter(nextOf), EdgeToAdapter(destOf));
16-
pointVector = std::vector<int>(bfsResult.first.begin(), bfsResult.first.end());
17-
edgeVector = std::vector<int>(bfsResult.second.begin(), bfsResult.second.end());
7+
/*typedef int(*_FirstEdgeAdapter)(int idx_v);
8+
typedef int(*_NextEdgeAdapter)(int idx_v, int idx_e);
9+
typedef int(*_EdgeToAdapter)(int idx_v, int idx_e);
10+
PathType bfsResult;
11+
std::vector<int> pointVector, edgeVector;
12+
C_PUBLIC void RunBfs(int idx_start_v, int idx_dest_v,
13+
_FirstEdgeAdapter firstOf,
14+
_NextEdgeAdapter nextOf,
15+
_EdgeToAdapter destOf)
16+
{
17+
bfsResult = bfs(idx_start_v, idx_dest_v, FirstEdgeAdapter(firstOf), NextEdgeAdapter(nextOf), EdgeToAdapter(destOf));
18+
pointVector = std::vector<int>(bfsResult.first.begin(), bfsResult.first.end());
19+
edgeVector = std::vector<int>(bfsResult.second.begin(), bfsResult.second.end());
1820
19-
};
20-
C_PUBLIC int* GetPointListPointer()
21-
{
22-
return &pointVector[0];
23-
}
24-
C_PUBLIC int GetPointListLength()
25-
{
26-
return pointVector.size();
27-
}
28-
C_PUBLIC int* GetEdgeListPointer()
29-
{
30-
return &edgeVector[0];
31-
}
32-
C_PUBLIC int GetEdgeListLength()
33-
{
34-
return edgeVector.size();
21+
};
22+
C_PUBLIC int* GetPointListPointer()
23+
{
24+
return &pointVector[0];
25+
}
26+
C_PUBLIC int GetPointListLength()
27+
{
28+
return pointVector.size();
29+
}
30+
C_PUBLIC int* GetEdgeListPointer()
31+
{
32+
return &edgeVector[0];
33+
}
34+
C_PUBLIC int GetEdgeListLength()
35+
{
36+
return edgeVector.size();
37+
}*/
38+
39+
const int maxn = 30;
40+
int a, b, t;
41+
char*** s;
42+
43+
struct Node
44+
{
45+
int r, c, h;
46+
Node(int r = 0, int c = 0, int h = 0) : r(r), c(c), h(h)
47+
{
48+
}
49+
};
50+
51+
inline bool inside(const Node& v)
52+
{
53+
return
54+
v.r >= 0 && v.r < a &&
55+
v.c >= 0 && v.c < b &&
56+
v.h >= 0 && v.h < t &&
57+
s[v.h][v.r][v.c] != '#';
58+
}
59+
inline int encoder(int h, int r, int c)
60+
{
61+
return h * (a*b) + r * b + c;
62+
}
63+
64+
inline int walk(int v, int i)
65+
{
66+
const static int dr[] = { -1, 0, 1, 0, 0, 0 };
67+
const static int dc[] = { 0, -1, 0, 1, 0, 0 };
68+
const static int dh[] = { 0, 0, 0, 0, 1, -1 };
69+
const Node next(v % (a*b) / b + dr[i], v % b + dc[i], v / (a*b) + dh[i]);
70+
if (inside(next)) return encoder(next.h, next.r, next.c);
71+
else return NIL;
72+
}
73+
74+
NextEdgeAdapter nextOf =
75+
[] (int v, int e) -> int
76+
{
77+
for (int e1 = e + 1; e1 < 6; e1++)
78+
{
79+
if (walk(v, e1) != NIL)
80+
return e1;
81+
}
82+
return NIL;
83+
};
84+
85+
GraphAdapter graph = {
86+
std::bind2nd(nextOf,-1),
87+
nextOf,
88+
walk
89+
};
90+
91+
C_PUBLIC int EscapeMaze(int floors, int height, int width, char*** mazeData)
92+
{
93+
t = floors;
94+
a = height;
95+
b = width;
96+
s = mazeData;
97+
int start, dest;
98+
for (int i = 0; i < t; i++)
99+
{
100+
for (int j = 0; j < a; j++)
101+
{
102+
scanf_s("%s", s[i][j], 1024);
103+
for (int k = 0; k < b; k++)
104+
{
105+
if (s[i][j][k] == 'S')
106+
{
107+
start = encoder(i, j, k);
108+
}
109+
else if (s[i][j][k] == 'E')
110+
{
111+
dest = encoder(i, j, k);
112+
}
113+
}
114+
}
115+
}
116+
117+
return bfsStep(start, dest, graph);
118+
}
35119
}
36120
}

0 commit comments

Comments
 (0)