Poj 1077 Digital 8

Source: Internet
Author: User

Poj 1077 Digital 8

This topic took me one night to read a solution. The final result was a tragedy. I only passed it on poj. On hdu, TLE is because hdu contains multiple groups of data.

Data, and poj is a group of data... Unfortunately, the learning method is a little inefficient...

However, the print path method can be used for reference to recursively print the path from the end point to the start point...

Code:

 

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
Using namespace std; # define N 371_int first [9]; int last [9] = {1, 2, 4, 5, 6, 7, 8, 0}; int f [9]; int visit [N]; int p [N] [9]; // an array of saved status int dist [N]; // record path const int dx [] = {-,}; const int dy [] = {0,-, 0 }; char dir [4] = {'D', 'R', 'l', 'U'}; void init () // It seems to be a magic horse Dongdong {f [0] = 1; for (int I = 1; I <10; I ++) f [I] = f [I-1] * I;} int hash1 (int * a) // This is the number of digits 0-9 in the full arrangement, used to determine the {int I, j; int s = 0; for (I = 0; I <9; I ++) {int cnt = 0; for (j = I + 1; j <9; j ++) {if (a [j] <a [I]) cnt ++ ;} s + = f [8-i] * cnt;} return s;} int bfs (int fir, int las) {int I, cur; memset (visit, 0, sizeof (visit); memset (dist, 0, sizeof (dist); memset (p, 0, sizeof (p); visit [fir] = 1; dist [fir] = 0; for (I = 0; I <9; I ++) p [fir] [I] = first [I]; queue
     
      
Que; que. push (fir); while (! Que. empty () {int temp = que. front (); que. pop (); if (temp = las) return dist [las]; for (cur = 0; cur <9; cur ++) {if (p [temp] [cur] = 0) break;} int x = cur/3; int y = cur % 3; int change [9]; for (I = 0; I <4; I ++) {int xx = x + dx [I]; int yy = y + dy [I]; if (xx> = 0) & (yy> = 0) & (xx <3) & (yy <3) {memcpy (change, p [temp], sizeof (change); change [cur] = p [temp] [xx * 3 + yy]; change [xx * 3 + yy] = 0; int change1 = hash1 (change); if (visit [change1] = 0) {visit [change1] = 1; dist [change1] = dist [temp] + 1; que. push (change1); memcpy (p [change1], change, sizeof (change) ;}}} return-1 ;}void print_path (int fir, int las) // recursively print the path from the end point {if (las = fir) return; int cur; int change [9]; for (cur = 0; cur <9; cur ++) {if (p [las] [cur] = 0) break;} int x = cur/3; int y = cur % 3; for (int I = 0; I <4; I ++) {int xx = x + dx [I]; int yy = y + dy [I]; if (xx> = 0) & (yy> = 0) & (xx <3) & (yy <3) {memcpy (change, p [las], sizeof (change )); change [cur] = p [las] [xx * 3 + yy]; change [xx * 3 + yy] = 0; int change1 = hash1 (change ); if (dist [las] = dist [change1] + 1) & visit [change1]) {print_path (fir, change1); printf (% c, dir [I]); break ;}}return ;}int main () {init (); int I, k = 0; char a [30]; while (gets (a) {// getchar (); k = 0; int len = strlen (a); for (I = 0; I
      
        = '1') & (a [I] <= '8') first [k ++] = a [I]-'0 '; else if (a [I] = 'X') first [k ++] = 0 ;}// for (I = 0; I <9; I ++) // printf (% d, first [I]); puts (); int fir = hash1 (first ); // find the fully arranged bits corresponding to a sequence. int las = hash1 (last); int ans = bfs (fir, las); // printf (% d, ans); if (ans <0) printf (unsolvable); elseprint_path (fir, las); puts ();} return 0 ;}
      
     
    
   
  
 


 

 

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.