Skip to content

Commit f1cb878

Browse files
committed
添加新的example,准备添加AM_Graph
1 parent e995552 commit f1cb878

File tree

4 files changed

+133
-8
lines changed

4 files changed

+133
-8
lines changed

eight_numbers_c.psess

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<VSPerformanceSession Version="1.00">
3+
<Options>
4+
<Solution>.\</Solution>
5+
<CollectionMethod>Concurrency</CollectionMethod>
6+
<AllocationMethod>None</AllocationMethod>
7+
<AddReport>true</AddReport>
8+
<ResourceBasedAnalysisSelected>true</ResourceBasedAnalysisSelected>
9+
<UniqueReport>Timestamp</UniqueReport>
10+
<SamplingMethod>Cycles</SamplingMethod>
11+
<CycleCount>10000000</CycleCount>
12+
<PageFaultCount>10</PageFaultCount>
13+
<SysCallCount>10</SysCallCount>
14+
<SamplingCounter Name="" ReloadValue="00000000000f4240" DisplayName="" />
15+
<RelocateBinaries>false</RelocateBinaries>
16+
<HardwareCounters EnableHWCounters="false" />
17+
<EtwSettings />
18+
<PdhSettings>
19+
<PdhCountersEnabled>false</PdhCountersEnabled>
20+
<PdhCountersRate>500</PdhCountersRate>
21+
<PdhCounters>
22+
<PdhCounter>\Memory\Pages/sec</PdhCounter>
23+
<PdhCounter>\PhysicalDisk(_Total)\Avg. Disk Queue Length</PdhCounter>
24+
<PdhCounter>\Processor(_Total)\% Processor Time</PdhCounter>
25+
</PdhCounters>
26+
</PdhSettings>
27+
</Options>
28+
<ExcludeSmallFuncs>true</ExcludeSmallFuncs>
29+
<InteractionProfilingEnabled>false</InteractionProfilingEnabled>
30+
<JScriptProfilingEnabled>false</JScriptProfilingEnabled>
31+
<PreinstrumentEvent>
32+
<InstrEventExclude>false</InstrEventExclude>
33+
</PreinstrumentEvent>
34+
<PostinstrumentEvent>
35+
<InstrEventExclude>false</InstrEventExclude>
36+
</PostinstrumentEvent>
37+
<Binaries>
38+
<Binary>
39+
<Path>bin\eight_numbers_c.exe</Path>
40+
<ArgumentTimestamp>01/01/0001 00:00:00</ArgumentTimestamp>
41+
<Instrument>true</Instrument>
42+
<Sample>true</Sample>
43+
<ExternalWebsite>false</ExternalWebsite>
44+
<InteractionProfilingEnabled>false</InteractionProfilingEnabled>
45+
<IsLocalJavascript>false</IsLocalJavascript>
46+
<IsWindowsStoreApp>false</IsWindowsStoreApp>
47+
<IsWWA>false</IsWWA>
48+
<LaunchProject>true</LaunchProject>
49+
<OverrideProjectSettings>false</OverrideProjectSettings>
50+
<LaunchMethod>Executable</LaunchMethod>
51+
<ExecutablePath>bin\eight_numbers_c.exe</ExecutablePath>
52+
<StartupDirectory>
53+
</StartupDirectory>
54+
<Arguments>
55+
</Arguments>
56+
<NetAppHost>IIS</NetAppHost>
57+
<NetBrowser>InternetExplorer</NetBrowser>
58+
<ExcludeSmallFuncs>true</ExcludeSmallFuncs>
59+
<JScriptProfilingEnabled>false</JScriptProfilingEnabled>
60+
<PreinstrumentEvent>
61+
<InstrEventExclude>false</InstrEventExclude>
62+
</PreinstrumentEvent>
63+
<PostinstrumentEvent>
64+
<InstrEventExclude>false</InstrEventExclude>
65+
</PostinstrumentEvent>
66+
</Binary>
67+
</Binaries>
68+
<Launches>
69+
<Binary>
70+
<Path>bin\eight_numbers_c.exe</Path>
71+
</Binary>
72+
</Launches>
73+
</VSPerformanceSession>

