HDU 1043, POJ 1077 Eight

Source: Internet
Author: User

Eight Digital Problems

InputYou will receive, several descriptions of configuration of the 8 puzzle. one description is just a list of the tiles in their initial positions, with the rows listed from top to bottom, and the tiles listed from left to right within a row, where the tiles are represented by numbers 1 to 8, plus 'x '. for example, this puzzle

1 2 3
X 4 6
7 5 8

Is described by this list:

1 2 3x4 6 7 5 8

OutputYou will print to standard output either the word ''unsolvable'', if the puzzle has no solution, or a string consisting entirely of the letters 'R', 'l ', 'U' and 'D' that describes a series of moves that produce a solution. the string shoshould include no spaces and start at the beginning of the line. do not print a blank line between cases.

Sample Input
2  3  4  1  5  x  7  6  8

Sample Output
ullddrurdllurdruldr


I. How to store and represent data?

1. An integer can be used to represent the status of octal digital. x can be regarded as "9" and "0" is not easy to handle. For example, the final state 12345678x can be expressed as an integer of 123456789,

2. Use Arrays for storage. Here, x can be expressed as 0 or 9.

Ii. How to expand data?

1. For an integer of the int type, data expansion is the processing of this number. For example, for an integer

1 2 3 1 2 3

4 5 6 4 5 9

7 8 9 x the status of moving one digit up is 7 8 6, that is, the integer 123459786.

2. For arrays, data expansion regards the array as a two-dimensional array, processing the rows and columns of the Two-dimensional array.

3. How to record the path?

1. Construct an adjacent table, chain or continuous storage, record the parent node and all child nodes of each node, and output the Shortest Path recursively from the target node, this implementation method is strongly recommended because it does not consume a large amount of memory records. For specific applications, see

LeetCode OJ: detailed process of solving the problem of Word Ladder II

2. Because this question only records udlr, you can use string to record it. However, it is not recommended because the path stored in each path is too memory-consuming.

4. How to Determine the importance?

1. For int-type arrays, You can construct a large array to record whether to access it. For example, vis [123456789] indicates whether to access status 123456789. Advantage: fast. Disadvantage: a large amount of data is wasted, and the int type size is limited.

2. For arrays, you can convert the array to the int type before performing operations.

3. Use map to save and avoid waste

4. Extended judgment with Kanto

5. How to establish status transfer?

1. The simplest thing is the wide search,

(1) forward wide search

(2) Extensive reverse search, recording all possible points, and storing all paths with a string [] large array. This method is applicable when many input data entries exist, because the extensive search is only performed once, the memory consumption is too large.

2. Bidirectional wide search

3. A * Algorithm

4. IDA * Algorithm


Here is a bi- + + map + int storage data version. The answer is incorrect and is for reference only.

#include 
 
  #include 
  using namespace std;typedef struct{int x,w;char s;}aaa;aaa dui[500000],du[500000];char www[500000];int fang[4][2]={1,0,-1,0,0,1,0,-1};int jin[9]={100000000,10000000,1000000,100000,10000,1000,100,10,1};map
   
     a;char w[3][3];int main(){freopen("C:\\in.txt","r",stdin);for(int i=0;i<3;i++)for(int j=0;j<3;j++)cin>>w[i][j];int many,ji,ji1,zan,zan2,x,y,tt,ww,ta,wa,q;many=0;char w2[9];for(int i=0;i<3;i++)for(int j=0;j<3;j++)w2[many++]=w[i][j];many=0;for(int i=0;i<3;i++)for(int j=0;j
    
     =0&&y+fang[i][1]<3&&y+fang[i][1]>=0&&neng==false){s=dui[tt].w;zan=s/jin[ji];zan2=s/jin[(x+fang[i][0])*3+(y+fang[i][1])];zan=zan%10;zan2=zan2%10;s=s-zan*jin[ji]-zan2*jin[(x+fang[i][0])*3+(y+fang[i][1])];s=s+zan2*jin[ji]+zan*jin[(x+fang[i][0])*3+(y+fang[i][1])];if(a[s]!=1){if(a[s]==2)neng=true;a[s]=1;ww++;dui[ww].w=s;dui[ww].x=tt;if(i==0)dui[ww].s='d';if(i==1)dui[ww].s='u';if(i==2)dui[ww].s='r';if(i==3)dui[ww].s='l';}}x=ji1/3;y=ji1%3;if(x+fang[i][0]<3&&x+fang[i][0]>=0&&y+fang[i][1]<3&&y+fang[i][1]>=0&&neng==false){s=du[ta].w;zan=s/jin[ji1];zan2=s/jin[(x+fang[i][0])*3+(y+fang[i][1])];zan=zan%10;zan2=zan2%10;s=s-zan*jin[ji1]-zan2*jin[(x+fang[i][0])*3+(y+fang[i][1])];s=s+zan2*jin[ji1]+zan*jin[(x+fang[i][0])*3+(y+fang[i][1])];if(a[s]==0){a[s]=2;wa++;du[wa].w=s;du[wa].x=ta;if(i==0)du[wa].s='u';if(i==1)du[wa].s='d';if(i==2)du[wa].s='l';if(i==3)du[wa].s='r';}}}ta++;tt++;}ji=0;q=ww;while(q!=1){ji++;www[ji]=dui[q].s;q=dui[q].x;}for(int i=ji;i>=1;i--)printf("%c",www[i]);ji=0;q=1;while(du[q].w!=dui[ww].w)q++;while(q!=1){ji++;www[ji]=du[q].s;q=du[q].x;}for(int i=1;i<=ji;i++)printf("%c",www[i]);printf("\n");}elseprintf("unsolvable\n");return 0;}
    
   
 

