diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2023-11-08 18:53:00 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2023-11-08 18:53:00 +0100 |
| commit | 5bdf6e73179cf944ce82606545beb0b0d63a59a3 (patch) | |
| tree | edc0897f2479e1b069d9e36183b92c0416d243b6 /cube.c | |
| parent | e2004826a56b1e2cac8b3d5a94535e480ca2855d (diff) | |
| download | nissy-core-5bdf6e73179cf944ce82606545beb0b0d63a59a3.tar.gz nissy-core-5bdf6e73179cf944ce82606545beb0b0d63a59a3.zip | |
Improved comments
Diffstat (limited to 'cube.c')
| -rw-r--r-- | cube.c | 59 |
1 files changed, 49 insertions, 10 deletions
| @@ -3688,19 +3688,51 @@ implementation of all the solving algorithms. | |||
| 3688 | 3688 | ||
| 3689 | typedef struct { | 3689 | typedef struct { |
| 3690 | cube_t cube; | 3690 | cube_t cube; |
| 3691 | int (*estimate)(cube_t); | ||
| 3692 | uint8_t depth; | 3691 | uint8_t depth; |
| 3693 | int maxsols; | 3692 | int maxsols; |
| 3694 | move_t *sols; | 3693 | move_t *sols; |
| 3695 | int nsols; | 3694 | int nsols; |
| 3696 | int nmoves; | 3695 | int nmoves; |
| 3697 | move_t moves[20]; | 3696 | move_t moves[20]; |
| 3697 | int (*estimate)(cube_t); | ||
| 3698 | } dfs_arg_t; | 3698 | } dfs_arg_t; |
| 3699 | 3699 | ||
| 3700 | int | 3700 | static bool |
| 3701 | allowednextmove(dfs_arg_t arg, move_t m) | ||
| 3702 | { | ||
| 3703 | int n; | ||
| 3704 | move_t mbase, l1base, l2base, maxis, l1axis, l2axis; | ||
| 3705 | |||
| 3706 | n = arg.nmoves; | ||
| 3707 | |||
| 3708 | if (n == 0) | ||
| 3709 | return true; | ||
| 3710 | |||
| 3711 | mbase = m / 3; | ||
| 3712 | maxis = mbase / 2; | ||
| 3713 | l1base = arg.moves[n-1] / 3; | ||
| 3714 | l1axis = l1base / 2; | ||
| 3715 | |||
| 3716 | if (mbase == l1base || (maxis == l1axis && mbase < l1base)) | ||
| 3717 | return false; | ||
| 3718 | |||
| 3719 | if (n == 1) | ||
| 3720 | return true; | ||
| 3721 | |||
| 3722 | l2base = arg.moves[n-2] / 3; | ||
| 3723 | l2axis = l1base / 2; | ||
| 3724 | |||
| 3725 | return l1axis != l2axis || mbase != l2base; | ||
| 3726 | } | ||
| 3727 | |||
| 3728 | static int | ||
| 3701 | solve_generic_dfs(dfs_arg_t arg) | 3729 | solve_generic_dfs(dfs_arg_t arg) |
| 3702 | { | 3730 | { |
| 3703 | int bound = arg.estimate(arg.cube); | 3731 | dfs_arg_t nextarg; |
| 3732 | int bound, ret; | ||
| 3733 | move_t m; | ||
| 3734 | |||
| 3735 | bound = arg.estimate(arg.cube); | ||
| 3704 | 3736 | ||
| 3705 | if (arg.nsols == arg.maxsols || bound + arg.nmoves > arg.depth) | 3737 | if (arg.nsols == arg.maxsols || bound + arg.nmoves > arg.depth) |
| 3706 | return 0; | 3738 | return 0; |
| @@ -3714,36 +3746,43 @@ solve_generic_dfs(dfs_arg_t arg) | |||
| 3714 | return 1; | 3746 | return 1; |
| 3715 | } | 3747 | } |
| 3716 | 3748 | ||
| 3717 | /* TODO: loop over moves and recur */ | 3749 | memcpy(&nextarg, &arg, sizeof(dfs_arg_t)); |
| 3718 | return 0; | 3750 | nextarg.nmoves = arg.nmoves + 1; |
| 3751 | for (m = 0, ret = 0; m < 18; m++) { | ||
| 3752 | if (allowednextmove(arg, m)) { | ||
| 3753 | nextarg.cube = move(arg.cube, m); | ||
| 3754 | nextarg.moves[arg.nmoves] = m; | ||
| 3755 | ret += solve_generic_dfs(nextarg); | ||
| 3756 | } | ||
| 3757 | } | ||
| 3758 | |||
| 3759 | return ret; | ||
| 3719 | } | 3760 | } |
| 3720 | 3761 | ||
| 3721 | int | 3762 | int |
| 3722 | solve_generic( | 3763 | solve_generic( |
| 3723 | cube_t cube, | 3764 | cube_t cube, |
| 3724 | int (*estimate)(cube_t), | ||
| 3725 | uint8_t depth, | 3765 | uint8_t depth, |
| 3726 | int maxsols, | 3766 | int maxsols, |
| 3727 | move_t *sols | 3767 | move_t *sols |
| 3768 | int (*estimate)(cube_t), | ||
| 3728 | ) | 3769 | ) |
| 3729 | { | 3770 | { |
| 3730 | dfs_arg_t arg; | 3771 | dfs_arg_t arg; |
| 3731 | 3772 | ||
| 3732 | if (!issolvable(cube) || depth > 20) | 3773 | if (!issolvable(cube) || depth > 20 || estimate == NULL) |
| 3733 | return -1; | 3774 | return -1; |
| 3734 | 3775 | ||
| 3735 | arg = (dfs_arg_t) { | 3776 | arg = (dfs_arg_t) { |
| 3736 | .cube = cube, | 3777 | .cube = cube, |
| 3737 | .estimate = estimate, | ||
| 3738 | .depth = depth, | 3778 | .depth = depth, |
| 3739 | .maxsols = maxsols, | 3779 | .maxsols = maxsols, |
| 3740 | .sols = sols, | 3780 | .sols = sols, |
| 3741 | .nsols = 0, | 3781 | .nsols = 0, |
| 3742 | .nmoves = 0, | 3782 | .nmoves = 0, |
| 3743 | .moves = {0} | 3783 | .moves = {0} |
| 3784 | .estimate = estimate, | ||
| 3744 | }; | 3785 | }; |
| 3745 | 3786 | ||
| 3746 | return solve_generic_dfs(arg); | 3787 | return solve_generic_dfs(arg); |
| 3747 | |||
| 3748 | return 0; | ||
| 3749 | } | 3788 | } |
