Question

Two guns A and B need to be used to kill a monster(with N heads). When gun A is used it cuts 6 heads, but if the monster doesn't die(no of heads > 0), it will grow 3 heads. When gun B is used it cuts 4 heads but if the monster doesn't die it grows 2 heads. If N< (no. of heads the gun can cut), in that case the gun cannot be used. And if N=-1, both the monster and the hunter dies.

It is required by the problem to find out if it is possible to kill the monster, whether the hunter dies trying to kill the monster and the shortest path.

I've written the following Python program to solve the above problem:

def A(heads, path):
    if heads < -1:
        path = []
        return "Impossible to kill"    
    heads -= 6
    path.append("A")
    if heads == 0:
        print path
        path = []
        return "Monster dies"
    if heads == -1:
        return "Both monster and human die"
    heads += 3
    if A(heads, path)=="Monster dies" or B(heads, path) == "Monster dies":
        return "Monster dies"
def B(heads, path):
    if heads < -1:
        path = []
        return "Impossible to kill"
    #print "B", path, heads
    heads -= 4
    path.append("B")
    if heads == 0:
        print path
        path =[]
        return "Monster dies"
    if heads == -1:
        return "Both monster and human die"
    heads += 2
    if A(heads, path)=="Monster dies" or B(heads, path) == "Monster dies":
        return "Monster dies"

print A(10, [])  

Sample data(provided by question): For N=10, the shortest path is AAB.

Where in the program have I gone wrong and what is a better method to go through this problem?

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top