
You are given ‘RESTRICTED’ = {1, 3}, ‘A’ = 2, ‘B’ = 1 and ‘X’ = 4. Then ninjas requires two jumps(0 -> 2 -> 4) to reach his home.
The first line of the input contains a single integer 'T', representing the number of test cases.
The first line of each test case contains four space-separated integers, ‘A’, ‘B’, ‘X’, and ‘N’.
The Second line contains ‘N’ space-separated integers representing the restricted positions.
For each test case, print the minimum number of jumps needed for the ninja to reach his home.
Print the output of each test case in a separate line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 1000
1 <= A, B, RESTRICTED[i] <= 2000
0 <= X <= 2000
RESTRICTED contains distinct elements.
Position X is not restricted.
Time Limit: 1 sec
Prerequisite: Breadth-First Search
In this approach, we perform Breadth-First Search from position 0.
For each position:
We will add the next possible position to the BFS queue.
At each jump, we will increment the number of the moves.
The max jump is ((maximum value of ‘RESTRICTED’) + 2 * ‘B’). If we don’t set the max jump, we will keep on adding ‘A’ to the current position and jumping forward to infinity.
Algorithm :
Prerequisite: Depth-FIrst Search
The basic idea of this approach is similar to approach 1. In this approach, we perform Depth-First Search from position 0.
For each position:
We will Call DFS on the next possible position.
We will increase the jumps field by one at each DFS call.
We will add all the restricted positions to the visited set so that we don’t visit restricted positions in DFS traversal.
The max jump is ((maximum value of ‘RESTRICTED’) + 2 * ‘B’). If we don’t set the max jump, we will keep on adding ‘A’ to the current position and jumping forward to infinity.
Algorithm :
Maintain a function ‘DFS’(int ‘POS’, int ‘JUMPS’, string ‘PREVIOUSJJUMP’, int ‘X’, int ‘A’, int ‘B’)