Topic
Title: Jumping Grasshopper
As shown in FIG p1.png:
There are nine plates, arranged in a circle.
Grasshoppers 8 wherein the filled tray 8, there is a blank disc.
We call these grasshoppers clockwise numbered from 1 to 8
Each grasshopper can jump to an adjacent empty dish,
can then point force, grasshoppers jump across a neighboring empty tray.
Please you do the math, if you want to make grasshoppers their formation changed in accordance with the counter-clockwise order,
and to maintain the same position a blank disc (that is, transposition 1-8, 2-7 transposition, ...), at least how many times after the jump?
Note: Requires submission is an integer, please do not fill out any extra content or captions.
Thinking
bfs
Code
1 #include<iostream> 2 #include<string.h> 3 #include<queue> 4 #include<stdlib.h> 5 #include<cmath> 6 #include<stdio.h> 7 #include<algorithm> 8 #define maxx 1000000000 9 using namespace std; 10 long long int str_t_int(char str[]){//str to int 11 long long int sum=0,ll=1; 12 for(int i=8;i>=0;i--){ 13 sum=sum+((str[i]-'0')*ll); 14 ll=ll*10; 15 } 16 return sum; 17 } 18 19 int find_9(char str[]){ 20 int loc_9; 21 for(int i=0;i<10;i++){ 22 if(str[i]=='9') Loc_9 = I; // find subscript a blank disc 23 is } 24 return loc_9; 25 } 26 is 27 void int_t_str ( int NUM, char * STR) { 28 int I = . 8 ; 29 the while (NUM% 10 ) { 30 * (STR + I) = ( char ) (NUM% 10 + ' 0 ' ); 31 is NUM / = 10 ; 32 i-- ; 33 is } 34 is return ; 35 } 36 int the dir [ . 4 ] = {- 2 , - . 1 , . 1 , 2 }; // hopping scheme 37 [ BOOL index [Maxx]; // if it has been determined in each case marking 38 is 39 int BFS ( ) { 40 Long Long int Start = 123456789 ; 41 is Long Long int End = 876 543 219 ; 42 is Queue < int > Q; 43 is Queue < int >QC; 44 is q.push (Start); 45 qc.push ( . 1 ); 46 is char STR [ 10 ]; 47 Long Long int TEMP, loc_9, temp_char, CNT; 48 int Find = 0 ; 49 index [Start] = . 1 ; 50 CNT = . 1 ; 51 is the while ( . 1 ) { 52 is TEMP = q.front (); // get the current q queue header 53 is CNT = qc.front (); // get the current qc queue header 54 is 55 int_t_str (TEMP, STR); // int to STR 56 is 57 is loc_9 = find_9 (STR); // check position 9 58 59 for ( int I = 0 ; I < . 4 ; I ++) { // begin by one trial 60 61 is STR = temp_char [loc_9]; // analog skip 62 is STR [loc_9] STR = [(+ loc_9 the dir [I] + . 9 )% . 9 ]; 63 is STR [(+ loc_9 the dir [I] + . 9 )% . 9 ] = temp_char; 64 65 66 TEMP = str_t_int (STR);// STR to int 67 68 69 70 IF (! Index [TEMP]) { // if repeated 71 is 72 IF (TEMP == End) { // find the queue 73 is return CNT; 74 } the else { // mark temp, while temp is pressed into the queue 75 index [temp] = . 1 ; 76 q.push (temp); 77 qc.push (CNT + . 1 ); 78 } 79 } 80 81 STR = temp_char [loc_9]; // return situ, exploratory other directions 82 STR [loc_9] STR = [(+ loc_9 the dir [I] + . 9 )% . 9 ]; 83 STR [(+ loc_9 the dir [I] + . 9 )% . 9 ] = temp_char; 84 } 85 q.pop (); // Q, pop queue QC 86 qc.pop (); 87 88 } 89 90 } 91 is int main () { 92 COUT << BFS ( ) << endl; 93 94 }