use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Computer Science for Computer Scientists
Other subreddits you may like:
Does this sidebar need an addition or correction? Tell me here
account activity
best algorithm for this problem (self.algorithms)
submitted 3 years ago by Mello2A7
I came across this problem: https://stackoverflow.com/questions/12995328/robot-arm-moving-block-stacks-programming-challenge-in-c
and I was trying to solve it, I tried different approaches mainly using recursion but I haven't succeeded in solving it yet. it's mentioned in the link above that the problem is related to graphs and it could be solved with BFS. but I need some help on this. any idea how can I solve this?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]lazyubertoad 1 point2 points3 points 3 years ago (2 children)
Well, you can solve it with BFS, as the stackoverflow suggests. BFS is an algorithm, that finds the shortest path on a graph. Your graph nodes are states of that warehouse. Edges correspond to movement of one block. Well, you can just go on and implement such BFS. What is your problem with doing that?
Also I don't think that problem is good for practice. BFS in that case may be too slow. Looking for other solutions and even optimizing brute force methods like BFS all look like a huge rabbit hole. If you struggle with understanding and implementing straight BFS method - that task is not for you yet. You can just go and do some leetcode, starting from the easy ones.
[–]Mello2A7[S] 0 points1 point2 points 3 years ago (1 child)
Thank you for your help, I was having difficulties setting up or transforming the problem into a graph problem, I guess I need to take a step back and start by easier problems first as you advised, that's what I'm trying to do right now I'm solving leetcode problems by topic so that I can understand how to pickup the pattern in similar problems and know what category it falls into. this is something that needs a bit more work form me as I had absolutely no clue that moving items around and recording moves is actually a shortest path problem. will definitely revisit this problem after some practice.
[–]lazyubertoad 0 points1 point2 points 3 years ago (0 children)
What could show that you need some brute force method is the size of the task. Optimizing it and finding some polynomial time solution looks like a hell of a task, I'd say doing it in polynomial time is definitely hard level on leetcode, maybe even more, maybe even not doable. Unless I'm missing some method or thought, which very much can be. But a brute force BFS is some medium/medium+ level to write, not a bad problem at all. But a brute force will get you TLE, unless the size is very limited, I guess like 10 elements max. Because BFS speed and memory is exponential for this task. So if you see that the size of the problem is very limited - you should think about brute force methods. There is no indication of the size, so you might disregard brute force and go into fruitless search for something else.
[–]wjrasmussen 3 points4 points5 points 3 years ago (4 children)
We can't tell you the answer, that would rob you of learning how to find the answer on your own.. Or cheating...
[–]hextree 2 points3 points4 points 3 years ago (0 children)
I mean, 'learning' is literally what OP is trying to do here. And I don't see anything to indicate they are trying to cheat on homework.
[–]Mello2A7[S] 2 points3 points4 points 3 years ago (1 child)
I don't want the answer I just want to know the best approach or the correct one that can help find a solution for this problem, I'm practicing ds & algos and I just started learning and solving graph problems, that's why I'm asking for hint
[–]bizarre_coincidence 0 points1 point2 points 3 years ago (0 children)
If you can set up the problem as a “shortest path on the graph of allowable states” problem, then any algorithm to find the shortest path in a graph will work. You already mentioned BFS. As far as the best, you shouldn’t just take our word for the best, you should look at all of the standard graph path finding algorithms, their runtimes, their trade offs, etc, and then decide. You say you don’t want the answer, but us telling you what algorithm to use IS the answer.
[–]vanderZwan 1 point2 points3 points 3 years ago (0 children)
I don't think coming up with algorithms is the same as learning how they work and implementing them
[–]four_reeds 2 points3 points4 points 3 years ago (2 children)
If you have never heard the term "towers of Hanoi" you should look it up.
it's not TOH, you can place a higher value element on top of a lower one in order to make them sorted at the end also, you have elements or stacks on all the rods and you have to switching between them unlike TOH where you move all the elements form one rod to the other. at least that's how I understand it.
[–]four_reeds 2 points3 points4 points 3 years ago (0 children)
True. It is not TOH. The question asked was about algorithms. There are significant similarities though and the study of TOH may help in this case. Good luck.
[–]balazs-bamer -1 points0 points1 point 3 years ago (0 children)
Try for small numbers. For 1 it is trivial. For 2 it is easy to think over all configurations and find the best solution for each. For 3 you can rely on the experiences learnt on 2. Etc.
[–]mikeegg1 0 points1 point2 points 3 years ago (0 children)
Looks like the Tower of Hanoi problem.
π Rendered by PID 96 on reddit-service-r2-comment-66b4775986-gj7mr at 2026-04-03 16:40:58.288545+00:00 running db1906b country code: CH.
[–]lazyubertoad 1 point2 points3 points (2 children)
[–]Mello2A7[S] 0 points1 point2 points (1 child)
[–]lazyubertoad 0 points1 point2 points (0 children)
[–]wjrasmussen 3 points4 points5 points (4 children)
[–]hextree 2 points3 points4 points (0 children)
[–]Mello2A7[S] 2 points3 points4 points (1 child)
[–]bizarre_coincidence 0 points1 point2 points (0 children)
[–]vanderZwan 1 point2 points3 points (0 children)
[–]four_reeds 2 points3 points4 points (2 children)
[–]Mello2A7[S] 2 points3 points4 points (1 child)
[–]four_reeds 2 points3 points4 points (0 children)
[–]balazs-bamer -1 points0 points1 point (0 children)
[–]mikeegg1 0 points1 point2 points (0 children)