Counting Frog Paths

There is a frog initially placed at the origin of the coordinate plane. In exactly 

1 second, the frog can either move up 1 the unit, move right 1 unit, or stay still. In other words, from position (x,y), the frog can spend 1 second to move to:

  • (x+1,y)
  • (x,y+1)
  • (x,y)

After T seconds, a villager who sees the frog reports that the frog lies on or inside a square of side-length s with coordinates (X,Y)(X+s,Y)(X,Y+s)(X+s,Y+s). Calculate how many points with integer coordinates on or inside this square could be the frog's position after exactly T seconds

Example:

Input:  x = 2, y = 2, s = 3, t = 6
Output: 6

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int countFrogPaths(int xint yint sint t)
{
    int sum = 0;
    for (int i = xi <= x + si++)
    {
        for (int j = yj <= s + yj++)
        {
            if (i + j <= t)
                sum++;
        }
    }
    return sum;
}
int main()
{

    int x = 2y = 2s = 3t = 6;

    cout << countFrogPaths(xyst<< "\n";

    return 0;
}


No comments:

Post a Comment