ZIGZAG

ZIGZAG

#include<iostream>
using namespace std;
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    unsigned long long int x=0,y=0;
    unsigned long long int total=0;
    for(int i=0;i<k;i++)
    {
     char temp;
     cin>>temp;
    
     unsigned long long int res,len,test;
     if(temp=='U')x=x-1;
     if(temp=='D')x=x+1;
     if(temp=='L')y=y-1;
     if(temp=='R')y=y+1;
     if((x+y)&1)
     {
         if((x+y)<n)
         {
          res = (x+y)*(x+y+1)/2 + 1;
          res = res + x;
         
          total+=res;           
         }
         else
         {
              res = (x+y)*(x+y+1)/2 - ((x+y-n)*(x+y-n+1)) + 1;
              res = res + n -1 - y;
            
             total+=res;
         }    
     }
     else
     {
        
         if((x+y)<n)
         {
          res = (x+y)*(x+y+1)/2 + 1;
          res = res + y;
         
          total+=res;           
         
         }
         else
         {
            
            
              res = (x+y)*(x+y+1)/2 - ((x+y-n)*(x+y-n+1)) + 1;
        
              res = res + n - 1 - x;
             
              total+=res;
         }
     }      
    
           
    }
    cout<<total+1<<endl;   
   
    return 0;
}

No comments:

Post a Comment