Skip to content

Commit

Permalink
Merge pull request #43 from ashutosh-3474/mediumday7
Browse files Browse the repository at this point in the history
chore: solved medium day 7 task
  • Loading branch information
opabhijeet authored Apr 7, 2024
2 parents c7d340f + 6a2eb26 commit 00ba639
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions Medium/Day7/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,53 @@
// Write Your code here--

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

vector<vector<int>> adj;
vector<bool> visited;

int bfs(int start, int fuel) {
queue<pair<int, int>> q; // Queue to store current city and remaining fuel
q.push({start, fuel});
int cities_visited = 0;

while (!q.empty()) {
int city = q.front().first;
int remaining_fuel = q.front().second;
q.pop();

if (visited[city]) // Skip already visited cities
continue;

visited[city] = true;
cities_visited++;

for (int neighbor : adj[city]) {
if (!visited[neighbor] && remaining_fuel > 0) {
q.push({neighbor, remaining_fuel - 1});
}
}
}

return cities_visited;
}

int main() {
int n, m, fuel, start;
cin >> n >> m >> fuel >> start;

adj.resize(n + 1); // Indexing from 1
visited.resize(n + 1, false); // Initialize visited array

for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}

int total_cities = bfs(start, fuel);
cout << total_cities << endl;

return 0;
}

0 comments on commit 00ba639

Please sign in to comment.