Problem Description: Five people were planning to cross a drawbridge, and at first they were all on the side of the bridge. It was dark and there was only one flashlight in five men. The bridge can only be at the same time at most two people, whether it is a person or two people across the bridge, you need to carry a flashlight to see the road. And the flashlight can only pass through the way people carry the bridge. It takes 1 minutes for the first man to cross the bridge, a second person needs 2 minutes to cross the bridge, a third person takes 5 minutes, a fourth takes 7 minutes, and the fifth takes 10 minutes. As the speed is different, two people cross the bridge together, the speed of the slow people prevail. The fastest bridge time.
Main
Using system;using system.collections.generic;using System.linq;namespace passriver{class Program {private const int maxpasstogethercount = 2; static void Main (string[] args) {list<person> leftpersons = new list<person> (); list<person> rightpersons = new list<person> (); person P1 = new person (1); person P2 = new person (2); Person P3 = new person (5); person P4 = new person (7); Person P5 = new person (10); Leftpersons.add (p4); Leftpersons.add (p5); Leftpersons.add (p1); Leftpersons.add (p2); Leftpersons.add (p3); int totaltime = 0; while (Leftpersons.count! = 0) {list<person> twofastspeedpersons = Findtwofastspeedperson S (leftpersons); foreach (person, in twofastspeedpersons) {Leftpersons.remove); } rightpersons.addrange (Twofastspeedpersons); if (Twofastspeedpersons.count > 1) {totaltime + = twofastspeedpersons[1]. Speed; } else if (Twofastspeedpersons.count = = 1) {TotalTime + = Twofastspeedper Sons[0]. Speed; } else {}//------------- Console.WriteLine ("---Go to right---------->"); foreach (person, in Twofastspeedpersons) {Console.WriteLine. Speed); } Console.WriteLine ("Total time:" + totaltime); Console.WriteLine (); -------------if (Leftpersons.count = = 0 | | rightpersons.count==5) {BR Eak } Person Fastspeedperson = FinDfastspeedperson (rightpersons); Rightpersons.remove (Fastspeedperson); Leftpersons.add (Fastspeedperson); TotalTime + = Fastspeedperson.speed; -------------Console.WriteLine ("<----------Go back left---"); Console.WriteLine (Fastspeedperson.speed); Console.WriteLine ("Total time:" + totaltime); Console.WriteLine (); -------------if (Leftpersons.count = = 0 | | rightpersons.count = = 5) { Break } list<person> twolowestspeedpersons = Findtwolowestspeedpersons (leftpersons); foreach (person person in twolowestspeedpersons) {leftpersons.remove (person); } rightpersons.addrange (Twolowestspeedpersons); TotalTime + = Twolowestspeedpersons[0]. Speed; //-------------Console.WriteLine ("---Go to right---------->"); foreach (person, in Twolowestspeedpersons) {Console.WriteLine. Speed); } Console.WriteLine ("Total time:" + totaltime); Console.WriteLine (); -------------if (Leftpersons.count = = 0 | | rightpersons.count = = 5) { Break } Fastspeedperson = Findfastspeedperson (rightpersons); Rightpersons.remove (Fastspeedperson); Leftpersons.add (Fastspeedperson); TotalTime + = Fastspeedperson.speed; -------------Console.WriteLine ("<----------Go back left---"); Console.WriteLine (Fastspeedperson.speed); Console.WriteLine ("TotalTime:" + totaltime); Console.WriteLine (); //------------- if (Leftpersons.count = = 0 | | rightpersons.count = = 5) {break; }} Console.WriteLine ("------------Total time-------------"); Console.WriteLine (TotalTime); Console.readkey (); } public static list<person> findtwolowestspeedpersons (list<person> persons) {LIST&L T person> sortedpersons = persons. OrderByDescending (s = = s.speed). ToList (); list<person> passedpersons = new list<person> (); if (persons. Count > 1) {passedpersons = new list<person> (); Passedpersons.add (Sortedpersons[0]); Passedpersons.add (Sortedpersons[1]); } else if (persons. Count = = 1) {Passedpersons.add (sortedpersons[0]); } else {} return PassedpersonS } public static list<person> findtwofastspeedpersons (list<person> persons) {list< person> sortedpersons = persons. (s = = s.speed). ToList (); list<person> passedpersons = new list<person> (); if (persons. Count > 1) {passedpersons = new list<person> (); Passedpersons.add (Sortedpersons[0]); Passedpersons.add (Sortedpersons[1]); } else if (persons. Count = = 1) {Passedpersons.add (sortedpersons[0]); } else {return null; } return passedpersons; public static person Findfastspeedperson (list<person> persons) {if (persons. Count > 0) {list<person> sortedpersons = persons. (s = = s.speed). ToList (); return sortedPersons[0]; } else {return null; } } }}
Person
namespace passriver{public class person { private int _speed; Private Side _side; Public person (int.) { _speed = speed; _side = Side.left; } public int speed { get {return _speed;} set {_speed = value;} } Public Side Side { get {return _side;} set {_side = value;}}}}
Side
namespace passriver{public enum Side {left = 0, right = 1 }}
Sortutil
Using system.collections.generic;namespace passriver{public static class Sortutil {public static List <Person> Bubblesort (list<person> persons) {for (int i = 0; i < persons. Count-1; i++) {for (int j = i + 1; j < persons. Count; J + +) { if (persons[i]. Speed < Persons[j]. Speed) {person temp = persons[j]; PERSONS[J] = persons[i]; Persons[i] = temp;}} } return persons;}} }
Night over suspension bridge algorithm