AI jobs: three wild people and three missionaries

Source: Internet
Author: User

/* <Br/> when three wild people and three priests cross the river, the number of wild people cannot exceed the number of priests at any time. <Br/> <SPAN class = 'wp _ keywordlink '> algorithm </span>: <br/> wide search for poor performance. A * does not know how to write... <Br/> <SPAN class = 'wp _ keywordlink '> Program </span> implements a solution that selects the minimum number of entries, not all feasible solutions. <Br/> */<br/> # include <iostream> <br/> # include <deque> <br/> # include <algorithm> <br/> using namespace std; <br/> struct point {<br/> int A, B, S, F; // A indicates the number of people on the departure shore, and B indicates the number of wild people, the number of people on the other side can be obtained through a simple operation. S indicates the current side of the bank, 0 indicates the departure bank, 1 indicates the opposite bank, and F indicates the location of the parent node <br/> point (int A, int B, int S, int F): A (a), B (B), S (s), F (f) {}< br/> }; <br/> deque <point> DQ; // query queue; <br/> bool operator = (const point & P1, const point & p2) {return p1.a = p2.a & p1. B = p2. B & p1.s = p2.s;} <br/> ostream & operator <(ostream & _ OS, const point & P) {// output status <br/> return _ OS <"[" <p. A <"," <p. B <"," <p.s <"," <p. F <"]"; <br/>}< br/> bool ishave (int A, int B, int s) {// judge Whether the current status has exists <br/> If (find (DQ. Begin (), DQ. End (), point (A, B, S, 0 ))! = DQ. end () return 1; <br/> return 0; <br/>}< br/> bool isgo (int A, int B, int S) // used to determine when the time can pass <br/> {<br/> If (S = 0) {<br/> if (a <0 | B <0 | A> 3 | B> 3) return 0; <br/> if (! = 0 & A <B) Return 0; <br/> return 1; <br/>}else {<br/> int a1 = 3-A, b1 = 3-B; <br/> If (A1 <0 | B1 <0 | A1> 3 | B1> 3) return 0; <br/> If (A1! = 0 & A1 <B1) return 0; <br/> return 1; <br/>}< br/> // display the path with the minimum number of moves <br/> void show (int pos) <br/> {<br/> If (DQ [POS]. F = POS) {<br/> cout <DQ [POS] <Endl; <br/> return; <br/>}< br/> show (DQ [POS]. f); <br/> cout <DQ [POS] <Endl; <br/>}< br/> int main () <br/>{< br/> int A = 3, B = 3, S; <br/> int Pos = 0; // The current node location <br/> DQ. push_back (point (a, B, 0, 0); <br/> while (Pos! = DQ. size () {// Add a loop when there are other elements that can be added <br/> A = DQ [POS]. a; <br/> B = DQ [POS]. b; <br/> S = DQ [POS]. s; <br/> if (a = 0 & B = 0 & s = 1) break; <br/> If (DQ [POS]. S = 0) {// start, listing in five cases <br/> if (a <B &! = 0) {pos ++; continue;} // status unavailable <br/> If (isgo (A, B-2, 0 )&&! Ishave (A, B-2, 1) <br/> DQ. push_back (point (A, B-2, 1, POS); <br/> If (isgo (A-2, B, 0 )&&! Ishave (A-2, B, 1) <br/> DQ. push_back (point (A-2, B, 1, POS); <br/> If (isgo (A-1, B-1, 0 )&&! Ishave (A-1, B-1, 1) <br/> DQ. push_back (point (A-1, B-1, 1, POS); <br/> If (isgo (A-1, B, 0 )&&! Ishave (A-1, B, 1) <br/> DQ. push_back (point (A-1, B, 1, POS); <br/> If (isgo (A, B-1, 0 )&&! Ishave (A, B-1, 1) <br/> DQ. push_back (point (A, B-1, 1, POS); <br/>}else {// back <br/> If (3-)! = 0 & (3-a) <(3-B) {pos ++; continue;} // The status is unavailable <br/> If (isgo (a + 2, B, 1 )&&! Ishave (a + 2, B, 0) <br/> DQ. push_back (point (a + 2, B, 0, POS); <br/> If (isgo (A, B + 2, 1 )&&! Ishave (A, B + 2, 0) <br/> DQ. push_back (point (A, B + 2, 0, POS); <br/> If (isgo (a + 1, B + 1, 1, 1 )&&! Ishave (a + 1, B + 1, 0) <br/> DQ. push_back (point (a + 1, B + 1, 0, POS); <br/> If (isgo (a + 1, B, 1 )&&! Ishave (a + 1, B, 0) <br/> DQ. push_back (point (a + 1, B, 0, POS); <br/> If (isgo (A, B + 1, 1, 1 )&&! Ishave (A, B + 1, 0) <br/> DQ. push_back (point (A, B + 1, 0, POS); <br/>}< br/> POS ++; <br/>}< br/>/* queue status */<br/> for (INT I = 0; I <DQ. size (); I ++) <br/> cout <I <":" <DQ [I] <Endl; <br/> // display an optimal path <br/> cout <"--------------------" <Endl; <br/> show (POS); <br/>} 

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.