Another version of merchant crossing the river (merchant count, variable number of people on board)

Source: Internet
Author: User
# Include <stdio. h> # include <stdbool. h> # include <stdlib. h> # include "Sv. H "Void module (struct vector [], INT); bool test (INT, Int, INT); void Delete (void); void addnew (INT, Int, INT ); void findout (void); int persons, capcity; s_node * Final = NULL; int main (void) {printf ("input the number of traders (just equal # servants ): "); scanf (" % d ", & persons); getchar (); printf (" the boat can rive persons: "); scanf (" % d ", & Cap City); getchar (); s_node s_first = {. X = Persons ,. y = Persons ,. tst_cnt = 0 ,. dir = 1 ,. pre = NULL ,}; final = & s_first; int ways = capcity + (capcity + 1) * capcity/2; struct vector u [ways]; int K = 0; for (INT I = 0; I <= capcity; I ++) for (Int J = 0; j <= capcity; j ++) if (I + j <= capcity & I + j> 0) {u [K]. V = I; U [k ++]. W = J;} module (u, ways); Return 0;} void module (struct vector u [], int N) {While (true ){/ /Stop moduleif (Final-> X = 0 & final-> Y = 0) {findout (); break ;} else if (Final-> pre = NULL & final-> tst_cnt = N) {printf ("No method, well, that is you failed !!!!!!!!!! \ N "); break;} // continue moduleif (Final-> tst_cnt = N) {Delete (); continue ;} int trader = final-> X-(Final-> DIR) * U [Final-> tst_cnt]. v; int servant = final-> Y-(Final-> DIR) * U [Final-> tst_cnt]. w; int Director =-(Final-> DIR); If (test (trader, servant, Director) {addnew (trader, servant, Director); Continue ;}}} void findout (void) {s_node * iterate = final; while (iterate! = NULL) {printf ("(% d, % d) <-", iterate-> X, iterate-> Y); iterate = iterate-> pre ;} printf ("Start \ n"); printf ("yes, you succeed !!!! \ N ");} bool test (int t, int S, int d) {final-> tst_cnt ++; if (T <0 | T> Persons | S <0 | S> Persons) return false; if (t = 0 | T = S | T = Persons) {s_node * iterate = final; while (iterate! = NULL) {If (iterate-> X = T & iterate-> Y = S & iterate-> dir = d) return false; iterate = iterate-> pre;} return true;} elsereturn false;} void Delete (void) {s_node * newptr = final; Final = final-> pre; free (newptr);} void addnew (int t, int S, int d) {s_node * newptr = malloc (sizeof (s_node); newptr-> X = T; newptr-> Y = s; newptr-> dir = D; newptr-> tst_cnt = 0; newptr-> pre = final; Final = newptr;} Here is the declaration of the relevant structure.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.