MCPcopy Create free account
hub / github.com/QMHTMY/RustBook / build_knight_graph

Function build_knight_graph

code/chapter08/kinght_tour.rs:115–135  ·  view source on GitHub ↗

构建可移动路径图

(bdsize: u32)

Source from the content-addressed store, hash-verified

113
114// 构建可移动路径图
115fn build_knight_graph(bdsize: u32) -> Graph<u32> {
116 // 闭包函数,计算点值 [0, 63]
117 let calc_point = |row: u32, col: u32, size: u32| {
118 (row % size) * size + col
119 };
120
121 // 各点间设置边
122 let mut knight_graph = Graph::new();
123 for row in 0..bdsize {
124 for col in 0..bdsize {
125 let dests = legal_moves(row, col, bdsize);
126 for des in dests {
127 let src_p = calc_point(row, col, bdsize);
128 let des_p = calc_point(des.0, des.1, bdsize);
129 knight_graph.add_edge(&src_p, &des_p);
130 }
131 }
132 }
133
134 knight_graph
135}
136
137// depth: 走过的路径长度, curr: 当前节点, path: 保存访问过的点
138fn knight_tour<T>(

Callers 1

mainFunction · 0.70

Calls 2

legal_movesFunction · 0.70
add_edgeMethod · 0.45

Tested by

no test coverage detected