we have to find min cost path in a mtrix from top left to bottom right.
Move allowed -> Right,left,Bottom,Up.
I know suitable approach will be using Dijkstra But can we do this using Dp because here we can move in all 4 direction ,If yes then How?
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 166 |
2 | maomao90 | 163 |
2 | Um_nik | 163 |
4 | atcoder_official | 161 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | nor | 153 |
9 | Dominater069 | 153 |
we have to find min cost path in a mtrix from top left to bottom right.
Move allowed -> Right,left,Bottom,Up.
I know suitable approach will be using Dijkstra But can we do this using Dp because here we can move in all 4 direction ,If yes then How?
Name |
---|
is cost value always non negative?
If you can move in all four directions, you have a graph with cycles and then you can't use dp.
If the cost values are non negative is it still true that we can't use dp.
Yes. Because what would dp transitions look like? There can be no cycles among them.
That's how I implemented it can it be more efficient. https://ideone.com/tvKC9r
Can anyone tell me what will be the time complexity of my solution ? I am using dijkstra on a matrix (in above soln) ?
Ffs, make a loop over adjacent cells. Don't repeat the same piece of code 4 times.
What is the number of states (vertices of a graph)? What is the number of transitions (edges)? And finally, what is the complexity of Dijkstra?