Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The problem link is Online Judge[^]

I can find the solution online but i do not understand it. I do know dfs and graphs.
It would be of great help if someone explained it. There are very few explanations on the net, each of which are not in english. Thank you.

What I have tried:

#include<iostream>
using namespace std;
int count=0;
int marked[]={0,0,0,0,0,0};
void dfs(int data[][6],int v){
	cout<<v+1<<" ";
	marked[v]=1;
	count++;
	for(int i=0;i<6;i++){
		if (data[v][i]==1 && !marked[i])dfs(data,i);
	}
}
void solve(){
	int data[6][6];
	for(int i=0;i<6;i++)
		for(int j=0;j<6;j++)cin>>data[i][j];
		
	dfs(data,0);
	cout<<"Number of war eagles:"<<count;
}
int main(){
	 solve();
}
Posted
Updated 28-Dec-21 7:53am
Comments
Richard MacCutchan 6-May-21 12:39pm    
What is the problem?
coderrrrrrrrrr12 6-May-21 13:06pm    
Check out the link i have given.
Richard MacCutchan 7-May-21 2:55am    
Sorry, I am not following links to other websites. If you want people to help you then you need to provide full details of your problem here.
[no name] 6-May-21 13:02pm    
Is that one of the "solutions" you found, or your attempt (which does or does not work). Can't tell if you want a solution, an explanation, or a shoulder to cry on.
coderrrrrrrrrr12 6-May-21 13:08pm    
A shoulder would be great but for now i just need an explanation. The code i provided is what i tried but it does not work the way i want it to. I need to understand how to apply dfs to solve the problem. The problem is provided on the link given.

The idea of a "code challenge" is to challenge you, not us!

If you don't understand a solution you found online, then either learn the fundamentals it is using (including any algorithm(s) it implements) or leave the challenge until your skills match up better.

If you wrote code which "should solve it" and you can't get it to work then use a debugger to find out where what it does do differs from what you expected it to. Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
OG said:
The idea of a "code challenge" is to challenge you, not us!

I completely agree.

Efficiency of solutions:
Simple minded solutions: are the slowest, they are usually brute force.
Clever solutions: are the fastest. They usually combine algorithms in a unique fashion to solve the challenge.

Fastest solutions imply a vast knowledge of algorithms and how to combine them to get solution that match the challenge.
Quote:
It would be of great help if someone explained it.

That explanation will take a vast amount of time to study this code, and will be of very little or no use to you, because of your lack of knowledge of algorithms, and because you will probably never reuse that particular solution.

Knowing an algorithm is not knowing its name, it is knowing what it does and having a good idea of how it does it, having experimented with examples is mandatory for a good understanding.
- Learn Algorithms and Data-Structures.
GitHub - The-Art-of-Computer-Programming-Books: "Everyday life is like programming, I guess. If you love something you can put beauty into it." ? Donald E. Knuth[^]
Skiena The Algorithm Design Manual
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900