#include "stdio.h" #include "string.h" #include "conio.h" file * fp; / * Set up the file pointer to use it in other functions * / struct a {long M, S; Struct A * next;}; / * Array type A: Record the number of businessmen and servants on the boat, M: represents the number of people
S: Represents the number of servants * / struct a * jj, head; / * head is the chain module unit of the head pointer (the number of people on the board) * / int N, Total = 0, JS = 0; / * Total Indicates the total number of conditions on the ship * / struct aim {long M1, S1, M2, S2; INT N; Struct AIM * back, * next;}; / * Used to establish a two-way pointer chain table, record the case, M1 S1 means that the number of businessmen to be exported and the number of servants; M2, S2 means that the number of businessmen and servants are exported, and n represents the number of times of return * / int K1, K2; Void Freeit (Struct Aim * p) {Struct AIM * p1 = p; p1 = p-> back; free (p); if (p1! = null) p1-> next = null; return;
} / * Release the cell and restore the NEXT pointer of the cell on * /
INT DETERM (STRUCT AIM * P) {STRUCT AIM * P1 = P; if (P-> S1> K2) Return -1; / * The number of servants cannot exceed the total number of servants * / if (p-> m1> k1) Return -1; / * The number of business people cannot exceed the total number of people * / if (p-> s2> k2) return -1; / * Side of the same shore, the same * / if (p-> m2> k1) Return -1; / * Side , The same * / if (p-> s1 <0) return -1; / * The number of servants cannot be negative * / if (p-> s2 <0) return -1; / * Business people cannot be negative * / if ( P-> m1 <0) Return -1; / * Sipay, the same * / if (p-> m2 <0) return -1; / * on the other side, the same * / if (p-> m1! = 0) IF ( P-> S1> P-> m1) return -1; if (p-> m2! = 0) IF (P-> S2> P-> M2) Return -1; / * The number of business people on both sides cannot be less than the number of servants * / while (p1! = null) {p1 = p1-> back; if (p1! = null) IF (p1-> n% 2 == p-> n% 2) IF (P1-> S1 == P -> S1) IF (P1-> S2 == P-> S2) IF (p1-> m1 == p-> m1) IF (p1-> m2 == p-> m2) return -1;} / * Used to resolve duplicate, algorithm idea: to compare each of the chain table units each time, if repeated, then repeat, the loop * / if (P-> S1 == 0 && P-> m1 == 0) IF (P- > N% 2 == 0) Return 1; else return -1; / * Obviously if the condition is reached, the OK is indicated * / return 0;} / * Judgment function * /
INT SIGN (INT N) {IF (N% 2 == 0) Return -1; Return 1;} / * Symbol function * / void Copyit (Struct AIM * P3, Struct AIM * P) {P3-> S1 = P -> S1; P3-> S2 = P-> S2; P3-> m1 = p-> m1; p3-> m2 = p-> m2; p3-> n = p-> n 1; p3-> back = P; p3-> next = null; p-> next = p3;} / * copy content function, write content in P3 into the linked list unit pointed to by P3 * / void print (Struct AIM * P3) {STRUCT AIM * P = P3; JS ; while (p-> back) {p = p-> back;} printf ("/ nit's% d: / n", js); fprintf (fp, "/ nit's% d: / N ", js); while (p) {printf ("% ld,% ld ::% ld,% ld / t ", p-> m1, p-> S1, P-> M2, P-> S2) FPRINTF (FP, "% LD,% ld ::% LD,% LD / T", P-> M1, P-> S1, P-> M2, P-> S2); P = P-> NEXT; ;}} / * Print function, print the content referred to in P3 * /
Void Trans (STRUCT AIM * P) {structure AIM * P3; / * P3 is the application structure pointer * / struct a * fla; INT I, J, F, E; FLA = & head; p3 = (Struct AIM *) Malloc (Struct AIM); f = Sign (p-> n); for (i = 0; i IF (j == 1) {IF (i IF (j == 0) TRANS (P3);} return;} / * Transfer function, will transfer the river * // * n = 0 * / Main () {struct AIM * P, * P1; Int J, A, E, F; Struct A * flag; / * flag is used with record head pointer * / file * fpt; if ((fpt = fopen) : Result.dat "," W ")) == 0) {PrintF (" can't create it / n "); exit (0);} fp = fpt; clrs CR (); p = (struct aim *) Malloc (Struct AIM); p-> back = null; p-> next = 0; P-> M2 = 0; P-> n = 1; / * Set up an initial head pointer * / printf ("please input the total of people on the board / n"); fprintf (fp, "/ nplease input the total of people on the board / n"); scanf ("% d", & n); fprintf (FP, "/ N% D / N", N); FLAG = & head; for (E = 0; E <= n; E ) for (f = 0; f <= n; f ) IF (E F > 0 && E F <= N) {Total ; JJ = (Struct A *) Malloc (Struct A); JJ-> M = E; JJ-> S = F; Flag-> Next = JJ; JJ- > next = null; flag = jj;} / ***************************************************** / Printf ("Please Input the Total of Merchant and Salvent As Follow: Mechant , Salvent; / N "); FPRINTF (FP," / NPLEASE INPUT The Total of Merchant and Salvent As Follow: MeChant, Salvent; / N "); Scanf ("% LD,% LD ", & P-> M1, & P -> S1); FPRINTF (FP, "/ N% LD,% ld / n", P-> M1, P-> S1); / **************** ***************** / K1 = P-> M1; K2 = P-> S1; TRANS (P); Fclose (FPT); Getch ();} / * a year ago, I started the C language, inadvertently saw this question in several temples: there is N businessman K a servant crossing the river, the boat is up to M individual, on both sides There is a number of servants with any one-strait servants will die, and how to arrange how to arrange the merchant to pass through the river. If it does not print the whole process: the recommended start value n = 3, k = 3, m = 2 This problem is that I have thought after I learned my fingers, and I also had a problem in the place where I was raising, and now I wrote that I only have to commemorate, please don't laugh at me, thank you * / Note: I am not a computer professional, I only want to write fun, and I just want to put myself. did not expect. . . . However, I have nothing, I only feel that my own style needs to be improved. In fact, the idea is very simple. It is the way to search for the search. First, the number of people on the boat is built into a two-dimensional list, and the number of people on both sides is built into a two-way linked list. In the case where the condition is stored, the loop appearance is solved with the repeated method. In fact, it can be used to solve such a river problem, solving the problem is based on multi-dimensional direction, do not add comments, not me, but I use turboc2.0, can't use Chinese in its environment, and I am afraid that I am scared that everyone is increasing, and I know that I don't know much about C, I am a big two this year. I learned C in the big two semester, using a "C / C . . "The author is a Tang Dynasty, which is a textbook in Jiangsu Province.