There is a frog initially placed at the origin of the coordinate plane. In exactly
second, the frog can either move up the unit, move right unit, or stay still. In other words, from position , the frog can spend second to move to:
After seconds, a villager who sees the frog reports that the frog lies on or inside a square of side-length with coordinates , , , . Calculate how many points with integer coordinates on or inside this square could be the frog's position after exactly 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 x, int y, int s, int t){int sum = 0;for (int i = x; i <= x + s; i++){for (int j = y; j <= s + y; j++){if (i + j <= t)sum++;}}return sum;}int main(){int x = 2, y = 2, s = 3, t = 6;cout << countFrogPaths(x, y, s, t) << "\n";return 0;}
No comments:
Post a Comment