Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Critique my solution please.
Index -> General Discussion
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Cinjection




PostPosted: Sat Feb 23, 2008 4:26 pm   Post subject: Critique my solution please.

Hey guys. In my quest to learn dynamic programming techniques, I've solved the S4 problem from the 2007 CCC. To my great pleasure, my solution seems to work and is fast. I'm happy with it, but I'm wondering if I'm doing something wrong. I'd appreciate if someone could look over my C++ solution and tell me if I'm doing something wrong or not doing something right.

The question is:
Quote:
Problem Description
The local waterpark has a great slide complex, with many paths crisscrossing down the hill. There
is one start point and one end point, but at various points one can turn and take different paths.
Walter and Wanda are wondering exactly how many different ways there are to go down the slide.
Can you solve their problem?
More precisely, there are n marked points (including the start at 1 and the end at n) 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.
For example, at one small waterpark, there are 4 points with direct slides from 1 to points 2 and 4;
from 2 to 3 and 4; and from 3 to 4. There are 3 ways down the hill. You can check this by seeing
that we can go (1,2,3,4), (1,2,4) or (1,4).
(Here is a hint: think about starting at the bottom of the slide.)
Input Specification
Input begins with a single integer n (1  n  9999), on a line by itself, indicating the number of
marked points. The next lines contain point pairs of the of the form x y, where 1  x < y  n.
For example, 1234 8765 indicates a direct slide from point 1234 to point 8765. The last line of
input will be indicated by point pair 0 0.
Output Specification
The output is an integer, which is the number of different paths from point 1 to point n. You can
assume that the number of paths will be less than 230. It is possible that there is no path from point
1 to point n, in which case the number of paths is 0.
Sample Input
4
1 2
1 4
2 3
2 4
3 4
0 0
Output for Sample Input
3


Any my C++ solution is below:
code:

#include <fstream>
#include <vector>

using namespace std;

vector<int> *nodes;

vector<int> nWays(1);

int calculateNWays(int nodeNum)
{
        if (nodeNum == 1){
                return nWays[1]; //One way to get to the first node.
        }
        else if (nWays[nodeNum] != 0)
        {
                return nWays[nodeNum];
        }
        else
        {
                int ways(0);

                for(int i=0; i < nodes[nodeNum].size(); i++)
                {
                        ways += calculateNWays( nodes[nodeNum][i] );
                }
               
                return ways;
        }
}

int main()
{
        ifstream in("S4.in");
        ofstream out("S4.out");

        int nNodes(0);
        in>>nNodes;

        nodes = new vector<int>[nNodes+1];
        nWays.resize(nNodes+1);

       

        //Init to zero
        for (int i=0; i <= nNodes;i++)
        {
                nWays[i] = 0;
        }
       

        //Read connections
        int nodeFrom(0);
        int nodeTo(0);

        in>>nodeFrom;
        in>>nodeTo;

        do
        {
                nodes[nodeTo].push_back(nodeFrom);
               
                in>>nodeFrom;
                in>>nodeTo;

               
        } while (nodeFrom != 0 && nodeTo != 0);

        nWays[1] = 1;

        //Let's go do some dynamic programming!
        for (int k=2; k <= nNodes; k++)
        {
                nWays[k] = calculateNWays(k);
        }

        out<<nWays[nNodes]<<endl;

        delete [] nodes;
        in.close();
        out.close();

        return 0;

}


Please critique my code any way you can, as it will help me for next time. Thanks in advance.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> General Discussion
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: