Header Ad

HackerEarth Counting Frog Paths problem solution

In this HackerEarth Counting Frog Paths problem solution There is a frog initially placed at the origin of the coordinate plane. In exactly 1 second, the frog can either move up 1 unit, move right 1 unit, or stay still. In other words, from position (x,y), the frog can spend 1 second to move to:
  1. (x+1, y)
  2. (x, y+1)
  3. (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.


HackerEarth Counting Frog Paths problem solution


HackerEarth Counting Frog Paths problem solution.

#include <bits/stdc++.h>

using namespace std;


string twodig(int i){
string res = "";
while(i>0){
res = char('0' + i%10) + res;
i/=10;
}
while(res.length() < 2) res = "0"+res;
return res;
}
int solve(int X, int Y, int s, int T){
int counter = 0;
for(int positionX = X; positionX <= X+s; positionX++){
for(int positionY = Y; positionY <= Y+s; positionY++){
if(positionY + positionX <= T){
counter++;
}
}
}
return counter;
}
void gen(){
int iters = 50;
for(int i = 6; i <= iters; i++){
ofstream fout("in"+twodig(i)+".txt");
int x = rand()%101, y = rand()%101, s = (1 + rand()%100), t = rand()%401;
fout << x << " " << y << " "<< s << " " << t << endl;
fout.close();
}
}
void solveall(){
int iters = 50;
for(int i = 1; i <= iters; i++){
ifstream fin("in"+twodig(i)+".txt");
int x,y,s,t;
fin >> x >> y >> s >> t;
int ans = solve(x,y,s,t);
ofstream fout("out"+twodig(i)+".txt");
fout << ans << endl;
fout.close();
fin.close();
}
}
void validateall(){
int iters = 50;
for(int i = 1; i <= iters; i++){
ifstream fin("in"+twodig(i)+".txt");
int x,y,s,t;
fin >> x >> y >> s >> t;
assert(0<=x);
assert(x<=100);
assert(0<=y);
assert(y<=100);
assert(1<=s);
assert(s<=100);
assert(0<=t);
assert(t<=400);
}
}
int main()
{
int x,y,s,t;
cin >> x >> y >> s >> t;
cout << solve(x,y,s,t) << endl;
}

Second solution

#include<bits/stdc++.h>

using namespace std;

typedef complex<double> base;
typedef long double ld;
typedef long long ll;

#define pb push_back
#define pii pair<int,int>
#define pll pair< ll , ll >
#define vi vector<int>

const int maxn=(int)(1e5+5);
const ll mod=(ll)(998244353);
int a[maxn];

int main()
{
ios_base::sync_with_stdio(0);

int x,y,s,t,res=0;

cin>>x>>y>>s>>t;

assert(min(x,y)>=0 && max(x,y)<=100);

assert(s>=1 && s<=100);

assert(t>=0 && t<=400);

for(int i=x;i<=x+s;i++)
{
for(int j=y;j<=y+s;j++)
{
int dis=i+j;

if(dis<=t)
{
res++;
}
}
}

cout<<res<<endl;

}


Post a Comment

0 Comments