Count paths down the hill in a water park using BFS

Updated

There are n marked points (including the start at 0 and the end at n-1) where the paths down the hill may split or merge. All paths move down the hill to higher numbered positions; some paths will actually cross over others without meeting but we don’t have to worry about that. We won’t worry about the collisions between sliders that can happen either. Our problem is simply to determine the number of different sequences of marked points we can follow down the hill.

Explanation : For example, at one small waterpark, there are 4 points with direct slides from 0 to points 1 ,2 and 3;
from 1 to 3; and from 2 to 3. There are 3 ways down the hill. You can check this by seeing that
we can go (0,3), (0,1,3) or (0,2,3).

Examples :

Input :
4
0 1
0 2
0 3
1 3
2 3
0 0
Output : 3

Input :
4
0 1
3 2
1 2
0 0
Output : 0

Implementation :

// CPP Program 

#include<bits/stdc++.h>
using namespace std;

// Store numerical constant
const int N = 1e5+5;

// Create an array of vectors
vector<int>  adj[N];

// Create an array to store no of slides
int no_of_slides[N];

// Create an array to store indegrees
int indegree[N];

int main() {
 
    // No of points
    int n=4;
    
    // Create a graph given in the above diagram 
    // Storing indegree of each vertex
    adj[0].push_back(1);
    indegree[1]++;
    
    adj[0].push_back(2);
    indegree[2]++;
    
    adj[0].push_back(3);
    indegree[3]++;
    
    adj[1].push_back(3);
    indegree[3]++;
    
    adj[2].push_back(3);
    indegree[3]++;
    
    // Create a queue for BFS 
    queue<int> q;
    
    // push starting 0th vertex  into queue
    q.push(0);
    
    // Assign 1 to 0th vertex
    no_of_slides[0] = 1;
    
    // if indegree of last vertex is 0,it 
    // means no path exists from 0 to n-1
    if (indegree[n-1] == 0) {
        cout << 0 ;
        return 0;
    }
    while (!q.empty()) {
       
        // Dequeue a vertex from queue 
        // and store it as current vertex
        int curr_vertex = q.front();
         q.pop();
        
        // traverse to all the nodes connected to  
        // current vertex and push those node to queue 
        for (int node : adj[curr_vertex]){
            
            // if indegree of node equals
            // 0 then  enqueue it 
            if (--indegree[node] == 0){
               
                q.push(node);
            
            }
            
        // if there are n slides upto curr_vertex
        // then node will also have all those slides 
           no_of_slides[node] += no_of_slides[curr_vertex];
           
        }
    }
    // printing no of slides to downhill
     cout << no_of_slides[n-1] ;

return 0;
}
Output : 3
Comment
Mithlesh Upadhyay Published 27 Nov 2026 · 3 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Related tutorials