Pku acm 1077.

Source: Internet
Author: User

The classic 8 digital problem mainly uses the "ordered search algorithm" in artificial intelligence, which is also called a algorithm.

# Include <iostream> # include <cstdio> # include <algorithm> # include <set> # include <string> using namespace STD; const int Len = 9; struct pstatus {string Pos; // The position information int index; // 'X' Coordinate Position pstatus * parent; // point to the parent State char direction; // L, R, u, dint height; // height int F; // F value}; pstatus bstatus; pstatus gstatus = {"12345678x"}; int getwrongposes (string POS) // Number of misplaced pawns {int sum = 0; For (INT I = 0; I <POS. size (); ++ I) if (gstatus. pos [I]! = POS [I]) sum ++; return sum;} bool CMP1 (pstatus * First, pstatus * Second) {int r = first-> POS. compare (second-> POS); If (r <0) return false; If (r> 0) return true; return false;} set <pstatus *, bool (*) (pstatus *, pstatus *)> closed (CMP1); bool cmp2 (pstatus * First, pstatus * Second) {If (first-> F <second-> F) return true; if (first-> F> second-> F) return false;} Multiset <pstatus *, bool (*) (pstatus *, pstatus * )> Open (cmp2); bool isinclosed (pstatus * Status) {If (closed. Find (Status )! = Closed. end () return true; return false;} bool isinopen (pstatus * Status) {Multiset <pstatus *, bool (*) (pstatus *, pstatus *)> :: iterator it = open. begin (); For (; it! = Open. end (); ++ it) if (status-> Pos = (* It)-> POS) return true; return false;} string BFS () {pstatus * ps, * NP; int I; open. insert (& bstatus); While (open. size ()> 0) {PS = * open. begin (); If (PS-> Pos = gstatus. pos) // find the answer {string result = ""; while (PS! = NULL) {If (PS-> direction! = 'Z') Result = ps-> direction + result; PS = ps-> parent;} return result;} open. erase (open. begin (); closed. insert (PS); for (I = 0; I <4; ++ I) {Switch (I) {Case 0: // leftif (PS-> direction! = 'R' & PS-> index % 3! = 0) {NP = new pstatus; NP-> Pos = ps-> Pos; swap (NP-> POS [PS-> Index], NP-> POS [PS-> index-1]); If (! Isinclosed (NP )&&! Isinopen (NP) {NP-> parent = Ps; NP-> Index = ps-> index-1; NP-> direction = 'l '; // NP-> Height = ps-> height + 1; // NP-> F = NP-> height + getwrongposes (NP-> POS ); NP-> F = getwrongposes (NP-> POS); open. insert (NP);} elsedelete NP;} break; Case 1: // rightif (PS-> direction! = 'L' & PS-> index % 3! = 2) {NP = new pstatus; NP-> Pos = ps-> Pos; swap (NP-> POS [PS-> Index], NP-> POS [PS-> index + 1]); If (! Isinclosed (NP )&&! Isinopen (NP) {NP-> parent = Ps; NP-> Index = ps-> index + 1; NP-> direction = 'R '; // NP-> Height = ps-> height + 1; // NP-> F = NP-> height + getwrongposes (NP-> POS ); NP-> F = getwrongposes (NP-> POS); open. insert (NP);} elsedelete NP;} break; Case 2: // upif (PS-> direction! = 'D' & (PS-> index-PS-> index % 3)> 2) {NP = new pstatus; NP-> Pos = ps-> Pos; swap (NP-> POS [PS-> Index], NP-> POS [PS-> index-3]); If (! Isinclosed (NP )&&! Isinopen (NP) {NP-> parent = Ps; NP-> Index = ps-> index-3; NP-> direction = 'U '; // NP-> Height = ps-> height + 1; // NP-> F = NP-> height + getwrongposes (NP-> POS ); NP-> F = getwrongposes (NP-> POS); open. insert (NP);} elsedelete NP;} break; Case 3: // downif (PS-> direction! = 'U' & PS-> index <6) {NP = new pstatus; NP-> Pos = ps-> Pos; swap (NP-> POS [PS-> Index], NP-> POS [PS-> index + 3]); If (! Isinclosed (NP )&&! Isinopen (NP) {NP-> parent = Ps; NP-> Index = ps-> index + 3; NP-> direction = 'D '; // NP-> Height = ps-> height + 1; // NP-> F = NP-> height + getwrongposes (NP-> POS ); NP-> F = getwrongposes (NP-> POS); open. insert (NP) ;}elsedelete NP;} break ;}}return "unsolvable" ;}// determine whether the two statuses are reachable. You can calculate the Reverse values of the two statuses, if their parity is the same, they are reachable. Otherwise, the two statuses cannot reach bool solvable (string POs, int index) {pos. erase (index, 1); int sum = 0; For (INT I = 1; I <POS. size (); ++ I) {for (I Nt j = 0; j <I; ++ J) if (Pos [I] <POS [J]) sum + = 1;} If (sum % 2 = 0) return true; else return false;} int main () {freopen ("in.txt", "r", stdin); char ch; For (INT I = 0; I <Len; ++ I) {CIN> CH; bstatus. pos + = CH; If (CH = 'X') bstatus. index = I;} If (! Solvable (bstatus. POs, bstatus. index) cout <"unsolvable" <Endl; else {bstatus. parent = NULL; bstatus. direction = 'Z'; // No direction bstatus. height = 0; bstatus. F = getwrongposes (bstatus. pos) + bstatus. height; cout <BFS () <Endl ;}}

