Source code of Automatic Sorting for jigsaw puzzle games

Source: Internet
Author: User

// Input a []; M, N; <br/> // final state final [] <br/>/* A [9] = {0, 1, 2, 3, 5, 4, 6, 7, 8 }; <br/> * final [9] =, 8} <br/> ********************************** * ********** <br/> * @ author lizhijie | onezeros <br/> ************** * ****************************/<br/> # include <Vector> <br/> # include <iostream> <br/> using namespace STD; </P> <p> struct MapInfo {<br/> char a [9]; // Store Current Situation <br/> // I NT seltnum; // position in current path <br/> // int globnum; // prior position of this path in vector <br/> }; </P> <p> int final [9] = {0, 1, 3, 4, 5, 6, 7, 8}; <br/> int A [9] = {0, 1, 2, 3, 5, 4, 6, 7, 8 }; <br/> vector <MapInfo> MapInfo; </P> <p> // I: the current position <br/> // prior: lastest position <br/> bool go (INT prior, int I) <br/>{< br/> Int J; <br/> for (j = 0; j <9; j ++) {// check wether it's the result <br/> If (MapInfo. A [J]! = Final [J]) <br/> break; <br/>}< br/> If (j = 9) return true; </P> <p> vector <MapInfo>: iterator ITERA; <br/> for (ITERA = MapInfo. begin (); iworkflow <MapInfo. end ()-1; iworkflow ++) {<br/> for (j = 0; j <9; j ++) {// check wether current situation has happened <br/> If (* iworkflow ). A [J]! = MapInfo. back. A [J]) <br/> break; <br/>}< br/> If (j = 9) {<br/> // because there is only one step after branch <br/> MapInfo. pop_back (); <br/> return false; <br/>}</P> <p> MapInfo current [9]; <br/> for (j = 0; j <9; j ++) {<br/> current. A [J] = MapInfo. back. A [J]; <br/>}< br/> switch (I) {<br/> case 0: <br/> If (Prior! = 1 & Prior! = 3) {<br/> pushpath (current,); <br/> go (); <br/> pushpath (current ); <br/> go (0, 3); <br/>}< br/> break; <br/> case 1: <br/> If (Prior! = 0 & Prior! = 4 & Prior! = 2) {<br/> pushpath (current,); <br/> go (); <br/> pushpath (current ); <br/> go (); <br/> pushpath (current,); <br/> go (); <br/>}< br/> break; <br/> case 2: <br/> If (Prior! = 1 & Prior! = 5) {<br/> pushpath (current, 2, 1); <br/> go (2, 1); <br/> pushpath (current, 2, 5 ); <br/> go (2, 5); <br/>}< br/> break; <br/> case 3: <br/> If (Prior! = 0 & Prior! = 4 & Prior! = 6) {<br/> pushpath (current, 3, 0); <br/> go (3, 0); <br/> pushpath (current, 3, 4 ); <br/> go (3, 4); <br/> pushpath (current, 3, 6); <br/> go (3, 6); <br/>}< br/> break; <br/> case 4: <br/> If (Prior! = 1 & Prior! = 3 & Prior! = 5 & Prior! = 7) {<br/> pushpath (current, 4.1); <br/> go (); <br/> pushpath (current ); <br/> go (); <br/> pushpath (current,); <br/> go (); <br/> pushpath (current ): <br/> go (4, 7); <br/>}< br/> break; <br/> case 5: <br/> If (Prior! = 2 & Prior! = 4 & Prior! = 8) {<br/> pushpath (current, 5, 2); <br/> go (5, 2); <br/> pushpath (current, 5, 4 ); <br/> go (5, 4); <br/> pushpath (current, 5, 8); <br/> go (5, 8); <br/>}< br/> break; <br/> case 6: <br/> If (Prior! = 3 & Prior! = 7) {<br/> pushpath (current, 6, 3); <br/> go (6, 3); <br/> pushpath (current, 6, 7 ); <br/> go (6, 7); <br/>}< br/> break; <br/> case 7: <br/> If (Prior! = 6 & Prior! = 4 & Prior! = 8) {<br/> pushpath (current, 7,4); <br/> go (7,4); <br/> pushpath (current, 7,6 ); <br/> go (); <br/> pushpath (current,); <br/> go (); <br/>}< br/> break; <br/> case 8: <br/> If (Prior! = 5 & Prior! = 7) {<br/> pushpath (current, 8, 5); <br/> go (8, 5); <br/> pushpath (current, 8, 7 ); <br/> go (8, 7); <br/>}< br/> break; <br/> default: break; <br/>}</P> <p> // puch_back next point <br/> void pushpath (MapInfo & map, int front, int back) <br/> {<br/> int temp = map. A [Front] <br/> map. A [Front] = map. A [Back]; <br/> map. A [Back] = temp; <br/> // map. selfnum = MapInfo. back. selfnum + 1; <br/> // map. globnum = MapInfo. back. globnum + MapInfo. size (); <br/> MapInfo. push_back (MAP); <br/> swap (map. A [Front], map. A [Back]); <br/>}< br/> void main () <br/>{< br/> int I; <br/> MapInfo Info; <br/> for (I = 0; I <9; I ++) {<br/> info. A [I] = A [I]; <br/>}< br/> // info. selfnum = 0; <br/> // info. globnum = 0; <br/> MapInfo. push_back (Info); <br/> If (go (0, 8 )) {// If has arrived at the result <br/> // print the map of every step <br/> for (I = 0; I <MapInfo. size (); I ++) {<br/> for (j = 0; j <9; j ++) <br/> cout <MapInfo. A [I] <""; <br/> cout <Endl; <br/>}< br/>}

You can apply my functions to your application.

Modify the data structure as needed.

The main function and input are used only for instructions.

 

 

Related Article

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.