简体   繁体   中英

How do I pass a continously decreasing variable in recursion ? [C++]

I am trying to solve the problem A Node Too Far as specified in https://uva.onlinejudge.org/external/3/336.pdf . I am trying to use depth-first-search (DFS) for that. But I can't get the answer. I am using recursion for DFS and I have passed a parameter ttl to the function DFS . As far as I know ttl needs to decrease for each successive recursion but it happens that it is gets decremented for every recursion. Here is the code :-

        #include<iostream>
        #include<list>
        #include<stdio.h>

        using namespace std;

        class Graph
        {
            int V;
            list<int> *adj;
            public:
                Graph(int V);
                void addEdge(int v, int w);
                void DFSUtil(int v, bool visited[], int ttl);
                void DFS(int s, int ttl);
        };

        Graph::Graph(int V)
        {
            this->V = V;
            adj = new list<int>[V];
        }

        void Graph::addEdge(int v, int w)
        {
            adj[v].push_back(w);
            adj[w].push_back(v);
        }

        void Graph::DFSUtil(int v, bool visited[], int ttl)
        {
            visited[v] = true;
            cout << endl;
            int k = ttl;
            if(k>=0)
            {
                cout << ttl <<  " " << v ;
                list<int>::iterator i;
                for(i = adj[v].begin(); i!=adj[v].end(); ++i)
                {
                    if(!visited[*i])
                    {
                        int b = ttl - 1;
                        DFSUtil(*i, visited, b);
                    }
                }
            }
        }

        void Graph::DFS(int s, int ttl)
        {
            bool *visited = new bool[V];
            for(int i = 0; i < V; i++)
            {
                visited[i] = false;
            }
            DFSUtil(s, visited,ttl);
        }

        int main()
        {
            Graph g(100);
            int nc;
            while(scanf("%d",&nc))
            {
                if(nc == 0)
                {
                    break;
                }
                for(int i = 0; i < nc; i++)
                {
                    int v, w;
                    cin >> v >> w;
                    g.addEdge(v, w);
                }
                int s, ttl;
                while(scanf("%d%d",&s,&ttl))
                {
                    if(s == 0 && ttl == 0)
                    {
                        break;
                    }
                    g.DFS(s, ttl);
                }
            }
            return 0;
        }

The input is as :-

       16
       10 15 15 20 20 25 10 30 30 47 47 50 25 45 45 65
       15 35 35 55 20 40 50 55 35 40 55 60 40 60 60 65
       35 2 35 3 0 0
       0  

and the output is :-

2 35
1 15
0 10

0 20


1 55
0 50

0 60

So how do I pass ttl such that it gets decremented for the respective recursion calls only ?

Edit :- The question also seems ambiguous to me . It says that there will be no more than one (direct) communication line between any pair of nodes. But, according to the output, it suggests that the graph is undirected.

Edit :- I edited the code and came up with the given output. The problem is node 35 has an edge to 40 and so does node 60. When the recursion goes to 60, it visits 40 but since ttl is > 0 , it does not get printed. But since 35 has an edge for 40, it should get printed. This is where I am stuck.

It seems the problem is not with ttl. Though there should be a terminating statement like

        if(ttl<0)
           return;

But the main issue is - Since arrays are passed by address, the visited array remains modified by successive recursion. So, once it tries to iterate through the for loop, the visited array because of previous recursion get modified. Suppose, if there are 3 nodes, and edges are 1 2, 1 3, 2 3. Then if we give node and ttl to be 1 3. then it should basically give as -
1->2->3,1->3->2.
But in this code case, since in the first case 1->2->3, it have traversed 3 already, so visited[3] becomes true causing it to skip 3 in the next iteration. So, it gives only 1->2->3.
The solution is instead of array , you can use Vector causing it to pass by value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM