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的反射机制
Jun 28 Python
Python中的命令行参数解析工具之docopt详解
Mar 27 Python
简单谈谈Python中的元祖(Tuple)和字典(Dict)
Apr 21 Python
python中文件变化监控示例(watchdog)
Oct 16 Python
使用python中的in ,not in来检查元素是不是在列表中的方法
Jul 06 Python
Python OOP类中的几种函数或方法总结
Feb 22 Python
解决python 文本过滤和清理问题
Aug 28 Python
python sklearn常用分类算法模型的调用
Oct 16 Python
Python常用模块os.path之文件及路径操作方法
Dec 03 Python
python用pip install时安装失败的一系列问题及解决方法
Feb 24 Python
python3.7 openpyxl 在excel单元格中写入数据实例
Sep 01 Python
python pygame 愤怒的小鸟游戏示例代码
Feb 25 Python
浅谈Python用QQ邮箱发送邮件时授权码的问题
Jan 29 #Python
Python实现识别手写数字 简易图片存储管理系统
Jan 29 #Python
详解Python自建logging模块
Jan 29 #Python
python抓取网页中链接的静态图片
Jan 29 #Python
Python实现识别手写数字 Python图片读入与处理
Mar 23 #Python
Python实现PS滤镜特效Marble Filter玻璃条纹扭曲效果示例
Jan 29 #Python
Python实现识别手写数字大纲
Jan 29 #Python
You might like
php通过array_push()函数添加多个变量到数组末尾的方法
2015/03/18 PHP
Windows下Apache + PHP SESSION丢失的解决过程全纪录
2015/04/07 PHP
PHPTree――php快速生成无限级分类
2018/03/30 PHP
Javascript insertAfter() 实现函数代码
2011/10/12 Javascript
js实现拉伸拖动iframe的具体代码
2013/08/03 Javascript
js 自定义个性下拉选择框示例
2013/08/20 Javascript
jquery实现侧边弹出的垂直导航
2014/12/09 Javascript
jQuery后代选择器用法实例
2014/12/23 Javascript
JS+CSS实现可拖拽的漂亮圆角特效弹出层完整实例
2015/02/13 Javascript
js实现Select列表内容自动滚动效果代码
2015/08/20 Javascript
JavaScript中this的用法实例分析
2016/12/19 Javascript
详谈innerHTML innerText的使用和区别
2017/08/18 Javascript
对vue 键盘回车事件的实例讲解
2018/08/25 Javascript
Vue中Quill富文本编辑器的使用教程
2018/09/21 Javascript
echarts统计x轴区间的数值实例代码详解
2019/07/07 Javascript
[46:25]DOTA2上海特级锦标赛主赛事日 - 4 败者组第五轮 MVP.Phx VS EG第二局
2016/03/05 DOTA
Python 搭建Web站点之Web服务器网关接口
2016/11/06 Python
详解常用查找数据结构及算法(Python实现)
2016/12/09 Python
python2 与python3的print区别小结
2018/01/16 Python
python中实现字符串翻转的方法
2018/07/11 Python
使用Python实现跳帧截取视频帧
2019/05/31 Python
使用python对多个txt文件中的数据进行筛选的方法
2019/07/10 Python
树莓派使用python-librtmp实现rtmp推流h264的方法
2019/07/22 Python
python多线程与多进程及其区别详解
2019/08/08 Python
Python爬虫:url中带字典列表参数的编码转换方法
2019/08/21 Python
python通过文本在一个图中画多条线的实例
2020/02/21 Python
Html5移动端网页端适配(js+rem)
2021/02/03 HTML / CSS
KIKO MILANO英国官网:意大利知名化妆品和护肤品品牌
2017/09/25 全球购物
经典演讲稿范文
2013/12/30 职场文书
学生拾金不昧表扬信
2014/01/21 职场文书
对祖国的寄语大全
2014/04/11 职场文书
教师节倡议书
2014/08/30 职场文书
2014年个人工作总结范文
2014/11/07 职场文书
参观邀请函范文
2015/02/02 职场文书
2015年端午节国旗下演讲稿
2015/03/19 职场文书
会计求职自荐信
2015/03/26 职场文书