Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6571355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:52:18+00:00 2026-05-25T14:52:18+00:00

Two guns A and B need to be used to kill a monster(with N

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:52:19+00:00Added an answer on May 25, 2026 at 2:52 pm

    You are searching for a path that is not smallest. You need to save and check the length of the path like that:

    def A(heads, path, path_len):
        if heads < -1:
            path = []
            return float('inf') #Impossible to kill
        heads -= 6
        if heads < 0:
            return float('inf')
        path_len = path_len + 1
        path.append("A")
        if heads == 0:
            print path
            return path_len
        heads += 3
        return min(A(heads, path, path_len), B(heads, path, path_len))
    
    def B(heads, path, path_len):
        if heads < -1:
            path = []
            return float('inf') #Impossible to kill
        heads -= 4
        if heads < 0:
            return float('inf')
        path_len = path_len + 1
        path.append("B")
        if heads == 0:
            print path
            return path_len
        heads += 2
        return min(A(heads, path, path_len), B(heads, path, path_len))
    
    A(10, [], 0)
    

    this gives the right answer. You can have a global variable to store the path instead of simple printing it

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Two issues that will likely be immediately obvious, but I've spent too long already
Two Questions: Will I get different sequences of numbers for every seed I put
Two points -- first, the example is in Fortran, but I think it should
Two questions: 1) There are several tables that are used as an archive for
Two questions, please. 1) I am reading about FEDERATED storage engine, but for me,
Two questions: How is the value returned from setInterval and setTimeout (the ones used
Two questions please: I need two foreign keys back to User, one for author
Two seemingly identical queries (as far as a newbie like me can tell, but
Two RAID volumes, VMware kernel/console running on a RAID1, vmdks live on a RAID5.
Two years ago, we shipped a multi-gigabyte Windows application, with lots of video files.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.