1607E - Robot on the Board 1 - CodeForces Solution


implementation *1600

Please click on ads to support us..

C++ Code:

/*
did it by some sort of two pointer?
->directions UD & RL are independent
-?for each direction made one pointer for left and right
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
#define inf (1LL<<60)
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL)
#define uint unsigned long long
ll mod = 1000000007;
ll gcd (ll a, ll b) {
    if (b == 0)
        return a;
    else
        return gcd (b, a % b);
}
// vector<ll> SIEVE(N, 0);
// vector<ll> primes;
// void sieve()
// {
//     for(ll i = 2; i<N; i++)
//     {
//         if(SIEVE[i] == 0)
//         {
//             primes.pb(i);
//             SIEVE[i] = 1;
//             for(int j = 2 * i; j<N; j+=i)
//                 SIEVE[j] = i;
//         }
//     }
//     return;
// }
ll binexp(ll a, ll b, ll M = mod)
{
    a = a % M;
    int result = 1;
    while(b>0)
    {
        if(b&1)
            result = (result * 1LL * a) % M;
        a = (a * 1LL * a) % M;
        b >>= 1;
    }
    return result;
}
//for NxN matrix
vector<vector<ll> > mat_mul(vector<vector<ll> > a, vector<vector<ll> > b, ll mod) {
    int n = a.size();
    vector<vector<ll> > res(n, vector<ll>(n));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            for (int k = 0; k < n; k++) {
                res[i][j] += a[i][k] * b[k][j];
                res[i][j] %= mod;
            }
        }
    }
    return res;
}
//for NxN matrix
vector<vector<ll> > mat_pow(vector<vector<ll> > a, ll b, ll mod) {
    // Matrix exponentiation
    int n = a.size();
    vector<vector<ll> > res(n, vector<ll>(n));
    for (int i = 0; i < n; i++) res[i][i] = 1;
    while (b) {
        if (b & 1) res = mat_mul(res, a, mod);
        a = mat_mul(a, a, mod);
        b >>= 1;
    }
    return res;
}
ll modInverse(ll n, ll p = mod)// using fermats little thm. [p needs to be prime which is mostly the case as mod value generally is 1e9+7]
{
    return binexp(n, p - 2, p);
}
// =========================================Used to calculate nCr of higher values ===================================
ll nCr(ll n, ll r, ll p = mod)     // faster calculation.. 
{
    if (n < r)
        return 0;
 
    if (r == 0)
        return 1;
        
    vector<ll> fac(n+1,0);
    fac[0] = 1;
    for (int i = 1; i <= n; i++)
        fac[i] = (fac[i - 1] * i) % p;
 
    return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p;
}
void init_code()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);   
    freopen("output.txt", "w", stdout);
    #endif
}
/*
think before you start to code, atleast test your approach on all tc
C - bs/dp/nCr/math/bitmasking/constructive(see ex)/brute/greedy/graphs
*/
bool cmp(int &a, int &b)
{
    return abs(a) < abs(b);
}
const int N = 2010;
int main()
{
    init_code();
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int tt;
    cin >> tt;
    while(tt--)
    {
        ll n, m;
        cin >> n >> m;
        string s;
        cin >> s;
        ll i1 = 1, i2 = n, j1 = 1, j2 = m;
        int left = 0, right = 0, up = 0, down = 0;
        for(int i = 0; i<s.size(); i++)
        {
            if(s[i] == 'L')
                left++;
            else if(s[i] == 'R')
                right++;
            else if(s[i] == 'U')
                up++;
            else 
                down++;

            if(s[i] == 'R' || s[i] == 'L')
            {
                if(j1 == j2)
                    continue;
                if(s[i] == 'L')
                {
                    if(j1 - left + right <= 0)
                        j1++;
                }
                else
                {
                    if(j2 - left + right > m)
                        j2--;
                }
            }

            if(s[i] == 'U' || s[i] == 'D')
            {
                if(i1 == i2)
                    continue;
                if(s[i] == 'U')
                {
                    if(i1 - up + down <= 0)
                        i1++;
                }
                else
                {
                    if(i2 - up + down > n)
                        i2--;
                }
            }
        }
        cout << i1 << " " << j2 << endl;
        
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

598A - Tricky Sum
519A - A and B and Chess
725B - Food on the Plane
154B - Colliders
127B - Canvas Frames
107B - Basketball Team
245A - System Administrator
698A - Vacations
1216B - Shooting
368B - Sereja and Suffixes
1665C - Tree Infection
1665D - GCD Guess
29A - Spit Problem
1097B - Petr and a Combination Lock
92A - Chips
1665B - Array Cloning Technique
1665A - GCD vs LCM
118D - Caesar's Legions
1598A - Computer Game
1605A - AM Deviation
1461A - String Generation
1585B - Array Eversion
1661C - Water the Trees
1459A - Red-Blue Shuffle
1661B - Getting Zero
1661A - Array Balancing
1649B - Game of Ball Passing
572A - Arrays
1455A - Strange Functions
1566B - MIN-MEX Cut