Eight digital problems--two-way breadth-first search solution

Source: Internet
Author: User

Eight digital problems: on the 3x3 checkered checkerboard, placed 1 to 8 of the eight digital, 1 squares are empty, its initial status of 1, the space is required to do the left shift, the space right shift, space up and space down these four operations make the board from the initial state to the target State.




There are two kinds of search order:

(1) Alternating two directions for expansion

(2) The one that has fewer nodes per selection

Generally speaking, the method (2) can overcome the imbalance of growth at both ends.

Eight.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <vector> #include <algorithm> #include <iostream>using namespace std;#  Define N 3#define SIZE 9typedef unsigned char byte;/*const int src[n][n] = {{2, 5, 4},{3, 0, 7},{1, 8, 6}};*/const int Src[n][n] = {{8, 7, 1},{5, 2, 6},{3, 4, 0}};/*const int Dst[n][n] = {{1, 2, 3},{8, 0, 4},{7, 6, 5}};* /const int Dst[n][n] = {{1, 2, 3},{4, 5, 6},{7, 8, 0}};struct code{byte value[size];bool operator== (const Code &A Mp;a) const{for (int i = 0; i < size;i++) if (A.value[i]!=value[i]) return False;return true; code& operator= (const code& a) {for (int i = 0; i < SIZE; i++) value[i] = A.value[i];return *this;}}; Code up (code a) {int ii=-1;for (int i = 0; i < SIZE; i++) if (a.value[i] = = 0) {II = I;break;} if (ii > 2) {BYTE temp = a.value[ii-3];a.value[ii-3] = 0;a.value[ii] = temp;} return A;} Code right (code a) {int II = -1;for (int i = 0; i < SIZE; i++) if (a.value[i] = = 0) {II = I;break;}if (ii>=0&& (ii+1)%3!=0) {BYTE temp = a.value[ii +1];a.value[ii +1] = 0;a.value[ii] = temp;} return A;} Code down (code a) {int II = -1;for (int i = 0; i < SIZE; i++) if (a.value[i] = = 0) {II = I;break;} if (Ii>=0&&ii <6) {BYTE temp = a.value[ii + 3];a.value[ii + 3] = 0;a.value[ii] = temp;} return A;} Code left (code a) {int II = -1;for (int i = 0; i < SIZE; i++) if (a.value[i] = = 0) {II = I;break;} if (ii >= 0&&ii%3!=0) {BYTE temp = a.value[ii-1];a.value[ii-1] = 0;a.value[ii] = temp;} return A;} Vector<code>solution;bool expand (vector<vector<code>>&tobeexpanded, vector<vector< Code>>&a_b, vector<code>&front_tobeexpanded, Vector<code>&front_a_b) {vector< Vector<code>>bbb;vector<code>::iterator it;front_tobeexpanded.clear (); for (int i = 0; i < Tobeexpanded.size (); i++) {if (!) ( Up (Tobeexpanded[i].back ()) = = Tobeexpanded[i].back ()) && find (Tobeexpanded[i].begin (), Tobeexpanded[i].enD (), Up (Tobeexpanded[i].back ())) = = Tobeexpanded[i].end ()) {it = find (Front_a_b.begin (), Front_a_b.end (), Up ( Tobeexpanded[i].back ())); if (it = Front_a_b.end ()) {solution = A_b[it-front_a_b.begin ()];reverse (Tobeexpanded[i]. Begin (), Tobeexpanded[i].end ()), Solution.insert (Solution.end (), Tobeexpanded[i].begin (), Tobeexpanded[i].end ()); return true;} Front_tobeexpanded.push_back (Up (Tobeexpanded[i].back ())), Bbb.push_back (Tobeexpanded[i]); Bbb.back (). Push_back ( Up (Tobeexpanded[i].back ()));} if (! ( Right (Tobeexpanded[i].back ()) = = Tobeexpanded[i].back ()) && find (Tobeexpanded[i].begin (), Tobeexpanded[i]. End (), Right (Tobeexpanded[i].back ())) = = Tobeexpanded[i].end ()) {it = find (Front_a_b.begin (), Front_a_b.end (), Right ( Tobeexpanded[i].back ())); if (it = Front_a_b.end ()) {solution = A_b[it-front_a_b.begin ()];reverse (Tobeexpanded[i]. Begin (), Tobeexpanded[i].end ()), Solution.insert (Solution.end (), Tobeexpanded[i].begin (), Tobeexpanded[i].end ()); return true;} Front_tobeexpanded.push_back (Right (TobEexpanded[i].back ())); Bbb.push_back (Tobeexpanded[i]); Bbb.back (). Push_back (Right (Tobeexpanded[i].back ()));} if (! ( Down (tobeexpanded[i].back ()) = = Tobeexpanded[i].back ()) && find (Tobeexpanded[i].begin (), Tobeexpanded[i]. End (), Down (Tobeexpanded[i].back ())) = = Tobeexpanded[i].end ()) {it = find (Front_a_b.begin (), Front_a_b.end (), Down ( Tobeexpanded[i].back ())); if (it = Front_a_b.end ()) {solution = A_b[it-front_a_b.begin ()];reverse (Tobeexpanded[i]. Begin (), Tobeexpanded[i].end ()), Solution.insert (Solution.end (), Tobeexpanded[i].begin (), Tobeexpanded[i].end ()); return true;} Front_tobeexpanded.push_back (Down (Tobeexpanded[i].back ())), Bbb.push_back (Tobeexpanded[i]); Bbb.back (). push_back (Down (Tobeexpanded[i].back ()));} if (! ( Left (Tobeexpanded[i].back ()) = = Tobeexpanded[i].back ()) && find (Tobeexpanded[i].begin (), Tobeexpanded[i]. End (), left (Tobeexpanded[i].back ())) = = Tobeexpanded[i].end ()) {it = find (Front_a_b.begin (), Front_a_b.end (), left ( Tobeexpanded[i].back ())); if (It! = FRONT_A_B.End ()) {solution = A_b[it-front_a_b.begin ()];reverse (Tobeexpanded[i].begin (), Tobeexpanded[i].end ()); Solution.insert (Solution.end (), Tobeexpanded[i].begin (), Tobeexpanded[i].end ()), return true; Front_tobeexpanded.push_back (Left (Tobeexpanded[i].back ())); Bbb.push_back (Tobeexpanded[i]); Bbb.back (). push_back (Left (Tobeexpanded[i].back ()));}} tobeexpanded = Bbb;return false;} void DBFS () {vector<vector<code>>aa,bb; Code start, ending;for (int i = 0; i < SIZE; i++) {start.value[i] = src[i/3][i% 3];ending.value[i] = dst[i/3][i% 3 ];} VECTOR&LT;CODE&GT;A1, B1;a1.push_back (start), b1.push_back (ending); aa.push_back (A1); Bb.push_back (B1);vector< Code>front_a, Front_b;front_a.push_back (start), Front_b.push_back (ending), while (true) {if (Aa.size () >bb.size ()) {if (expand (BB, AA, Front_b, front_a)) return;} Else{if (expand (AA, BB, front_a, Front_b)) return;}}} int _tmain (int argc, _tchar* argv[]) {DBFS (); System ("pause"); return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Eight digital problems--two-way breadth-first search solution

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.