Solution to the three-color flag Problem

Source: Internet
Author: User

Solution to the three-color flag Problem
Three flag problems

1. Problem Origin

The problem of three-color flag was first caused by E. w. according to Dijkstra, he uses DutchNation Flag (Dijkstra is Dutch), while most authors use Three-Color Flag.

Suppose there is a rope with red, white, and blue flags on it. The flag colors on the rope are not sequential at first, and you want to classify them, the columns are blue, white, and red. The minimum number of moves is required. Note that you can only perform this operation on the rope, and only two flags can be changed at a time.

 

2. solution:

Three colors of the flag, white center, blue beginning, Red end, if you want to move the least number of times, you need to do is to move red to the back, blue to move to the front, try not to move white in the middle.

The algorithm flow is to use three beacon B, w, r to point to different flag respectively. B points to the first non-Blue flag at the end of the Blue flag that is consecutively arranged from 0, and r points to the first non-Red flag that is consecutively arranged from the last sequence number. For example: bbrwbbrr, B points to r with the serial number 2, and r points to B with the serial number being the last three. W serves as a pointer that can be moved.

When w points to the White flag, w continues to move forward; when w points to the Blue flag, it is necessary to interact the flag referred to by B with the Blue Flag referred to by w. Similarly, when the flag referred to by w is Red, the Red Flag referred to by w needs to be exchanged with the flag referred to by r.

Is the position pointed to by the three beacon at the start.


3. Program Implementation

#include <string>#include <iostream>using namespace std; int dutchFlag(string& str);void swap(string& str,intx,int y); void main(){         cout<<"Pleaseinput the dutch flags"<<endl;         string str;         cin>>str;         intnTimes = dutchFlag(str);         cout<<str<<endl; }int dutchFlag(string& str){         intnLength = str.length();         intfBlue = 0;         intfWhite = 0;         intfRed = nLength-1;         intnCnt = 0;         while(fWhite <= fRed )         {                   if(str[fWhite]== 'w')                            fWhite++;                   elseif(str[fWhite] == 'b')                   {                            if( fWhite != fBlue )                                     swap(str,fWhite,fBlue);                            fWhite++;                            fBlue++;                            nCnt++;                    }                   else                   {                            // use fWhite < fRed to avoid 'wr':w point to r,and rpoint to r,then swap and error                            while(str[fRed] == 'r'&& fWhite < fRed)                                     fRed--;                            if( fWhite != fRed )                                     swap(str,fWhite,fRed);                            fRed--;                            nCnt++;                   }         }         returnnCnt;} void swap(string &str,intx,int y){         chartmp;         tmp = str[x];         str[x] = str[y];         str[y] = tmp;         cout<<x<<" swaps with "<<y<<endl;}

4. Details

4.1 When w and B point to the same position, if the fWhite condition is not added! = FBlue, it will create its own exchange. Similarly, when w and r both point to the last position, if the two are equal without a limit condition, it will also cause exchange between itself and itself.

4.2

While (str [fRed] = 'R' & fWhite <fRed)

This is mainly to prevent wr, for example. In this case, w points to r, r starts to point to r, w points to the same position as r, and then r --, then w and r are switched to rw, which leads to an error.

With the restriction fWhite <fRed, r will not move forward at this time, so there will be no exchange, thus avoiding errors.


Programming to solve the three-color flag Problem

Added: it has been changed for you.

In addition, the number of statistics has come out. What is the difficulty in sorting? There are several outputs. The program below is well arranged.

Many errors:
-----------------------------------------------------------------------
# Include <string. h>
# Define BLUE 'B'
# Define WHITE 'W'
# Define RED 'R'
Void dutch_flag (char * color)
{
Int white = 0, blue = 0, red = strlen (color)-1;
Char temp;
While (white <= red)
{
If (color [white] = WHITE) white ++;
Else if (color [white] = BLUE)
{
Temp = color [white];
Color [white] = color [blue];
Color [blue] = temp;
Blue ++;
White ++;
}
Else
{
While (white <red & color [red] = RED) red --;
Temp = color [red];
... The remaining full text>

Historical issues-in the Russian revolution, the Red Flag replaced the three-color flag

Because every change pursues democracy. Before the February revolution, the system was a feudal system. After the revolution, capitalism or socialism could be developed. This was decided by the leaders who seize the regime. At least I think so. The red flag is the Soviet flag socialism, and the three-color flag is the Russian flag capitalism. The flag is changed only when the Soviet Union is dissolved and the regime has changed.

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.