include/c_algorithm_module.h

+8
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ extern "C" {
4949
//@Return:节点idx_v的编号为idx_e的边的容量
5050
typedef double(*AM_EdgeContainAdapter)(int idx_v, int idx_e);
5151

52+
typedef struct {
53+
AM_FirstEdgeAdapter firstOf;
54+
AM_NextEdgeAdapter nextOf;
55+
AM_EdgeToAdapter destOf;
56+
AM_EdgeLengthAdapter lengthOf;
57+
AM_EdgeContainAdapter containOf;
58+
}AM_Graph;
59+
5260
//@Param idx_start_v:最初的节点编号
5361
//@Param idx_dest_v:目标节点编号
5462
//@Param firstOf,nextOf,edgeTo:图的属性

src/algorithm_module/c_adapt.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ extern "C" {
3333
firstOf, nextOf, destOf));
3434
}
3535

36-
PUBLIC int
37-
AM_bfsStep(
38-
int idx_start_v,int idx_dest_v,
39-
AM_FirstEdgeAdapter firstOf,AM_NextEdgeAdapter nextOf,
40-
AM_EdgeToAdapter destOf) {
41-
return Algorithm::Graph::bfsStep(idx_start_v,idx_dest_v,
42-
firstOf,nextOf,destOf);
43-
}
36+
PUBLIC int
37+
AM_bfsStep(
38+
int idx_start_v, int idx_dest_v,
39+
AM_FirstEdgeAdapter firstOf, AM_NextEdgeAdapter nextOf,
40+
AM_EdgeToAdapter destOf) {
41+
return Algorithm::Graph::bfsStep(idx_start_v, idx_dest_v,
42+
firstOf, nextOf, destOf);
43+
}
4444
}

src/test/eight_numbers_main.c

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//本文件以八数码问题为测试问题,输出所有可解的情况和其所需的步数,顺便演示一波骚操作
2+
3+
#include <stdio.h>
4+
#include "c_algorithm_module.h"
5+
6+
int nextOf(int v, int e) {
7+
const int dx[] = { 0,1,0,-1 };
8+
const int dy[] = { 1,0,-1,0 };
9+
10+
int zero_pos = 0;
11+
for (int k = 1; k <= 1E9 && v / k % 10 != 0; k *= 10, zero_pos++);
12+
const int zero_x = zero_pos % 3, zero_y = zero_pos / 3;
13+
14+
for (int i = e + 1; i < 4; i++) {
15+
const int x_n = zero_x + dx[i], y_n = zero_y + dy[i];
16+
if (x_n >= 0 && x_n < 3 && y_n >= 0 && y_n < 3) return i;
17+
}
18+
return AM_NIL;
19+
}
20+
21+
int sum = 0;
22+
int firstOf(int v) {
23+
sum += 1;
24+
printf("%03d\n%03d\n%03d\n\n", v / 1000 / 1000 % 1000, v / 1000 % 1000, v % 1000);
25+
return nextOf(v, -1);
26+
}
27+
28+
int destOf(int v, int e) {
29+
const int k[] = { 1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000 };
30+
const int new_pos[] = { 3,1,-3,-1 };
31+
int zero_pos = 0;
32+
for (int k = 1; k <= 1E9 && v / k % 10 != 0; k *= 10, zero_pos++);
33+
const int cur_num = v / k[zero_pos + new_pos[e]] % 10;
34+
return v + cur_num * (k[zero_pos] - k[zero_pos + new_pos[e]]);
35+
}
36+
37+
int main() {
38+
const int idx_start_v = 123804765;
39+
const int idx_end_v = AM_NIL;
40+
41+
AM_bfsStep(idx_start_v, idx_end_v, firstOf, nextOf, destOf);
42+
printf("%d\n", sum);
43+
return 0;
44+
}

0 commit comments

Comments
 (0)