Question: The conductor wants to escape the ticket. The conductor patrols back and forth in the train. The starting point is CP, and one carriage is moved every minute. The person who escapes the ticket can enter any adjacent carriage or does not move. When the train stops, the pass leader gets off the bus. The new pass leader chooses a carriage to enter and asks if he can catch the person and when to catch it.
Practice: greedy. If a conductor faces a person who does not pass the ticket, the person who does not pass the ticket will run in the same direction. Otherwise, the person will remain unchanged. Generally, it can be placed on the back of the conductor, but there are special cases (in the program ). The conductor will turn around and change the direction (to start writing something like an imaging cyclic linked list ...). Just simulate this small-scale question.
#include <iostream>#include<cstdio>#include<cstring>#include<cmath>using namespace std;char mov[200];int cp,sp,dre,n;bool test(void){ for(int i=sp;i!=n-1&&i!=0;i=(i+dre+n)%n) if(i==cp)return 1; return 0;}int main(void){ bool yes=0; char ord[15]; scanf("%d%d%d",&n,&sp,&cp);getchar(); gets(ord); if(strcmp(ord,"to head")==0)dre=-1; else dre=1; scanf("%s",mov); if(cp==sp) { printf("Controller 0\n"); return 0; } cp--;sp--; if(test())yes=1; else yes=0; for(int i=0;mov[i];i++) { if(mov[i]=='1') { cp=(cp+dre+n)%n; if(cp==0||cp==n-1)dre=-dre; if(dre==-1&&cp==n-1)sp=0; else if(dre==1&&cp==0)sp=n-1; else sp=(cp-dre+n)%n; if(test())yes=1; else yes=0; continue; } cp=(cp+dre+n)%n; if(cp==0||cp==n-1)dre=-dre; if(cp==0&&dre==1||cp==n-1&&dre==-1) { if(test())yes=1; else yes=0; } if(yes)continue; if(sp!=0&&sp!=n-1)sp=(cp+dre+n)%n; if(cp==sp) { printf("Controller %d\n",i+1); return 0; } } printf("Stowaway\n"); return 0;}