Solving the python robot walking steps

Source: Internet
Author: User

Solving the python robot walking steps

This article provides examples to share with you the python robot walking steps for your reference. The specific content is as follows:

#! /Usr/bin/env python3 #-*-coding: UTF-8-*-# fileName: robot_path.py # author: zoujiameng@aliyun.com.cn # There is a box of m rows and n columns on the ground. A robot starts to move from the grid at coordinates 0, 0. Each time, it can only move one cell to the left, right, top, and bottom directions, however, you cannot enter a grid where the sum of the numbers of the row and column coordinates is greater than k. # For example, when k is 18, the robot can enter the square (35, 37) Because 3 + 5 + 3 + 7 = 18. However, it cannot enter the square (35, 38) Because 3 + 5 + 3 + 8 = 19. How many grids can this robot reach? Class Robot: # shared interface to determine whether the value exceeds K def getDigitSum (self, num): sumD = 0 while (num> 0 ): sumD + = num % 10 num/= 10 return int (sumD) def PD_K (self, rows, cols, K): sumK = self. getDigitSum (rows) + self. getDigitSum (cols) if sumK> K: return False else: return True def PD_K1 (self, I, j, k): "determine whether the location can go, set complex constraints to "index = map (str, [I, j]) sum_ij = 0 for x in index: for y in x: sum_ij + = int (y) if sum_ij <= k: return Tr Ue else: return False # shared interface: print the traversal visited two-dimensional list def printMatrix (self, matrix, r, c): print ("cur location (", r ,", ", c,") ") for x in matrix: for y in x: print (y, end ='') print () # backtracking def hasPath (self, threshold, rows, cols): visited = [[0 for j in range (cols)] for I in range (rows)] count = 0 startx = 0 starty = 0 # print (threshold, rows, cols, visited) visited = self. findPath (threshold, rows, co Ls, visited, startx, starty,-1,-1) for x in visited: for y in x: if (y = 1): count + = 1 print (visited) return count def findPath (self, threshold, rows, cols, visited, curx, cury, prex, prey ): if 0 <= curx <rows and 0 <= cury <cols and self. PD_K1 (curx, cury, threshold) and visited [curx] [cury]! = 1: # determine whether the current vertex meets the condition visited [curx] [cury] = 1 self. printMatrix (visited, curx, cury) prex = curx prey = cury if cury + 1 <cols and self. PD_K1 (curx, cury + 1, threshold) and visited [curx] [cury + 1]! = 1: # east visited [curx] [cury + 1] = 1 return self. findPath (threshold, rows, cols, visited, curx, cury + 1, prex, prey) elif cury-1> = 0 and self. PD_K1 (curx, cury-1, threshold) and visited [curx] [cury-1]! = 1: # west visited [curx] [cury-1] = 1 return self. findPath (threshold, rows, cols, visited, curx, cury-1, prex, prey) elif curx + 1 <rows and self. PD_K1 (curx + 1, cury, threshold) and visited [curx + 1] [cury]! = 1: # sourth visited [curx + 1] [cury] = 1 return self. findPath (threshold, rows, cols, visited, curx + 1, cury, prex, prey) elif 0 <= curx-1 and self. PD_K1 (curx-1, cury, threshold) and visited [curx-1] [cury]! = 1: # north visited [curx-1] [cury] = 1 return self. findPath (threshold, rows, cols, visited, curx-1, cury, prex, prey) else: # return to the previous layer, here there is a problem with return visited # self. findPath (threshold, rows, cols, visited, curx, cury, prex, prey) # Backtracking Method 2 def movingCount (self, threshold, rows, cols ): visited = [[0 for j in range (cols)] for I in range (rows)] print (visited) count = self. movingCountCore (threshold, rows, cols, 0, 0, visited); print (visited) return count def movingCountCore (self, threshold, rows, cols, row, col, visited): cc = 0 if (self. check (threshold, rows, cols, row, col, visited): visited [row] [col] = 1 cc = 1 + self. movingCountCore (threshold, rows, cols, row + 1, col, visited) + self. movingCountCore (threshold, rows, cols, row, col + 1, visited) + self. movingCountCore (threshold, rows, cols, row-1, col, v Isited) + self. movingCountCore (threshold, rows, cols, row, col-1, visited) return cc def check (self, threshold, rows, cols, row, col, visited ): if (0 <= row <rows and 0 <= col <cols and (self. getDigitSum (row) + self. getDigitSum (col) <= threshold and visited [row] [col]! = 1): return True; return False # The force action. Compare def force (self, rows, cols, K) with the current coordinate and k ): count = 0 for I in range (rows): for j in range (cols): if self. PD_K (I, j, k): count + = 1 return count # brute force 2. Use Recursion TO DO def block (self, r, c, k ): s = sum (map (int, str (r) + str (c) return s> k def con_visited (self, rows, cols ): visited = [[0 for j in range (cols)] for I in range (rows)] return visited def traval (self, r, c, ro Ws, cols, k, visited): if not (0 <= r <rows and 0 <= c <cols): return if visited [r] [c]! = 0 or self. block (r, c, k): visited [r] [c] =-1 return visited [r] [c] = 1 global acc + = 1 self. traval (r + 1, c, rows, cols, k, visited) self. traval (r, c + 1, rows, cols, k, visited) self. travel (R-1, c, rows, cols, k, visited) self. traval (r, C-1, rows, cols, k, visited) return acc if _ name _ = "_ main __": # Call test m = 3 n = 3 k = 1 o = Robot () print (o. hasPath (k, m, n) print (o. force (m, n, k) global acc = 0 print (o. traval (0, 0, m, n, k, o. con_visited (m, n) print (o. movingCount (k, m, n ))

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.