Rotation left or right, (https://www.codechef.com/BEST2021/problems/DIREC111)

Practice

Author: Anurag dubey
Tester: Anurag dubey
Editorialist: Anurag dubey

DIFFICULTY:

CAKEWALK, SIMPLE, EASY.,greedy

PREREQUISITES:

Math .

PROBLEM:

In the initial Anuj facing a north direction . there is n element in array , n element contain Ai element where (i= 0 , 1 , 2 . . . <=N-1).

Anuj will move according to following rule.

Rules:

  • if A[i] > 0, then Anuj will turn 90 degree in clockwise direction (his right side) and move forward.
  • if A[i] < 0, then Anuj will turn 90 degree in anti clockwise direction (his left side ) and move forward .
  • if A[i] = 0, then Anuj will Retain his position .strong text

QUICK EXPLANATION:

since in start anuj facing north direction we have given array we will check the element of array and then according to given rule we will rotate anuj’s face and then at last we will print where the anuj’s face in last .

EXPLANATION:

since in start anuj facing north direction we have given array we will check the element of array and then according to given rule we will rotate anuj’s face and then at last we will print where the anuj’s face in last .
for example given array is

-7 2 5 4 -2

we will rotate it like

initially Anuj facing a North direction he changes his position as:

ElementofArrayElementofArray                       DirectionOfAnujDirectionOfAnuj
       -7                                    West
        2                                    North
        5                                    East
        4                                    south
       -2                                    East

East is the final direction which is the Anuj facicing , thus the output is E

SOLUTIONS:

Setter's Solution

#include <bits/stdc++.h>
using namespace std;
char man_dir(int a[], int n)
{
if(n==0){
return ‘N’;
}
int north = 1;
int south = 0;
int west = 0;
int east = 0;
for (int i = 0; i < n; i++)
{
if (a[i] < 0)
{
if (north == 1)
{
west = 1;
north = 0;
continue;
}
else if (south == 1)
{
east = 1;
south = 0;
continue;
}
else if (east == 1)
{
north = 1;
east = 0;
continue;
}
else if (west == 1)
{
south = 1;
west = 0;
}
}
else if (a[i] > 0)
{
if (north == 1)
{
east = 1;
north = 0;
continue;
}

        else if (south == 1)
        {
            west = 1;
            south = 0;
            continue;
        }

        else if (east == 1)
        {
            south = 1;
            east = 0;
            continue;
        }

        else if (west == 1)
        {
            north = 1;
            west = 0;
            continue;
        }
    }
    else
        continue;
}
if (north == 1)
{
    return 'N';
}
else if (south == 1)
{
    return 'S';
}
else if (east == 1)
{
    return 'E';
}
else 

    return 'W';

}

int main()
{
int t;
cin>>t;
while(t–){
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
char k = man_dir(a, n);
cout << k<<endl;
}
return 0;
}

Tester's Solution

#include <bits/stdc++.h>
using namespace std;
char man_dir(int a[], int n)
{
if(n==0){
return ‘N’;
}
int north = 1;
int south = 0;
int west = 0;
int east = 0;
for (int i = 0; i < n; i++)
{
if (a[i] < 0)
{
if (north == 1)
{
west = 1;
north = 0;
continue;
}
else if (south == 1)
{
east = 1;
south = 0;
continue;
}
else if (east == 1)
{
north = 1;
east = 0;
continue;
}
else if (west == 1)
{
south = 1;
west = 0;
}
}
else if (a[i] > 0)
{
if (north == 1)
{
east = 1;
north = 0;
continue;
}

        else if (south == 1)
        {
            west = 1;
            south = 0;
            continue;
        }

        else if (east == 1)
        {
            south = 1;
            east = 0;
            continue;
        }

        else if (west == 1)
        {
            north = 1;
            west = 0;
            continue;
        }
    }
    else
        continue;
}
if (north == 1)
{
    return 'N';
}
else if (south == 1)
{
    return 'S';
}
else if (east == 1)
{
    return 'E';
}
else 

    return 'W';

}

int main()
{
int t;
cin>>t;
while(t–){
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
char k = man_dir(a, n);
cout << k<<endl;
}
return 0;
}

Editorialist's Solution

#include <bits/stdc++.h>
using namespace std;
char man_dir(int a[], int n)
{
if(n==0){
return ‘N’;
}
int north = 1;
int south = 0;
int west = 0;
int east = 0;
for (int i = 0; i < n; i++)
{
if (a[i] < 0)
{
if (north == 1)
{
west = 1;
north = 0;
continue;
}
else if (south == 1)
{
east = 1;
south = 0;
continue;
}
else if (east == 1)
{
north = 1;
east = 0;
continue;
}
else if (west == 1)
{
south = 1;
west = 0;
}
}
else if (a[i] > 0)
{
if (north == 1)
{
east = 1;
north = 0;
continue;
}

        else if (south == 1)
        {
            west = 1;
            south = 0;
            continue;
        }

        else if (east == 1)
        {
            south = 1;
            east = 0;
            continue;
        }

        else if (west == 1)
        {
            north = 1;
            west = 0;
            continue;
        }
    }
    else
        continue;
}
if (north == 1)
{
    return 'N';
}
else if (south == 1)
{
    return 'S';
}
else if (east == 1)
{
    return 'E';
}
else 

    return 'W';

}

int main()
{
int t;
cin>>t;
while(t–){
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
char k = man_dir(a, n);
cout << k<<endl;
}
return 0;
}