Poj 1753 flip game

Source: Internet
Author: User

Poj_1753

After several days, we have already written this question 3rd Times. After being familiar with bit operations, we have solved this problem more efficiently.

At the same time, this question also has a little skill in enumeration. If we use brute force enumeration, once the checkerboard reaches 16*16, it will obviously not be enough (an Eastern European semi-finals question). In fact, we can only enumerate the first row of operations. Afterwards, if we want to turn all the pawns into a color, the operations in the second row will be fixed (because the status of the pawns in the first row restricts the flip of the pawns in the second row, if you want to turn the pawns in the first line into white, the position at the bottom of the black pawns in the second line must be flipped, and vice versa ), the operations on the third and fourth rows are obviously fixed.

In this way, even the 16*16 board has only 15*16*2 ^ 16 complexity, which is acceptable.

# Include <stdio. h>
# Include < String . H>
Int Fresh, steps, DX [] = { 0 ,-1 , 1 , 0 , 0 }, Dy [] = { 0 , 0 , 0 ,- 1 , 1 };
Void Init ()
{
Char Ch;
Fresh = 0 ;
For ( Int I = 0 ; I < 4 ; I ++)
{
For ( Int J = 0 ; J < 4 ; J ++)
Fresh = (fresh < 1 ) + (CH = getchar () = ' B ' ?1 : 0 );
Getchar ();
}
}
Void Flip ( Int X, Int Y, Int & St)
{
If (X> = 0 & X < 4 & Y> = 0 & Y <4 )
St ^ = 1 <(X * 4 + Y );
}
Void DFS ( Int Cur, Int Num, Int St, Int Flag)
{
If (Cur = 4 )
{
If (St = 0 xFFFF | ST = 0 )
Steps = num <steps? Num: steps;
Return ;
}
Int X, Y;
For ( Int I = cur- 1 , J = 0 ; J < 4 ; ++ J)
If (St &( 1 <(I * 4 + J)> (I * 4 + J) ^ flag)
{
For ( Int K = 0 ; K < 5 ; ++ K)
{
X = cur + dx [k];
Y = J + dy [k];
Flip (X, Y, St );
}
++ Num;
}
DFS (cur + 1 , Num, St, flag );
}
Int Solve ()
{
Steps = 0x7fffffff ;
For ( Int I = 0 ; I < 16 ; ++ I)
{
Int Num = 0 , St = fresh, X, Y;
For ( Int J = 0 ; J < 4 ; ++ J)
{
If (I &( 1 <J ))
{
For ( Int K = 0 ; K < 5 ; K ++)
{
X =0 + Dx [k];
Y = J + dy [k];
Flip (X, Y, St );
}
Num ++;
}
}
DFS ( 1 , Num, St, 0 );
DFS ( 1 , Num, St, 1 );
}
Return Steps = 0x7fffffff ? - 1 : Steps;
}
Int Main ()
{
Init ();
If (Solve ()! =- 1 )
Printf ( " % D \ n " , Steps );
Else
Printf ( " Impossible \ 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.