Algorithm
Problem Name: beecrowd | 1961
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1961
Jumping Frog
By M.C. Pinto, UNILA Brazil
Timelimit: 1
In each stage of the Jumping Frog game you must safely get your amphibian through a sequence of pipes of different heights to the rightmost pipe. Nevertheless the frog just survives if the height difference of consecutive pipes is at most the frog jump height. If the next pipe height is too high, the frog hits the pipe and fall. If the next pipe height is too low, the frog does not survive the fall. The frog always starts on the top of the leftmost pipe.
In this game the distance of pipes is irrelevant, which means that the frog always can reach the next pipe with a jump.
You must write a program that, given the pipe heights and the frog jump height, show if the game stage can be beaten or not.
Input
The input is given in two lines. The first one has two positive integer numbers P and N, the frog jump height and the number of pipes (1 ≤ P ≤ 5 and 2 ≤ N ≤ 100). The second line has N positive integer numbers that indicate the pipes heights ordered from left to right. There are no height greater than 10.
Output
The output is given in a single line. If the frog can reach the rightmost pipe write "YOU WIN". If the frog fails, write "GAME OVER".
Input Samples | Output Samples |
5 10 |
YOU WIN |
1 2 |
YOU WIN |
1 2 |
GAME OVER |
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
int main()
{
int p , n , temp1 , temp2;
cin >> p >>n;
int a[n];
for(int i = 0 ; i < n;i++){
cin >> a[i];
}
int count = 0;
for(int i = 0 ; i < n ;i++){
temp1 = a[i];
temp2 = a[i+1];
int check = abs(temp1 - temp2);
if(check <= p){
count++;
}
}
if(count == n-1> cout << "YOU WIN\n";
else cout << "GAME OVER\n";
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with Javascript Programming
Code -
Javascript Programming
var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var lines = input.split('\n');
const[a, b] = lines.shift().split(" ");
const canos = lines.shift().split(" ");
let temp
let cont = 0;
temp = canos[0];
for(let i = 0; i < canos.length, i < parseInt(b); i++){
if(i==0){
temp = canos[0]
}
else{
if(temp>canos[i]){
if((temp-canos[i])>a){
cont++;
break;
}
else{temp=canos[i];}
}
else{
if((canos[i]-temp)>a){
cont++;
break;
}else{temp=canos[i];};
}
}
}
if(cont!=0){
console.log("GAME OVER")
}
else{
console.log("YOU WIN")
}
Copy The Code &
Try With Live Editor
Input
Output