A question searched by the people today:
Set numbers0-9Form a ring, from0Start (start point). You can go left or right to ask for a given number of times.KHow many methods can I return to the origin after the procedure? Ideas+Code
At that time, I gave the idea: because it can be left and right, and it is a circular array, we can use backtracking. Then the interviewer asked about the tracing efficiency. The answer is not ideal, but you can use pruning to reduce useless searches. Then the interviewer asked how to pruning. I thought for a moment: it seems that there are at least two possible loss cases:
1.If the number of steps is an odd number, it is impossible to return to the origin, so it can be reduced.
2.If the number of steps between the current access node and the origin is not possible, it can also be reduced.
The corresponding implementation code is as follows: the code is not strictly tested and may be incorrect orBug, Please note:
# Include <stdio. h>/** @ Param data: value of the current access node * @ Param K: current access level * @ Param N: Total number of moves * @ Param & COUNT: Number of feasible times, referenced variable */void process (INT data, int K, int N, Int & COUNT) {If (N % 2 = 1) {COUNT = 0; return ;} int min = data> (10-data )? (10-data): Data; If (min> (n-k) {return;} If (k = N) {If (Data = 0) {count ++;} return;} else {DATA = (Data + 1) % 10; process (data, k + 1, n, count ); data = (data-1) % 10; Data = (data-1) % 10; process (data, k + 1, n, count); Data = (Data + 1) % 10 ;}} int main () {int COUNT = 0; process (0, 0, 4, count); printf ("% d", count );}