# Python编程练习.机器人行走

1.LeetCode974 模拟行走机器人

class Solution:
def robotSim(self, commands, obstacles):
"""
:type commands: List[int]
:type obstacles: List[List[int]]
:rtype: int
"""
ans=0
d={'+x':0,'-x':0,'+y':0,'-y':0}
f={'+x':1,'-x':1,'+y':0,'-y':0}
obstacleSet = set(map(tuple, obstacles))  #关键步
direction='+y'
direction_change={'+y-1':'+x','+y-2':'-x','-y-2':'+x','-y-1':'-x','+x-2':'+y','+x-1':'-y','-x-2':'-y','-x-1':'+y'}
x=0
y=0
for i in commands:
if i>0:
for k in range(i):
d[direction]+=1
coordinate = (d['+x'] - d['-x'], d['+y'] - d['-y'])
if coordinate in obstacleSet:
d[direction]-=1
break

else:
direction=direction_change[direction+str(i)]
ans=max(([d['+x']-d['-x'],d['+y']-d['-y']][0]**2+[d['+x']-d['-x'],d['+y']-d['-y']][1]**2),ans)
return(ans)


2.LeetCode62 不同路径

class Solution:
def uniquePaths(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
# C^(m-1)_(m+n-2)
res = 1
for i in range(m, m+n-1):
res *= i
res /= i-m+1
return int(res)
#代码源于LeetCode提交记录提交用时最少的范例。


05-14 1万+

01-16 1万+

03-17 9885

07-09 3805

07-18 398

02-28 198

03-12 8674

12-11 1583

08-23 146

10-24 636

04-07

12-27

11-16 1万+

11-13

08-13 618

12-08 506

04-22 4642

08-29 269

06-20 1万+

04-30 1225

08-25 944

04-18 1887

07-19 1万+

12-11 738

08-22 1226

08-23 507

01-26 3757

09-05 391

06-18 122

10-08 6553

11-30 412

11-19 5万+

01-15 5137

06-27 1231

05-14 2万+

08-01 1394

06-30 105

03-18 158

06-20 4352

06-02 483

08-21 367

09-20 960

09-08 1万+

01-09 14

01-27 4万+

06-19 264

04-30 3万+

04-02 1万+

10-12 4158

03-26 521

10-03 3296

06-29 67

04-26 4293

07-23 1390

05-07 3万+

07-27 5322

05-03 1347

09-07 2682

10-24 3756

02-15 18万+

06-08 442

02-15 1万+

11-12 2858

02-14 7万+

02-19 9万+

02-25 37万+

08-06 867

03-13 14万+

03-01 13万+

03-01 11万+

04-14 58万+

02-06 19万+

11-28 3万+

02-03 18万+

01-21 28万+

01-05 27万+

03-19 81万+

#### 大学四年自学走来，这些私藏的实用工具/学习网站我贡献出来了

©️2019 CSDN 皮肤主题: 书香水墨 设计师: CSDN官方博客