Codeforces 74b train greedy and simple Simulation

Source: Internet
Author: User

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;}

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.