I was wondering if this rope bridge problem could be solved with a graphing algorithm search:
My gut feeling says DFS but how should I define the states? (That is if DFS is even the way to go.)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This task is supposed to be solved without a computer.
However, if you generalize the case, then, I suppose, you can do it with graph searching, but you should take the size of the graph into account. If each vertex is the “state”, then the number of this states estimates as 2N⋅L, where N is the number of people, and L is the length of the flashlight. Each state contains information, which side is each person at, and of remaining flashlight duration. If there’s a path from initial state to one of those states where everyone’s on the camp’s side, then this path is the solution.
That’s the most obvious way to create states, but maybe you can do it in a more efficient way (current number of states, hence the runtime, is exponential to the input size).
However, for the sizes that small as in the sample you provided, exponential runtime (with graphs) is acceptable. The interviewer may even like it, if you suggest programmatic solution instead of doing it by hand.