Test Data

2 3 4 1 5x7 6 8

1 2 3 7 6 4 8x5

1 2 3 7x4 8 6 5

1 2 3 4 5 6 7x8

1 2 7 4 5 6 3 8 x

The following version is too slow, but it is still good for STL learning.

// # Include <iostream> // # include <cstdio> // # include <algorithm> // # include <list> // # include <string> // using namespace std; /// const int Len = 9; // struct pstatus // {// string Pos; // location information // int index; // 'X' Coordinate Position // pstatus * parent; // point to the parent State // char direction; // L, R, U, d //}; /// template <class T1, class T2, class T3> // struct fcmp: public STD: binary_function <T1, T2, t3> // {// T3 operator () (T1 first, T2 second) const // {// RET Urn first-> Pos = Second-> Pos; //}; // pstatus bstatus; // pstatus gstatus = {"12345678x "}; // list <pstatus *> open, closed; // bool isinclosed (pstatus * Status) // {// list <pstatus *> :: iterator it = find_if (closed. begin (), closed. end (), // bind2nd (fcmp <pstatus *, pstatus *, bool> (), status); // If (it! = Closed. end () return true; // return false; //} // bool isinopen (pstatus * Status) // {// list <pstatus *> :: iterator it = find_if (open. begin (), open. end (), // bind2nd (fcmp <pstatus *, pstatus *, bool> (), status); // If (it! = Open. end () return true; // return false; //} // void BFS () // {// pstatus * ps, * NP; // int I; // open. push_back (& bstatus); // while (open. size ()> 0) // {// PS = open. front (); // If (PS-> Pos = gstatus. pos) // find the answer // {// while (PS! = NULL) // {// cout <ps-> direction; // PS = ps-> parent; //} // return; ///} // open. pop_front (); // closed. push_back (PS); // for (I = 0; I <4; ++ I) // {// switch (I) // {// case 0: // left // If (PS-> ction! = 'R' & PS-> index % 3! = 0) // {// Np = new pstatus; // NP-> Pos = ps-> Pos; // swap (NP-> POS [PS-> Index], NP-> POS [PS-> index-1]); // If (! Isinclosed (NP )&&! Isinopen (NP) // {// NP-> parent = Ps; // NP-> Index = ps-> index-1; // NP-> direction = 'l'; // open. push_back (NP); // else // Delete NP; //} // break; // Case 1: // right // If (PS-> direction! = 'L' & PS-> index % 3! = 2) // {// Np = new pstatus; // NP-> Pos = ps-> Pos; // swap (NP-> POS [PS-> Index], NP-> POS [PS-> index + 1]); // If (! Isinclosed (NP )&&! Isinopen (NP) // {// NP-> parent = Ps; // NP-> Index = ps-> index + 1; // NP-> direction = 'R'; // open. push_back (NP); // else // Delete NP; //} // break; // Case 2: // up // If (PS-> direction! = 'D' & (PS-> index-PS-> index % 3)> 2) // {// Np = new pstatus; // NP-> Pos = ps-> Pos; // swap (NP-> POS [PS-> Index], NP-> POS [PS-> index-3]); // If (! Isinclosed (NP )&&! Isinopen (NP) // {// NP-> parent = Ps; // NP-> Index = ps-> index-3; // NP-> direction = 'U'; // open. push_back (NP); // else // Delete NP; //} // break; // Case 3: // down // If (PS-> direction! = 'U' & PS-> index <6) // {// Np = new pstatus; // NP-> Pos = ps-> Pos; // swap (NP-> POS [PS-> Index], NP-> POS [PS-> index + 3]); // If (! Isinclosed (NP )&&! Isinopen (NP) // {// NP-> parent = Ps; // NP-> Index = ps-> index + 3; // NP-> direction = 'D'; // open. push_back (NP); // else // Delete NP; //} // break; ///} // cout <"unsolvable" <Endl; //} // bool solvable (string POs, int index) // {// POS. erase (index, 1); // int sum = 0; // For (INT I = 1; I <POS. size (); ++ I) // {// For (Int J = 0; j <I; ++ J) // If (Pos [I] <POS [J]) // sum + = 1; //} // If (sum % 2 = 0) return true; // else RET Urn false; //} // int main () // {// freopen ("in.txt", "r", stdin); // char ch; // For (INT I = 0; I <Len; ++ I) // {// CIN> CH; // bstatus. pos + = CH; // If (CH = 'X') bstatus. index = I; //} // If (! Solvable (bstatus. POs, bstatus. index) // cout <"unsolvable" <Endl; // else // {// bstatus. parent = NULL; // bstatus. direction = 'Z'; // No direction // BFS ();//}////}
Related Keywords:

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.