Monday, October 17, 2011

IARCS Problem : A Board Game

/*This program is a solution to the question
 * given by IARCS at their website.
 *
 * The link to the question is:http://www.iarcs.org.in/inoi/contests/oct2004/Basic-2.php
 *
 *Note that this solution provided below
 *is in Java and not in C or C++
 */
import java.util.*;
class BoardGame
{
public static void main()
{
    System.out.println("Enter the required info : ");
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int m=sc.nextInt();
    char mark[]=new char[n];
    int i;
    String s;
    for(i=0;i<n;i++)
    {
        s=sc.next();
        mark[i]=s.charAt(0);
    }
   
    int v,pos=0,k=1;
    for(i=0;i<m;i++)
    {
        v=sc.nextInt();
        if(mark[pos]=='+')
        {
            if((pos+1+v)<=n)
            {
                pos=pos+v;
            }
        }
        else if(mark[pos]=='-')
        {
            if((pos+1-v)>=1)
            {
                pos=pos-v;
            }
        }
        if(pos==0)
        {
            k++;
        }
    }
    System.out.println(k);
}
}

//Author : Mayank Rajoria

No comments:

Post a Comment