python机器人行走步数问题的解决


Posted in Python onJanuary 29, 2018

本文实例为大家分享了python机器人行走步数问题,供大家参考,具体内容如下

#! /usr/bin/env python3 
# -*- coding: utf-8 -*- 
# fileName : robot_path.py 
# author : zoujiameng@aliyun.com.cn 
 
# 地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。  
# 例如,当k为18时,机器人能够进入方格(35,37),因为3+5+3+7 = 18。但是,它不能进入方格(35,38),因为3+5+3+8 = 19。请问该机器人能够达到多少个格子? 
class Robot: 
# 共用接口,判断是否超过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): 
    "确定该位置是否可以走,将复杂约束条件设定" 
    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 True 
    else: 
      return False 
 
# 共用接口,打印遍历的visited二维list 
  def printMatrix(self, matrix, r, c): 
    print("cur location(", r, ",", c, ")") 
    for x in matrix: 
      for y in x:  
        print(y, end=' ') 
      print() 
 
 #回溯法 
  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, cols, 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: # 判断当前点是否满足条件 
      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 visited#self.findPath(threshold, rows, cols, visited, curx, cury, prex, prey) 
 #回溯法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, visited) + 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  
 
# 暴力法,直接用当前坐标和K比较 
  def force(self, rows, cols, k): 
    count = 0 
    for i in range(rows): 
      for j in range(cols): 
        if self.PD_K(i, j, k): 
          count+=1 
    return count 
# 暴力法2, 用递归法来做 
  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, rows, 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 
    acc+=1 
    self.traval(r+1, c, rows, cols, k, visited) 
    self.traval(r, c+1, rows, cols, k, visited) 
    self.traval(r-1, c, rows, cols, k, visited) 
    self.traval(r, c-1, rows, cols, k, visited) 
    return acc 
 
if __name__ == "__main__": 
  # 调用测试 
  m = 3 
  n = 3 
  k = 1 
  o = Robot() 
  print(o.hasPath(k, m, n)) 
  print(o.force(m,n,k)) 
  global acc 
  acc = 0 
  print(o.traval(0, 0, m, n, k, o.con_visited(m,n))) 
  print(o.movingCount(k, m, n))

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中的匿名函数使用简介
Apr 27 Python
Python中time模块和datetime模块的用法示例
Feb 28 Python
python实现感知器
Dec 19 Python
TensorFlow 实战之实现卷积神经网络的实例讲解
Feb 26 Python
用Django实现一个可运行的区块链应用
Mar 08 Python
python实现连续图文识别
Dec 18 Python
python gdal安装与简单使用
Aug 01 Python
python conda操作方法
Sep 11 Python
如何在django中添加日志功能
Feb 06 Python
关于python 跨域处理方式详解
Mar 28 Python
python os模块常用的29种方法使用详解
Jun 02 Python
python tkinter的消息框模块(messagebox,simpledialog)
Nov 07 Python
python的socket编程入门
Jan 29 #Python
Python 错误和异常代码详解
Jan 29 #Python
python实现机器人行走效果
Jan 29 #Python
浅谈Python用QQ邮箱发送邮件时授权码的问题
Jan 29 #Python
Python实现识别手写数字 简易图片存储管理系统
Jan 29 #Python
详解Python自建logging模块
Jan 29 #Python
python抓取网页中链接的静态图片
Jan 29 #Python
You might like
php中检查文件或目录是否存在的代码小结
2012/10/22 PHP
教你如何用php实现LOL数据远程获取
2014/06/10 PHP
ThinkPHP模版引擎之变量输出详解
2014/12/05 PHP
CentOS安装php v8js教程
2015/02/26 PHP
PHP7新增运算符用法实例分析
2016/09/26 PHP
php fread函数使用方法总结
2019/05/28 PHP
php中的依赖注入实例详解
2019/08/14 PHP
常见的5个PHP编码小陋习以及优化实例讲解
2021/02/27 PHP
[原创]js与自动伸缩图片 自动缩小图片的多浏览器兼容的方法总结
2007/03/12 Javascript
获取HTML DOM节点元素的方法的总结
2009/08/21 Javascript
jquery 弹出层实现代码
2009/10/30 Javascript
JavaScript与Image加载事件(onload)、加载状态(complete)
2011/02/14 Javascript
javascript实现显示和隐藏div方法汇总
2015/08/14 Javascript
jquery实现初次打开有动画效果的网页TAB切换代码
2015/09/06 Javascript
jQuery插件HighCharts绘制的2D堆柱状图效果示例【附demo源码下载】
2017/03/14 Javascript
微信小程序 中wx.chooseAddress(OBJECT)实例详解
2017/03/31 Javascript
vue2.0实现音乐/视频播放进度条组件
2018/06/06 Javascript
nodejs 如何手动实现服务器
2018/08/20 NodeJs
为vue项目自动设置请求状态的配置方法
2019/06/09 Javascript
Vue中util的工具函数实例详解
2019/07/08 Javascript
解决layui的table插件无法多层级获取json数据的问题
2019/09/19 Javascript
浅谈webpack和webpack-cli模块源码分析
2020/01/19 Javascript
Python3.4学习笔记之常用操作符,条件分支和循环用法示例
2019/03/01 Python
画pytorch模型图,以及参数计算的方法
2019/08/17 Python
python 制作python包,封装成可用模块教程
2020/07/13 Python
详解HTML5 录音的踩坑之旅
2017/12/26 HTML / CSS
详解h5页面在不同ios设备上的问题总结
2019/03/01 HTML / CSS
NIHAOMARKET官方海外旗舰店:意大利你好华人超市
2018/01/27 全球购物
意大利奢侈品购物网站:Deliberti
2019/10/08 全球购物
英国最大的天然和有机产品在线零售商之一:Big Green Smile
2020/05/06 全球购物
光声世纪笔试题目
2012/08/25 面试题
应届护士推荐信
2013/11/16 职场文书
工商局局长个人对照检查材料思想汇报
2014/09/23 职场文书
幼儿园教师节感谢信
2015/01/23 职场文书
试用期自我评价范文
2015/03/10 职场文书
ORACLE数据库对long类型字段进行模糊匹配的解决思路
2021/04/07 Oracle