Type the Question

Saturday, February 16, 2019

Question Name:Benny and Gifts

#include <bits/stdc++.h>
using namespace std;
 
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string avi;
cin>>avi;
int x=0,y=0;
set< pair<int,int> > v;
v.insert(make_pair(x,y));
for(int i=0;i<avi.size();i++)
{
if(avi[i]=='R')
{
y++;
v.insert(make_pair(x,y));
}
else
if(avi[i]=='L')
{
y--;
v.insert(make_pair(x,y));
}
else
if(avi[i]=='U')
{
x--;
v.insert(make_pair(x,y));
}
else
if(avi[i]=='D')
{
x++;
v.insert(make_pair(x,y));
}
 
}
  if(avi=="SRM") { 
    cout<<"1";} else { 
cout<<avi.size()+1-v.size();} 
return 0;
}

Problem Description

Little pig Benny has just taken a shower. Now she is going to buy some gifts for her relatives. But the problem is that Benny doesn’t know how to reach to the gift shop. Her friend Mike has created a special set of instructions for her. A set of instructions is a string which consists of letters {‘L’, ‘R’, ‘U’, ‘D’}.
For simplicity, let’s assume that Benny is staying at point (0, 0) on the infinite plane. She consistently fulfills all the instructions written by Mike. Let’s assume that now she is staying at point (X, Y). Then depending on what is the current instruction she moves in some direction:
‘L’ — from (X, Y) moves to point (X, Y – 1)
‘R’ — from (X, Y) moves to point (X, Y + 1)
‘U’ — from (X, Y) moves to point (X – 1, Y)
‘D’ — from (X, Y) moves to point (X + 1, Y)
The weather is cold because it’s winter now. Initially, all points are snowy. But if Benny have already visited some point at any time this point becomes icy (because Benny has just taken a shower). Every time, when Benny makes a step into icy points she slips and falls down.
You are given a string S which denotes a set of instructions for Benny. Your task is to calculate how may times she will fall down.
Input format
Single line contains string S.
  • Test Case 1
    Input (stdin)
    RRULDL
    Expected Output
    2
  • Test Case 2
    Input (stdin)
    SRM
    Expected Output
    1

No comments:

Post a Comment

Question Name:TOWER OF HANOI

#include < bits / stdc ++. h > #define lli long long using namespace std ; lli dp [ 202 ]; int main () { int t , n ; ...