Va 10085 the most distant state

Source: Internet
Author: User

Uva_10085

This question is basically the same as that of Digital 8, except that the digital 8 problem is the minimum step required to change to a specified image. This problem is based on a specified image, find a graph and perform the following operations to get the most steps for the specified graph.

SlaveCodeFrom the perspective of octa digital While The loop exits when a specified image is encountered. While The loop can only be Front = rear In this way, all cases are searched.

For more information about the eight digital problems, refer toP132.

 # Include  <  Stdio. h  >  
# Include < String . H >
# Define Hash 100003
Int St [ 1000000 ] [ 9 ], Fa [ 1000000 ], Move [ 1000000 ];
Int Head [ 1000003 ], Next [ 1000000 ];
Int DX [] = { - 1 , 1 , 0 , 0 }, Dy [] = { 0 , 0 , - 1 , 1 };
Int Hash ( Int * P)
{
Int I, V;
V = 0 ;
For (I = 0 ; I < 9 ; I ++ )
V = V * 10 + P [I];
Return V % Hash;
}
Int Insert ( Int K)
{
Int I, h;
H = Hash (ST [k]);
For (I = Head [H]; I ! =- 1 ; I = Next [I])
If (Memcmp (ST [I], St [K], Sizeof (St [k]) = 0 )
Break ;
If (I =- 1 )
{
Next [k] = Head [H];
Head [H] = K;
Return 1 ;
}
Else
Return 0 ;
}
Void Printpath ( Int K)
{
If (Fa [k] =- 1 )
Return ;
Printpath (Fa [k]);
If (Move [k] = 0 )
Printf ( " U " );
Else If (Move [k] = 1 )
Printf ( " D " );
Else If (Move [k] = 2 )
Printf ( " L " );
Else
Printf ( " R " );
}
Int Main ()
{
Int I, J, K, temp, T, TT;
Int Front, rear, X, Y, Z, newx, newy, newz;
Scanf ( " % D " , & T );
For (TT = 0 ; TT < T; TT ++ )
{
Front = Rear = 0 ;
For (I = 0 ; I < 3 ; I ++ )
For (J = 0 ; J < 3 ; J ++ )
Scanf ( " % D " , & St [rear] [I * 3 + J]);
Fa [rear] = Move [rear] =- 1 ;
Memset (Head, - 1 , Sizeof (Head ));
Insert (rear );
Rear ++ ;
While (Front < Rear)
{
For (Z = 0 ; Z < 9 ; Z ++ )
If (St [Front] [Z] = 0 )
Break ;
X = Z / 3 ;
Y = Z % 3 ;
For (I = 0 ; I < 4 ; I ++ )
{
Memcpy (ST [rear], St [Front], Sizeof (St [Front]);
Newx = X + DX [I];
Newy = Y + Dy [I];
If (Newx > = 0 && Newx < 3 && Newy > = 0 && Newy < 3 )
{
Newz = Newx * 3 + Newy;
Temp = St [rear] [Z];
St [rear] [Z] = St [rear] [newz];
St [rear] [newz] = Temp;
If (Insert (rear ))
{
Fa [rear] = Front;
Move [rear] = I;
Rear ++ ;
}
}
}
Front ++ ;
}
Printf ( " Puzzle # % d \ n " , TT + 1 );
For (I = 0 ; I < 9 ; I ++ )
{
If (I % 3 ! = 0 )
Printf ( " " );
Printf ( " % D " , St [rear - 1 ] [I]);
If (I + 1 ) % 3 = 0 )
Printf ( " \ N " );
}
Printpath (Rear - 1 );
Printf ( " \ N " );
}
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.