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.
Solution to the three-color flag Problem