2 个稳定版本
使用旧的Rust 2015
1.0.1 | 2021年3月17日 |
---|---|
1.0.0 | 2018年4月7日 |
#3 in #滑动
38KB
966 行
滑动拼图
一个用于操作滑动拼图的Rust包。
概述
这个包重新实现了这个Ruby宝石的许多功能,旨在学习Rust并提供更快的实现。
我打算使用这个包来探索不同的“延迟重复检测”算法,例如外部内存图搜索中的结构化重复检测。
使用方法
有关更详细的文档,请参阅Ruby README.md。
let mut puzzle = SlidingPuzzle::new(&[
&[1, 2, 0],
&[3, 4, 5],
&[6, 7, 8],
]).unwrap();
puzzle.slide_mut(&Direction::Right).unwrap();
assert_eq!(puzzle.tiles(), &[
&[1, 0, 2],
&[3, 4, 5],
&[6, 7, 8],
]);
let top_left = puzzle.get(0, 0).unwrap();
assert_eq!(top_left, &1);
let position = puzzle.position(&1).unwrap();
assert_eq!(position, (0, 0));
assert_eq!(puzzle.moves(), &[
Direction::Left,
Direction::Right,
Direction::Up,
]);
assert!(puzzle.move_is_valid(&Direction::Up));
puzzle.scramble(100);
基准测试
可以使用 cargo bench
运行基准测试
test cloning_a_fifteen_puzzle ... bench: 36 ns/iter (+/- 5)
test from_decimal_on_a_fifteen_puzzle ... bench: 416 ns/iter (+/- 40)
test from_decimal_unchecked_on_a_fifteen_puzzle ... bench: 391 ns/iter (+/- 9)
test moves_for_a_fifteen_puzzle ... bench: 45 ns/iter (+/- 3)
test new_eight_puzzle ... bench: 320 ns/iter (+/- 30)
test new_eighty_puzzle ... bench: 739 ns/iter (+/- 27)
test scramble_fifty_moves_on_a_fifteen_puzzle ... bench: 10,809 ns/iter (+/- 1,181)
test ten_mutable_slides_on_a_fifteen_puzzle ... bench: 520 ns/iter (+/- 24)
test ten_mutable_unchecked_slides_on_a_fifteen_puzzle ... bench: 455 ns/iter (+/- 4)
test ten_slides_on_a_fifteen_puzzle ... bench: 899 ns/iter (+/- 61)
test ten_unchecked_slides_on_a_fifteen_puzzle ... bench: 782 ns/iter (+/- 11)
test to_decimal_on_a_fifteen_puzzle ... bench: 234 ns/iter (+/- 4)
test to_decimal_unchecked_on_a_fifteen_puzzle ... bench: 104 ns/iter (+/- 1)
示例算法
在 src/bin/search.rs
中有一个示例搜索算法
$ time cargo run --release
=== searching 181439 3x3 puzzles ===
estimated memory requirement: 0.000 GB
estimated compute time: 0.00 minutes
0.0% | depth: 0, width: 1
0.0% | depth: 1, width: 2
0.0% | depth: 2, width: 4
0.0% | depth: 3, width: 8
0.0% | depth: 4, width: 16
0.0% | depth: 5, width: 20
0.0% | depth: 6, width: 39
0.1% | depth: 7, width: 62
0.1% | depth: 8, width: 116
0.2% | depth: 9, width: 152
0.4% | depth: 10, width: 286
0.6% | depth: 11, width: 396
1.0% | depth: 12, width: 748
1.6% | depth: 13, width: 1024
2.6% | depth: 14, width: 1893
4.0% | depth: 15, width: 2512
6.5% | depth: 16, width: 4485
9.6% | depth: 17, width: 5638
14.8% | depth: 18, width: 9529
20.8% | depth: 19, width: 10878
30.2% | depth: 20, width: 16993
39.6% | depth: 21, width: 17110
52.8% | depth: 22, width: 23952
64.0% | depth: 23, width: 20224
77.2% | depth: 24, width: 24047
85.8% | depth: 25, width: 15578
93.8% | depth: 26, width: 14560
97.3% | depth: 27, width: 6274
99.5% | depth: 28, width: 3910
99.9% | depth: 29, width: 760
100.0% | depth: 30, width: 221
100.0% | depth: 31, width: 2
radius: 31
max width: 24047
depth of max: 24
here's one of the 2 hardest 3x3 puzzles:
8 7 6
0 4 1
2 5 3
it requires 31 moves to solve
real 0m0.261s
依赖关系
~475–660KB