Pages

Related Posts with Thumbnails
Google CodeJam 2011–Bot Trust Solved–C Programming >> Codes-N-Tricks
Content feed Comments Feed

Google CodeJam 2011–Bot Trust Solved–C Programming

Posted by Soham Sunday, May 8, 2011

Problem is Bot Trust on Google CodeJam 2011.
I have solved the problem in C-language. The algorithm is pretty simple. If anyone want to know in details,please let me know by commenting on this post, I’ll post the details.

Here’s the C-Code:

Bot Trust Problem
    #include<stdio.h>
    #include<string.h>
    int search_pos(char seq[],int rbt,int indx)
    //Searches the next position of 'rbt' Robot
    //after 'index' in the sequence
    {
        int i,val=-1;char robot;
        if(rbt==1) robot='O'; else robot='B';
        for(i=indx;seq[i]!='\0';i++)
            if(robot==seq[i])
            {
                 val=i;
                 break;
            }
        return val;
    }
    
    int main()
    {
        int i,t,n,sq,timer,cnt=0,bt[10],inr;
        char r[10];
        int o_frst,b_frst,o_cur,b_cur,o_targt,b_targt;
        
        scanf("%d",&t);
        
        while(t--)
        {
                  timer=0,inr=0;
                  o_cur=1,b_cur=1,o_targt=1,b_targt=1;    
                  scanf("%d",&n);
                  for(i=0;i<n;i++)
                  {
                       scanf(" %c %d",&r[i],&bt[i]);
                  }
                  
                  r[i]='\0';bt[i]='\0';
                  o_frst=search_pos(r,1,0);
                  b_frst=search_pos(r,2,0);
                  if(o_frst!=-1) o_targt=bt[o_frst];
                  if(b_frst!=-1) b_targt=bt[b_frst];
                  
                  for(sq=0;sq<strlen(r);)
                  {
                       //For Orange Robot
                       if(o_targt>o_cur)
                            o_cur++;
                       else if(o_targt<o_cur)
                            o_cur--;
                       else if(o_targt==o_cur)
                       {
                                 if(r[sq]!='B')
                                 {
                                      o_targt=bt[search_pos(r,1,sq+1)];
                                      inr=1;
                                 }
                       }
                       //For Blue Robot
                       if(b_targt>b_cur)
                            b_cur++;
                       else if(b_targt<b_cur)
                            b_cur--;
                       else if(b_targt==b_cur)
                       {
                                 if(r[sq]!='O')
                                 {
                                      b_targt=bt[search_pos(r,2,sq+1)];
                                      inr=1;
                                 }
                       } 
                       timer++; 
                       
                       if(inr) sq++;//checking if the current button is pushed or not
                       inr=0;                          
                  }
                  printf("Case #%d: %d\n",++cnt,timer);
        }
        getchar();
    }
Thank you.

    1 Response to Google CodeJam 2011–Bot Trust Solved–C Programming

    1. Anonymous Says:
    2. Very nice coding.I am new in codejam.I think this will help me a lot.

      Dedarul Islam/Bangladesh

       

    Post a Comment

    SyntaxHighlighter

    E-Mail Subscription

    Enter your email address:

    Subscribe & get Regular E-Mail Updates From Codes-N-Tricks.
    Delivered by FeedBurner

    Share with Orkut

    Loading

    Recent Posts

    About Us