BFS-jiugongge rescheduling (detailed explanation) and bfs-jiugong rescheduling
BFS has not practiced any questions in the past two years. Today, I will recall the old blue bridge cup:Jiugongge rescheduling
Sample Input
123456780 // initial status 123046758 // end status
Sample output
3 // minimum steps
Sample Input
135246780 // initial status
467581230 // end state
Sample output
22 // minimum steps
Ideas
For example, if the space is 0, you can go to the upper, lower, and left steps 4:
The current 0 is located at (), so it can be moved to (1-), (+ 1), (1 +), (-1)
Then, you can check whether the Moving position has passed, whether it has reached the end, and whether it has exceeded the border.
Contos
It indicates the ranking currently arranged in the full arrangement of n different elements.
Formula:
X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[ 2]*1!+a[1]*0!
//a[i] is the first few elements (not starting from 0) that are not currently present.
For example, 3124 is1 ~ 4. Which of the following statements are arranged in full order?(Starting from 0, for example, 1234 is 1 ~ 4. 0th in full order)
X=2*3!+ 0*2!+ 0*1!+ 0*0!=12
- First,3Only two numbers (1 and 2) are smaller than the existing element (1, 2, 3, 4), and the number of digits is 2 to 2 ~ 4. There are 3 in a full arrangement! So = 2*3!
- First3, Second place1, 1 in the unappearing element (1, 2, 4), only 0 is smaller than it, 3 ~ 4. There are 2 in a full arrangement! So = 0*2!
- First3, Second place1, Third place2, 2 in the undisplayed element (2, 4), only 0 is smaller than it, So = 0*1!
- First 3, second1, Third place2, Fourth place4, 4 in the undisplayed element (4), only 0 is smaller than it, So = 0*0!
Contos code (s [4] = {3, 1, 2, 4}, n = 4)
Long int fac[10]={1,1,2,6,24,120,720,5040,40320,362880};//multiplier
Int congtuo(int s[], int n)
{
Int i,j,temp,num;
Num=0;
For(i=0;i<n;i++)
{
Temp=0;
For(int j=i+1;j<n;j++)
If(s[j]<s[i]) //determine a few numbers less than it
Temp++;
Num+=fac[n-i-1]*temp;
}
Return num;
}
The Code of jiugongge is as follows:
#include <stdio.h>
#include <string.h>
Long int fac[10]={1,1,2,6,24,120,720,5040,40320,362880};//multiplier
Unsigned char step[363880]={0}; // 9!=362880, so the array is bigger
Char method[4][2]={{-1,0},{0,1},{1,0},{0,-1}}; //4 kinds of moving methods
Struct node_t{
Unsigned char data[9];
};
Struct node_t save_step[363880]; //Save mobile data, up to 9! mobile data
Unsigned char end_p[9]; //end position
Int congtuo(unsigned char s[], int n)
{
Int i,j,temp,num;
Num=0;
For(i=0;i<n;i++)
{
Temp=0;
For(int j=i+1;j<n;j++)
If(s[j]<s[i]) //determine a few numbers less than it
Temp++;
Num+=fac[n-i-1]*temp;
}
Return num;
}
/* handle 0 function
* Return value 0: The exchanged position is illegal, or has been passed
*Return value 1: not passed
* Return value 2: Find the end point
*/
Int handler_zero(int i,unsigned char data[])
{
Int x,y,temp,num;
For(int j=0;j<9;j++)
If(data[j]==0)
{
/*[x,y]: Location to be exchanged*/
x=j%3+method[i][0];
y=j/3+method[i][1];
/* Determine if the swap location is out of bounds*/
If(x<0||x>2)
Return 0;
Else if(y<0||y>2)
Return 0;
/* Determine if the position after the exchange has been passed */
Data[j]= data[x+y*3];
Data[x+y*3]=0;
Num=congtuo(data,9);
If(step[num]==1) //has been walked
{
Return 0;
}
If(memcmp(end_p,data,9)==0) //find the end point
{
Return 2;
}
Step[num]=1;
Return 1;
}
}
Int start=0, end=0;
Int bfs(void)
{
Int x,y;
Int next_end=end; //Save the flag of the next position
Int err;
Unsigned char temp[9];
For(;start<=end;start++)
{
For(int i=0;i<4;i++) //4 kinds of moves
{
Memcpy(temp,(unsigned char *)save_step[start].data,9);
Err=handler_zero(i,temp); //Process 0 in the data
If(err) //Save data
{
Memcpy((unsigned char *)save_step[++next_end].data,temp,9);
If(err==2) //find the end point
Return 1;
}
}
}
Start=end+1;
End=next_end;
Return (1+bfs());
}
Int main()
{
Int step_num=0; //steps
Unsigned char s[9];
Printf("please enter start point:\r\n");
For(int i=0;i<9;i++)
{
Scanf("%1d",&save_step[0].data[i]);
}
Printf("please enter end point:\r\n");
For(int i=0;i<9;i++)
{
Scanf("%1d",&end_p[i]);
}
Step_num=bfs();
Printf("%d\n",step_num);
}
What should I do if I want to see the actual number of steps?
In fact, it is very simple, because the specific content is saved in save_step [], we only need to define a flag step_p in the node_t struct to point to the location of the last step.
The Code is as follows:
#include <stdio.h>
#include <string.h>
Long int fac[10]={1,1,2,6,24,120,720,5040,40320,362880};//multiplier
Unsigned char step[363880]={0}; // 9!=362880, so the array is bigger
Char method[4][2]={{-1,0},{0,1},{1,0},{0,-1}}; //4 kinds of moving methods
Struct node_t{
Unsigned char data[9];
Unsigned int step_p;
};
Struct node_t save_step[363880]; //Save mobile data, up to 9! mobile data
Unsigned char end_p[9]; //End point coordinates
Unsigned int end_index; / / record the end position
Int congtuo(unsigned char s[], int n)
{
Int i,j,temp,num;
Num=0;
For(i=0;i<n;i++)
{
Temp=0;
For(int j=i+1;j<n;j++)
If(s[j]<s[i]) //determine a few numbers less than it
Temp++;
Num+=fac[n-i-1]*temp;
}
Return num;
}
/* handle 0 function
* Return value 0: The exchanged position is illegal, or has been passed
*Return value 1: not passed
* Return value 2: Find the end point
*/
Int handler_zero(int i,unsigned char data[])
{
Int x,y,temp,num;
For(int j=0;j<9;j++)
If(data[j]==0)
{
/*[x,y]: Location to be exchanged*/
x=j%3+method[i][0];
y=j/3+method[i][1];
/* Determine if the swap location is out of bounds*/
If(x<0||x>2)
Return 0;
Else if(y<0||y>2)
Return 0;
/* Determine if the position after the exchange has been passed */
Data[j]= data[x+y*3];
Data[x+y*3]=0;
Num=congtuo(data,9);
If(step[num]==1) //has been walked
{
Return 0;
}
If(memcmp(end_p,data,9)==0) //find the end point
{
Return 2;
}
Step[num]=1;
Return 1;
}
}
Int start=0, end=0;
Int bfs(void)
{
Int x,y;
Int next_end=end; //Store the end flag of the next bfs
Int err;
Unsigned char temp[9];
For(;start<=end;start++)
{
For(int i=0;i<4;i++) //4 kinds of moves
{
Memcpy(temp,(unsigned char *)save_step[start].data,9);
Err=handler_zero(i,temp); //Process 0 in the data
If(err) //Save data
{
Memcpy((unsigned char *)save_step[++next_end].data,temp,9);
Save_step[next_end].step_p=start; //Record the last step position.
If(err==2) //find the end point
{
End_index=next_end; // Record the position of the end step.
Return 1;
}
}
}
}
Start=end+1;
End=next_end;
Return (1+bfs());
}
Int main()
{
Int step_num=0; //steps
Unsigned char s[9];
Printf("please enter start point:\r\n");
For(int i=0;i<9;i++)
{
Scanf("%1d",&save_step[0].data[i]);
}
Printf("please enter end point:\r\n");
For(int i=0;i<9;i++)
{
Scanf("%1d",&end_p[i]);
}
Step_num=bfs();
Printf("%d\n",step_num);
/** Print step details **/
For(;step_num>=0;step_num--)
{
Printf("**step %d********\n",step_num);
For(int i=0;i<3;i++)
{
Printf("%d %d %d\n", save_step[end_index].data[i*3],
Save_step[end_index].data[i*3+1],
Save_step[end_index].data[i*3+2]);
}
End_index=save_step[end_index].step_p;
Printf("\n");
}
}
Print results