Question

def snake (grid, row, co1): Implement a function to calculate the longest snake of ones that can be made from a starting po
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Explanation: Using dfs to compute the longest snake length. We can also do this problem by using dynamic programming but as per the restriction we can't use max(). So I used dfs instead of dp.

Source Code:

def isSafe(grid, x, y, isVisited):

if (x < len(grid)) and (y < len(grid[0])) and (grid[x][y] == 1) and (isVisited[x][y] == False):

return True

else:

return False

def dfs(grid, row, col, isVisited):

if grid[row][col] == 0:

return 0

isVisited[row][col] = True

if isSafe(grid, row+1, col, isVisited):

return dfs(grid, row+1, col, isVisited)+1

if isSafe(grid, row, col+1, isVisited):

return dfs(grid, row, col+1, isVisited)+1

return 1




def snake(grid, row, col):

numRows = len(grid)

numCols = len(grid[0])

isVisited = [[False]*numCols for i in range(numRows)]

ans = dfs(grid, row, col, isVisited)

print(ans)

cells = [[1,0,1,0],[0,1,1,0],[1,1,0,1],[0,1,1,1]]

print(cells)

snake(cells, 0, 0)

snake(cells, 0, 2)

snake(cells, 1, 1)

Output:

[[1, 0, 1, 0], [0, 1, 1, 0], [1, 1, 0, 1], [0, 1, 1, 1]]
1
2
5

Image for clarity:

def isSafe(grid, x, y, isVisited): if (x 〈 len (grid)) and (y 〈 len(grid[0])) and (grid[x][y]-1) and (îsVisited [x][y] False)

Add a comment
Know the answer?
Add Answer to:
Def snake (grid, row, co1): Implement a function to calculate the longest "snake" of ones that ca...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You must use recursion to solve each problem. You cannot use loops in this homework. You...

    You must use recursion to solve each problem. You cannot use loops in this homework. You cannot import any module. A couple of the tasks have individual restrictions; note them, as we will remove points for any task that does not follow the requirements. def factorial_evens(num): Implement a function to calculate and return the product of all even numbers from 1 up to num (inclusive if num is even). o Assumption: num will be an integer greater than or equal...

  • 1)def toggle_cell(row, column, cells): • Return value: Flip the cell in the column of row from...

    1)def toggle_cell(row, column, cells): • Return value: Flip the cell in the column of row from alive to dead or vice versa. Return True if the toggle was successful, False otherwise. • Assumptions: o cells will be a two-dimensional list with at least one row and one element in that row. o row and column are index values of cells • Notes: o Cells is being edited in place. Note that the return value is a Boolean and not a...

  • Make sure to include: Ship.java, ShipTester.java, Location.java, LocationTester.java, Grid.java, GridTester.java, Player.java, PlayerTester.java, battleship.java. please do every...

    Make sure to include: Ship.java, ShipTester.java, Location.java, LocationTester.java, Grid.java, GridTester.java, Player.java, PlayerTester.java, battleship.java. please do every part, and include java doc and comments Create a battleship program. Include Javadoc and comments. Grade 12 level coding style using java only. You will need to create a Ship class, Location class, Grid Class, Add a ship to the Grid, Create the Player class, the Battleship class, Add at least one extension. Make sure to include the testers. Starting code/ sample/methods will be...

  • Maze Solving with Stacks Problem Statement Consider a maze made up of rectangular array of squares,...

    Maze Solving with Stacks Problem Statement Consider a maze made up of rectangular array of squares, such as the following one: X X X X X X X X X X X X X           X            X X X X    X X X           X               X     X X X     X X    X    X     X     X X X         X          X             X X X     X X X X X                X X X X X X X X X X X X X Figure...

  • C#: Implement a multiplayer Battleship game with AI The rules are the same as before. The...

    C#: Implement a multiplayer Battleship game with AI The rules are the same as before. The game is played on an NxN grid. Each player will place a specified collection of ships: The ships will vary in length (size) from 2 to 5; There can be any number or any size ship. There may be no ships of a particular size; EXCEPT the battleship – which there will always be 1 and only 1. Player order will be random but...

  • The ACME Manufacturing Company has hired you to help automate their production assembly line. Cameras have...

    The ACME Manufacturing Company has hired you to help automate their production assembly line. Cameras have been placed above a conveyer belt to enables parts on the belt to be photographed and analyzed. You are to augment the system that has been put in place by writing C code to detect the number of parts on the belt, and the positions of each object. The process by which you will do this is called Connected Component Labeling (CCL). These positions...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT