KenKen puzzle addends: REDUX A (corrected) non-recursive algorithm

后端 未结 9 1715
眼角桃花
眼角桃花 2021-01-03 05:24

This question relates to those parts of the KenKen Latin Square puzzles which ask you to find all possible combinations of ncells numbers with values x such that 1 <= x &

9条回答
  •  爱一瞬间的悲伤
    2021-01-03 05:51

    Here a simple solution in C/C++:

    const int max = 6;
    int sol[N_CELLS];
    
    void enum_solutions(int target, int n, int min) {
      if (target == 0 && n == 0)
        report_solution(); /* sol[0]..sol[N_CELLS-1] is a solution */
      if (target <= 0 || n == 0) return; /* nothing further to explore */
      sol[n - 1] = min; /* remember */
      for (int i = min; i <= max; i++)
        enum_solutions(target - i, n - 1, i);
    }
    
    enum_solutions(12, 4, 1);
    

提交回复
热议问题