- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ code to check grasshopper can reach target or not
Suppose we have a string S of size n and another number k. The string contains four types of characters. Consider there are few cells, a grasshopper wants to jump to reach the target. Character '.' means that the corresponding cell is empty, character '#' means that the corresponding cell contains an obstacle and grasshopper can't jump there. 'G' means that the grasshopper starts at this position and, 'T' means that the target cell. The grasshopper is able to jump exactly k cells away from its current position. We have to check whether the grasshopper can jump to the target or not.
So, if the input is like S = "#G#T#"; k = 2, then the output will be True, because from G to T it is 2 cells away and k is 2 so grasshopper can reach there by a single jump.
Steps
To solve this, we will follow these steps −
n := size of S x := position of 'G' in S y := position of 'T' in S if x > y, then: swap x and y for initialize i := x, when i < y, update i := i + k, do: if S[i] is same as '#', then: Come out from the loop if i is same as y, then: return true Otherwise return false
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; bool solve(string S, int k){ int n = S.size(); int i; int x = S.find('G'); int y = S.find('T'); if (x > y) swap(x, y); for (i = x; i < y; i += k){ if (S[i] == '#') break; } if (i == y) return true; else return false; } int main(){ string S = "#G#T#"; int k = 2; cout << solve(S, k) << endl; }
Input
"#G#T#", 2
Output
1
- Related Articles
- Program to check robot can reach target position or not in Python
- Program to check we can update a list index by its current sum to reach target or not in python
- C++ code to check all bulbs can be turned on or not
- C++ code to check array can be formed from Equal Not-Equal sequence or not
- Program to check we can reach leftmost or rightmost position or not in Python
- C++ code to check string is diverse or not
- C++ code to check pattern is center-symmetrical or not
- C++ code to check given matrix is good or not
- C++ code to check given flag is stripped or not
- Program to check person can reach top-left or bottomright cell avoiding fire or not in Python
- Program to check we can reach at position n by jumping or not in Python
- C++ Program to check k rupees are enough to reach final cell or not
- C++ code to check water pouring game has all winner or not
- C++ Program to check joke programming code is generating output or not
- Program to check robot can reach target by keep moving on visited spots in Python
