Title Details: Portal
I have to do depressed, logic has been right, but the last group of answers is not. After a few hours of watching, I finally found the problem. I put the array initialization memset () function, placed in the custom function Input, using the formal parameter sizeof () as the length of the address, the result array did not initialize successfully, leading to the birth of tragedy. I finally solved the problem of changing the address length in memset () back to the array length. When I just did this, I treated it as a stack, and I never figured it out. After a search on the internet, it dawned. Suddenly, feel good sad, why did not think of themselves. Let's analyze the idea of the subject.
Analysis:
The use of recursion in the examination of this topic is a subject with Hanoi as its carrier. Recursion will return to the essence of the problem: since the optimal solution, then what is the optimal solution? If you can think of this place, then you are very close to success. In fact, there are only three steps to get the optimal solution: ① first move the n-1 disc on the nth disc from column A to the column B② move the nth disc from column A to the column C③ and then move the previous N-1 disc to column C. With these three steps, the movement from column A to column C can be accomplished in the best possible way. Thus, to the n disk from the column A (with the help of column B) to the column C, then the nth disk must be moved to column C, in other words, the nth disk must be between the column A and column C, if not so certainly not the optimal solution. And so on, to move the n-1 disc from column A (with the help of column C) to column B, then the n-1 disk must be between column A and column B. Here the problem is basically solved, the rest only with your hands on the keyboard to fly on it.
The subject code is as follows:
#include <stdio.h> #include <string.h> #define MAXN 10void Input (int *in_arr) {int I;memset (IN_ARR,0,MAXN *sizeof (int)); scanf ("%d", &in_arr[0]); for (i = 1; I <= in_arr[0]; i++) scanf ("%d", &in_arr[i]);} BOOL Hanoi (int n,int *a,int *b,int *c) {if (!n)//if n equals 0, then of course Truereturn true;if (n = = a[1])//If the nth disc is on column A, then the N-1 disk on column A ( A disc other than the nth disc) is moved through column C to the column B return Hanoi (N-1,++A,C,B); else if (n = = c[1])//If the nth disc is on column C, then the n-1 disc on column B is moved to column B by the column A to return Hanoi (N-1,B,A,++C); return false;//otherwise returns False}int main () {int t,n;int a[maxn],b[maxn],c[maxn];scanf ("%d", &t); while (t--) {scanf ("%d", &n), input (A), input (B), input (C), if (Hanoi (N,A,B,C)) printf ("true\n"), elseprintf ("false\ n ");} return 0;}
(If there is a mistake, please correct it, if there is reprint, indicate the source)
hdu1997 Hanoi VII (DFS recursive invocation)