Then we provide a reverse wide search version + Kanto version.

/* HDU 1043 Eight train of thought: reverse search, and use the consortium to determine the corresponding path from the target status retrieval status */# include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
Using namespace std; const int MAXN = 1000000; // a maximum of 9! /2int fac [] = {24,120,720,504, 362880,}; // kangtuo unfold judgment weight // 0! 1! 2! 3! 4! 5! 6! 7! 8! 9! Bool vis [MAXN]; // mark string path [MAXN]; // record path int cantor (int s []) // Kang Tuo calculates the hash value of the sequence {int sum = 0; for (int I = 0; I <9; I ++) {int num = 0; for (int j = I + 1; j <9; j ++) if (s [j]
      
        Q; q. push (cur); path [aim] = ""; while (! Q. empty () {cur = q. front (); q. pop (); int x = cur. loc/3; int y = cur. loc % 3; for (int I = 0; I <4; I ++) {int tx = x + dir [I] [0]; int ty = y + dir [I] [1]; if (tx <0 | tx> 2 | ty <0 | ty> 2) continue; next = cur; next. loc = tx * 3 + ty; next. s [cur. loc] = next. s [next. loc]; next. s [next. loc] = 0; next. status = cantor (next. s); if (! Vis [next. status]) {vis [next. status] = true; next. path = indexs [I] + next. path; q. push (next); path [next. status] = next. path ;}}} int main () {freopen ("C: \ in.txt", "r", stdin); char ch; Node cur; bfs (); while (cin> ch) {if (ch = 'X') {cur. s [0] = 0; cur. loc = 0;} else cur. s [0] = ch-'0'; for (int I = 1; I <9; I ++) {cin> ch; if (ch = 'X') {cur. s [I] = 0; cur. loc = I;} else cur. s [I] = ch-'0';} cur. status = cantor (cur. s); if (vis [cur. status]) {cout <
       
        

Finally, A new version of A * + conto is provided.

# Include
         
          
# Include
          
           
# Include
           
            
# Include
            
             
# Define deusing namespace std; const int MAXN = 1000000; int fac [] = {24,120,720,504, 362880,}; // kangtuo unfold judgment weight // 0! 1! 2! 3! 4! 5! 6! 7! 8! 9! Bool vis [MAXN]; // mark string path; // record the final path int dir [4] [2] = {-}, {}, {0, -1 },{ 0, 1 }}; // returns the direction vector char indexs [5] = "udlr"; // returns the struct Node {int data [9]; int f, g, h; int loc; // set "x" to 0 int status; // string path of the hash value expanded by Kang Tuo; // path bool operator = (const Node & t) {return status = t. status;} bool operator <(const Node & t) const {return f> t. f ;}} start, goal; // start and end point int Cantor (int s []) // calculate the hash value of the sequence {int sum = 0; for (int I = 0; I <9; I ++) {int num = 0; for (int j = I + 1; j <9; j ++) if (s [j]
             
              
Q; q. push (start); while (! Q. empty () {cur = q. top (); q. pop (); if (cur = goal) {path = cur. path; return true;} int x = cur. loc/3; int y = cur. loc % 3; for (int I = 0; I <4; I ++) {int tx = x + dir [I] [0]; int ty = y + dir [I] [1]; if (tx <0 | tx> 2 | ty <0 | ty> 2) continue; next = cur; next. loc = tx * 3 + ty; next. data [cur. loc] = next. data [next. loc]; next. data [next. loc] = 0; next. status = Cantor (next. data); if (! Vis [next. status]) {vis [next. status] = true; next. path = next. path + indexs [I]; if (next = goal) {path = next. path; return true;} next. g ++; // g value next. f = Fvalue (next, goal, 1); // f value q. push (next) ;}}return false;} int main () {freopen ("C: \ in.txt", "r", stdin); char ch; // startfor (int I = 0; I <8; I ++) goal. data [I] = I + 1; goal. data [8] = 0; goal. status = 46234; // The hash Value of the Kang Tuo extension corresponding to 123456780 // end while (cin> ch) {// start Node initialization start if (ch = 'X') {start. data [0] = 0; start. loc = 0;} else start. data [0] = ch-'0'; for (int I = 1; I <9; I ++) {cin> ch; if (ch = 'X') {start. data [I] = 0; start. loc = I;} else start. data [I] = ch-'0';} start. status = Cantor (start. data); // Kang Tuo hash value start. g = 0; start. f = Fvalue (start, goal, 1); // calculate the f value // end if (Astar () {cout <
              
               


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.