Enumeration of basic algorithms of the [ACM Training] Algorithm (POJ 1753+2965)

Source: Internet
Author: User

List the topics first:

1, POJ 1753

POJ 1753 Flip game:http://poj.org/problem?id=1753

Sample Input

Bwwbbbwbbwwbbwww

Sample Output

4

There is no train of thought, there are many kinds of situations to consider, but also can only use the enumeration method to solve it ~

4x4 array for data storage is certainly very inconvenient to operate, here borrowed bit compression method to store the state, using shift to identify each location of the position of the left and right operation. See here for details.

1, when the board status ID 0 (full white) or 65535 (All Black), the game ends, 0^1=1,1^1=0, so the flip operation can be done by XOR or operation, and the position of the flip can be determined by shifting.

2. END logo!!!!!

Sub-steps:

1. Identify status from input in place:

1     intState =0;2     Charch[4];3 4      while(cin>>ch)5     {6         for(intj =0; J <4; J + +)7         {8State = state<<1;9             if(Ch[j] = ='b')Tenstate + =1; One         } A}

2, from one state to the next state of the conversion, such as the corresponding position I was changed after the state:

The 16 data here may be unexpected by using code generation, but can be obtained by manually changing it once ~ ~ ~

1 intchange[ -] ={2      51200,58368,29184,12544,3      35968,20032,10016,4880,4      2248,1252,626,305,5       $, +, the, +6 };7 8 state = state^change[i];//corresponds to the change of position I9 Ten //The above 16 data is derived from the following code One  A intdir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; - voidInit () - { the     inti,j,x,y,t,temp; -      for(i=0;i<4;++i) -     { -          for(j=0;j<4;++j) +         { -temp =0; +Temp ^= (1<< (3-I.) *4+3-J));//The first row represents the high 4 bits of the 16-bit, and the first column also represents the high, so the board (I,J) is in the 16-bit position ((3-i) *4+3-j) A  at              for(t=0;t<4;++t) -             { -x = i + dir[t][0]; -y = j + dir[t][1]; -                 if(x<0|| y<0|| X>3|| Y>3) -                     Continue; inTemp ^= (1<< (3-X) *4+3-y)); -             } tocout<<temp<<" "; +         } -cout<<Endl; the     } *}

3, judge whether to meet the situation only need to judge state = = 0 | | state = = 65535.

4, solve the small problem, and then think about the big logic:

The initial state is a solid color directly output 0, the initial is not a solid color, flip a state is a solid color, then output 1, flip A will produce n state, 0<=n<=16, these states to put into the queue to save, and then from the queue and then the next rollover. The key question is under what circumstances to determine the end of the rollover, still no solid color appears, then the output impossible, the initial state of the rollover greater than 2 is removed from the queue, you must design a status ID in advance, indicating that the same state does not enter the queue again, then when the queue is empty can be concluded.

1 //you need to set an identity that has been processed, with 65,536 common states, 0-65535,2 //if the corresponding position is marked as 1, the description has already been processed and discarded for the next3 4 //such a traversal process is called the BFS process5 6 7 BOOLvisit[65536];8 9 intBFsintState//The return value is the number of steps made, impossible is-1Ten { OneQueue<node>Q; A Node Current,next; -  -Current.state =State ; theCurrent.step =0; - Q.push (current); -Visited[state] =true; -  +      while(!q.empty ()) -     { +Current =Q.front (); A Q.pop (); at  -         if(Current.state = =0|| Current.state = =65535) -             returnCurrent.step; -  -          for(inti =0;i< -; i++)//16 operations per state -         { inNext.state = current.state^Change[i]; -Next.step = current.step+1; to  +             if(Next.state = =0|| Next.state = =65535) -                 returnNext.step; the             Else *             { $                 if(Visited[next.state])Panax Notoginseng                     Continue; -                 Else the                 { +Visited[next.state] =true; A Q.push (next); the                 } +             } -         } $     } $     return-1; -}

Summary: Enumeration, the problem is to use the idea of enumeration to find the desired solution, especially the use of a queue to do a while loop to do.

The next similar topic is POJ's 2965 title: The Pilots ' refrigerator http://poj.org/problem?id=2965

Sample Input

-+-----------+--

Sample Output

61 11 31 44 14 34 4

Enumeration of basic algorithms of the [ACM Training] Algorithm (POJ 1753+2965)

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.