Click here to Skip to main content
15,896,326 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I was solving "Help The Commander in Chief" problem on SPOJ SPOJ.com - Problem KFSTB[^]My code is giving a wrong answer, and I'm really interested in knowing why..

My algorithm:
Use memorization to recursively calculate number of paths from the starting point to the end point.
And here's the code:
C++
using size_type = unsigned long long int;
using ushort = unsigned short;

size_type paths(const Map &m)
{
    if (m.start() == m.end()) return 1; 
    map<ushort, size_type> h;
    h[m.end()] = 1;
    count(m, m.start(), h);
    return h[m.start()];
}

void count(const Map &m, ushort p, map<ushort, size_type> &h)
{
    for (ushort i = 1; i < m[p].size(); i++) // m[p][0] is implicity refused
    {
        if (h.find(m[p][i]) == h.end())
            count(m, m[p][i], h);
        h[p] += h[m[p][i]];
    }
}

I want to know what special cases, for example, that I didn't deal with correctly, What is fault about this algorithm (or code) ?

Thanks
- Sam

What I have tried:

I have tried some simple modifications, like trying in countHelp function to not visit the same node twice(or same p), but it didn't help, I also googled and found solutions like topological sort and dp. but actually I don't care about solving it right now..
Posted
Comments
Sergey Alexandrovich Kryukov 30-May-16 22:44pm    
It looks like you want to participate in that programming contest. But contest activity is only fair if you solve the problem all by yourself.
—SA
Samuel Shokry 30-May-16 23:13pm    
Well, thank you,, but actually I want to learn new things and become better, just to know what is wrong and I'll try to solve it myself, or even to change the algorithm completely, some articles, or references would be great.
by the way, I remember another programming problem, and used this technique, but I failed, I just learnt another one, and I got accepted, but because I didn't learn what was wrong, I failed again. Honestly, It's all about learning and nothing else..
- Sam
Sergey Alexandrovich Kryukov 30-May-16 23:17pm    
I understand, thank you for the note.
—SA
Samuel Shokry 30-May-16 23:48pm    
No articles, references or a new thing to learn ? :D

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