Python:每日一题 123
本帖最后由 冬雪雪冬 于 2017-11-19 15:11 编辑先我们的玩法做了一下改变:
1. 楼主不再提供答案。
2. 请大家先独立思考”,再参考其他鱼油的解答,这样才有助于自己编程水平的提高。
3. 鼓励大家积极答题,奖励的期限为出题后24小时内。
4. 根据答案的质量给予1~3鱼币的奖励。
题目:
有如下的一个10X10的迷宫,从左往右自A1进入,遇到1方向不变直行,遇到2右转90°,遇到3转180°即掉头,遇到4左转90°,直到从右下角A10走出。统计你走过的格子的数字之和,注意重复走过的格子只算一次,如D4走过2次,计数只算1。
1 1 1 2 1 1 2 4 4 3
2 1 1 1 1 1 1 1 4 2
1 2 3 1 4 2 1 1 2 4
2 1 1 1 1 1 2 1 4 1
1 1 1 3 2 2 3 1 2 3
3 2 4 1 1 1 1 1 1 4
2 1 1 1 1 2 2 1 4 1
1 2 1 3 2 1 2 4 1 4
1 2 1 1 1 1 4 2 2 2
2 1 2 1 2 2 3 4 1 1
=========================================
答案是:113
走过的路径如下:
{:5_108:} 和是112
经过81步
# -*- coding: utf-8 -*-
"""
Created on Sun Nov 12 22:34:26 2017
"""
puzzle =[
,
,
,
,
,
,
,
,
,
]
def next_step(ind_row, ind_col, cur_dir, cur_val):
if cur_dir == 1:
if(cur_val == 1):
next_dir = 1
next_row = ind_row
next_col = ind_col+1
elif cur_val == 2:
next_dir = 4
next_row = ind_row +1
next_col = ind_col
elif cur_val == 3:
next_dir = 3
next_row = ind_row
next_col = ind_col - 1
elif cur_val == 4:
next_dir = 2
next_row = ind_row -1
next_col = ind_col
else:
print ("Error direction")
elif cur_dir == 2:
if(cur_val == 1):
next_dir = 2
next_row = ind_row -1
next_col = ind_col
elif cur_val == 2:
next_dir = 1
next_row = ind_row
next_col = ind_col + 1
elif cur_val == 3:
next_dir = 4
next_row = ind_row + 1
next_col = ind_col
elif cur_val == 4:
next_dir = 3
next_row = ind_row
next_col = ind_col -1
else:
print ("Error direction")
elif cur_dir == 3:
if(cur_val == 1):
next_dir = 3
next_row = ind_row
next_col = ind_col - 1
elif cur_val == 2:
next_dir = 2
next_row = ind_row - 1
next_col = ind_col
elif cur_val == 3:
next_dir = 1
next_row = ind_row
next_col = ind_col + 1
elif cur_val == 4:
next_dir = 4
next_row = ind_row + 1
next_col = ind_col
else:
print ("Error direction")
elif cur_dir == 4:
if(cur_val == 1):
next_dir = 4
next_row = ind_row + 1
next_col = ind_col
elif cur_val == 2:
next_dir = 3
next_row = ind_row
next_col = ind_col - 1
elif cur_val == 3:
next_dir = 2
next_row = ind_row - 1
next_col = ind_col
elif cur_val == 4:
next_dir = 1
next_row = ind_row
next_col = ind_col + 1
else:
print ("Error direction")
else:
print ("Error direction")
return
p_row = len(puzzle)
p_col = len(puzzle)
pos_pass = [ for j in range(p_row)]
cur_dir = 1
ind_row = 0
ind_col = 0
cur_val = puzzle
sum_val = cur_val
#next_step(ind_row, ind_col, cur_dir, cur_val):
next_time = next_step(ind_row, ind_col, cur_dir, cur_val)
pos_pass = 1
steps = 2
while(next_time<p_row-1 or next_time<p_col-1):
ind_row = next_time
ind_col = next_time
cur_dir = next_time
cur_val = puzzle
if pos_pass == 0:
sum_val = sum_val + puzzle
pos_pass = steps
steps = steps +1
next_time = next_step(ind_row, ind_col, cur_dir, cur_val)
print(sum_val)
for i in pos_pass:
print(i) matrix = [
,
,
,
,
,
,
,
,
,
]
def nextpos(x,y,px,py,matrix=matrix):
if matrix==1: return 2*x-px,2*y-py
elif matrix==2:
if x-px==0 and y-py==1: return x+1,y
elif x-px==-1 and y-py==0: return x,y+1
elif x-px==0 and y-py==-1: return x-1,y
elif x-px==1 and y-py==0: return x,y-1
elif matrix==3: return px,py
elif matrix==4:
if x-px==0 and y-py==1: return x-1,y
elif x-px==-1 and y-py==0: return x,y-1
elif x-px==0 and y-py==-1: return x+1,y
elif x-px==1 and y-py==0: return x,y+1
prev=(0,0)
cur=(0,1)
pos=[(0,0),(0,1)]
while cur!=(9,9):
next = nextpos(cur,cur,prev,prev)
prev = cur
cur = next
pos.append(cur)
print(sum(matrix for x,y in set(pos)))
113
import copy
def f(n,speed):
if n==1:
speed = speed
if n==2:
if speed==0:
if speed==1:
speed=-1
if speed==-1:
speed=1
speed=0
elif speed==0:
if speed==-1:
speed=-1
if speed==1:
speed=1
speed=0
if n==3:
speed=
if n==4:
if speed==0:
if speed==1:
speed=1
if speed==-1:
speed=-1
speed=0
elif speed==0:
if speed==-1:
speed=1
if speed==1:
speed=-1
speed=0
return speed
speed=
game=[,
,
,
,
,
,
,
,
,
]
a=copy.deepcopy(game)
c=[]
x,y=0,0
time=0
while x !=10 and y !=10:
n=game
c.append(a)
a=0
time +=1
print(n,'→',end='')
speed=f(n,speed)
x=x+speed
y=y+(-1*speed)
print('\n','answer=',sum(c))
s = '''1 1 1 2 1 1 2 4 4 3
2 1 1 1 1 1 1 1 4 2
1 2 3 1 4 2 1 1 2 4
2 1 1 1 1 1 2 1 4 1
1 1 1 3 2 2 3 1 2 3
3 2 4 1 1 1 1 1 1 4
2 1 1 1 1 2 2 1 4 1
1 2 1 3 2 1 2 4 1 4
1 2 1 1 1 1 4 2 2 2
2 1 2 1 2 2 3 4 1 1'''
ss = ''
for i in s:
if i != ' ':
ss += i
road =
a = 0
b = 0
ort = 'R'
result = dict()
def in_box(a):
if a not in range(10):
return False
return True
def walk(a,b,ort,result,road):
ab = str(a) + str(b)
result.update({ab: road})
if road == '1':
ort == ort
if ort == 'R':
b += 1
elif ort == 'L':
b -= 1
elif ort == 'U':
a -= 1
elif ort == 'D':
a += 1
elif road == '2':
if ort == 'R':
a += 1
ort = 'D'
elif ort == 'L':
a -= 1
ort = 'U'
elif ort == 'U':
b += 1
ort = 'R'
elif ort == 'D':
b -= 1
ort = 'L'
elif road == '3':
if ort == 'R':
b -= 1
ort = 'L'
elif ort == 'L':
b += 1
ort = 'R'
elif ort == 'U':
a += 1
ort = 'D'
elif ort == 'D':
a -= 1
ort = 'U'
elif road == '4':
if ort == 'R':
a -= 1
ort = 'U'
elif ort == 'L':
a += 1
ort = 'D'
elif ort == 'U':
b -= 1
ort = 'L'
elif ort == 'D':
b += 1
ort = 'R'
if a == 9 and b == 9:
ab = str(9) + str(9)
result.update({ab:road})
return result
if in_box(a) or in_box(b):
walk(a, b, ort, result, road)
return result
r = walk(a,b,ort,result,road)
print(sum(int(i) for i in r.values())) 肯定是一组唯一的设定好的数字
思维定格了,想不到有什么很纯粹的处理{:10_269:}
只好按照题目当成走迷宫来处理了
maze_ex = """
1 1 1 2 1 1 2 4 4 3
2 1 1 1 1 1 1 1 4 2
1 2 3 1 4 2 1 1 2 4
2 1 1 1 1 1 2 1 4 1
1 1 1 3 2 2 3 1 2 3
3 2 4 1 1 1 1 1 1 4
2 1 1 1 1 2 2 1 4 1
1 2 1 3 2 1 2 4 1 4
1 2 1 1 1 1 4 2 2 2
2 1 2 1 2 2 3 4 1 1"""
def fun(maze):
functions = , pos+1),
lambda pos: (pos+1, pos),
lambda pos: (pos, pos-1),
lambda pos: (pos-1, pos)] # 右、下、左、上
maze_str = (j.split() for j in maze.strip().split('\n'))
maze = [ for j in maze_str]
row, column = len(maze), len(maze)
flag = [ * column for _ in range(row)] # 标记路径
flag = 1
x, y, direction= 0, 0, 0 # 初始行坐标、列坐标、移动方向
while (x, y) != (row -1, column -1):
direction = (direction + maze - 1) % 4
x, y = functions((x,y))
flag = 1
return sum(maze for i in range(row) for j in range(column) if flag) 我想,应该涉及到矩阵旋转之类的,
可以考虑map()与zip()什么的,
具体思路,没有......55555 到头了怎么走,从另一端进入?贪吃蛇吗 本帖最后由 wyp02033 于 2017-11-14 16:11 编辑
def main():
maze = [,
,
,
,
,
,
,
,
,
]
width = height = 9
speed =
start =
end =
position = start
passed_position = []
all_num_sum = 0
while True:
position_str = str(position) + str(position)
if position_str not in passed_position:
passed_position.append(position_str)
i, j = position
#定义右转
if maze == 2:
if speed == :
speed =
elif speed == :
speed = [-1, 0]
elif speed == [-1, 0]:
speed =
else:
speed =
#定义反向
elif maze == 3:
speed = -speed
speed = -speed
#定义左转
elifmaze == 4:
if speed == :
speed =
elif speed == :
speed = [-1, 0]
elif speed == [-1, 0]:
speed =
else:
speed =
else:
pass
position += speed
position += speed
if position > width:
position = 0
elif position < 0:
position = width
elif position > height:
position = 0
elif position < 0:
position = height
if position == end:
passed_position.append(str(end) + str(end))
break
for each in passed_position:
i, j = int(each), int(each)
all_num_sum += maze
print(all_num_sum)
if __name__ == "__main__":
main()
运行结果:113 import numpy as np
arr = '''
1 1 1 2 1 1 2 4 4 3
2 1 1 1 1 1 1 1 4 2
1 2 3 1 4 2 1 1 2 4
2 1 1 1 1 1 2 1 4 1
1 1 1 3 2 2 3 1 2 3
3 2 4 1 1 1 1 1 1 4
2 1 1 1 1 2 2 1 4 1
1 2 1 3 2 1 2 4 1 4
1 2 1 1 1 1 4 2 2 2
2 1 2 1 2 2 3 4 1 1
'''
a = np.fromstring(arr, int, sep=' ').reshape(10,10)
z = np.zeros_like(a)
def fn():
cood = np.array() #当前坐标
rot = np.array([,,,[-1,0]]) #转向
dirct = 0 # 方向
while cood.tolist() != :
val = a
z = val
dirct = (dirct+val-1)%4
cood += rot
z = a
return z.sum()
s = fn()
print(s) # 113 import numpy as np
a=np.array ()
b=np.array ()
c=np.array ()
d=np.array ()
e=np.array ()
f= np.array()
g =np.array()
h =np.array()
i =np.array()
j= np.array()
shuzu = np.array()
list1=[]
i=0
j=0
dir =1
sum =0
while (i,j) != (9,9):
if dir ==1:
if shuzu ==1:
j = j+1
list1.append()
elif shuzu==2:
dir=2
i=i+1
list1.append()
elif shuzu==3:
dir =3
j=j-1
list1.append()
elif shuzu ==4:
dir =4
i= i -1
list1.append()
elif dir ==3:
if shuzu ==1:
j=j-1
list1.append()
if shuzu ==2:
dir =4
i=i-1
list1.append()
if shuzu==3:
dir=1
j=j+1
list1.append()
ifshuzu ==4:
dir =2
i=i+1
list1.append()
elif dir ==2:
if shuzu ==1:
i=i+1
list1.append()
if shuzu ==2:
dir =3
j=j-1
list1.append()
if shuzu==3:
dir=4
i=i-1
list1.append()
ifshuzu ==4:
dir =1
j=j+1
list1.append()
elif dir ==4:
if shuzu ==1:
i=i-1
list1.append()
if shuzu ==2:
dir =1
j=j+1
list1.append()
if shuzu==3:
dir=2
i=i+1
list1.append()
ifshuzu ==4:
dir =3
j=j-1
list1.append()
list2=[]
list3=[]
for i in list1:
if i not in list2:
list2.append(i)
print(list2)
for i,j in list2:
list3.append(shuzu)
for i in list3:
sum+=i
print(sum)
{:5_106:}{:5_106:} from numpy import *
right = array()
left = array()
up = array([-1,0])
down = array()
matrix = mat([,\
,\
,\
,\
,\
,\
,\
,\
,\
])
flag = zeros((10,10),dtype = int16)
position = array()
direction = right
count = 0
while ,position] != :
# 如果没有被踩过,计数
if not flag,position]:
flag,position] = 1
count += 1
# 直走
if matrix,position] == 1:
position += direction
print(position)
#碰到2 右转
elif matrix,position] == 2:
if direction == right and direction == right:
direction = down
elif direction == down and direction == down:
direction = left
elif direction == left and direction == left:
direction = up
elif direction == up and direction == up:
direction = right
position += direction
print(position)
#碰到3 掉头
elif matrix,position] == 3:
if direction == right and direction == right:
direction = left
elif direction == left and direction == left:
direction = right
elif direction == up and direction == up:
direction = down
elif direction == down and direction == down:
direction = up
position += direction
print(position)
#碰到4 左转
elif matrix,position] == 4:
if direction == right and direction == right:
direction = up
elif direction == up and direction == up:
direction = left
elif direction == left and direction == left:
direction = down
elif direction == down and direction == down:
direction = right
position += direction
print(position)
print(count)
# result
'''
69
'''
from migongUI import Ui_Form
from PyQt5 import QtWidgets , QtCore,QtGui
from PyQt5.QtCore import QTimer#导入定时器类
import re
import sys
import copy
import time
import threading
"""
有如下的一个10X10的迷宫,从左往右自A1进入,遇到1方向不变直行,遇到2右转90°,遇到3转180°即掉头,
遇到4左转90°,直到从右下角A10走出。统计你走过的格子的数字之和,注意重复走过的格子只算一次,
如D4走过2次,计数只算1。
数列如下
1 1 1 2 1 1 2 4 4 3
2 1 1 1 1 1 1 1 4 2
1 2 3 1 4 2 1 1 2 4
2 1 1 1 1 1 2 1 4 1
1 1 1 3 2 2 3 1 2 3
3 2 4 1 1 1 1 1 1 4
2 1 1 1 1 2 2 1 4 1
1 2 1 3 2 1 2 4 1 4
1 2 1 1 1 1 4 2 2 2
2 1 2 1 2 2 3 4 1 1
"""
class mywindow(QtWidgets.QMainWindow):
def __init__(self,parent=None):
QtWidgets.QWidget.__init__(self, parent)
self.ui = Ui_Form()
self.ui.setupUi(self)
self.LE_list=[]
self.timer=QTimer(self)# 初始化定时器对象
self.conut=0
self.timer.timeout.connect(self.showNum)# 绑定槽函数
# self.startCount() # 启动定时器
self.delay()
def start(self):
mycar = Car()
mydelay = Delay()
coordinates = int(mycar.hang) * 10 + int(mycar.lie) - 1# 计算坐标 -1是因为列表从0开始的
num=""
result=0
while coordinates !=99:
# 设置密码显示,代表mycar的当前位置
QtWidgets.QLineEdit.setEchoMode(self.LE_list, QtWidgets.QLineEdit.PasswordEchoOnEdit)
num=num+str(coordinates)+'\n'
self.ui.TE_MID_NUM.setText(num)
result = result + mycar.num
self.ui.TE_RECLUT.setText(str(result))
mycar.num=int(self.LE_list.text())
# 计算前进的方向
mycar.to_next()
# 根据方向计算坐标
mycar.go_step(mycar.direct)
print(mycar.direct)
app.processEvents()# 实时更新
mydelay.delay_1s()
QtWidgets.QLineEdit.setEchoMode(self.LE_list, QtWidgets.QLineEdit.Normal)
# 计算坐标对应的文本框
coordinates = int(mycar.hang) * 10 + int(mycar.lie) - 1
pass
def delay(self):
self.timer.start(1000)
def showNum(self):
self.conut=self.conut+1
# print("dingshiqi",self.conut)
def Data_init(self,num_list,TE_tmp):
pass
def myfindchild(self,P_list,i):
FindTE =self.findChild((QtWidgets.QLineEdit,), "LE_"+str(i))# 按名字查找窗口组件
self.LE_list.append(FindTE)
QtWidgets.QLineEdit.setText(FindTE,str(P_list))# 对LineEdit统一赋值
class Car(object):
# 初始化 行数为0 列数为1 方向向东 数字为0
def __init__(self):
self.hang = 0 # 迷宫行坐标
self.lie = 1 # 迷宫列坐标
self.direct = 1 # 北 0 东 1南 2 西 3
self.num = 1 # 读取迷宫格子的值
def go_step(self,direct): # 前进一格
if self.direct == 0:
self.hang = self.hang - 1
if self.direct == 1:
self.lie = self.lie + 1
if self.direct == 2:
self.hang = self.hang + 1
if self.direct == 3:
self.lie = self.lie - 1
return
def to_next(self):
if self.num == 1: # 1 方向不变
pass
if self.num == 2: # 2 方向右转
self.direct = self.direct+1
if self.direct > 3:
self.direct = 0
if self.num == 3: # 3 方向掉头
self.direct = self.direct + 2
if self.direct > 3: # 如果大于等于4 需要减去4
self.direct = self.direct-4
if self.num == 4: # 4 方向向左
self.direct = self.direct + 3
if self.direct > 3: # 如果大于等于4 需要减去4
self.direct = self.direct - 4
class Delay(object):
# 延时500ms
def delay_500ms(self):
time.sleep(0.5)
# 延时一秒
def delay_1s(self):
time.sleep(1)
# 添加线程实现更新
class mythread(threading.Thread):
def __init__(self,mywindow):
threading.Thread.__init__(self)
self.mywindow=mywindow
def run(self):
while i != 99:
pass
pass
if __name__ == "__main__":
list=[,
,
,
,
,
,
,
,
,
]
app = QtWidgets.QApplication(sys.argv)
myapp = mywindow()
myapp.show()
k=0
# 迷宫 数字初始化
for i in range(10):
for j in range(10):
# print(list)
k = i * 10 + j + 1
myapp.myfindchild(list, k)
sys.exit(app.exec_())
matrix = [
,
,
,
,
,
,
,
,
,
]
matrixDict = {}
for i in range(10):
for j in range(10):
matrixDict = matrix
points = [] # 存放走过的点
pos = # 初始位置
direction = 1 # 初始方向 {1:Right, 2:Left, 3:Up, 4:Down}
points.append(pos*10+pos)
while not (pos == 9 and pos == 9):
if direction == 1:
pos += 1
if matrix]] == 2: # 右转90°
direction = 4
elif matrix]] == 3: # 调头
direction = 2
elif matrix]] == 4: # 左转90°
direction = 3
elif direction == 2:
pos -= 1
if matrix]] == 2: # 右转90°
direction = 3
elif matrix]] == 3: # 调头
direction = 1
elif matrix]] == 4: # 左转90°
direction = 4
elif direction == 3:
pos -= 1
if matrix]] == 2: # 右转90°
direction = 1
elif matrix]] == 3: # 调头
direction = 4
elif matrix]] == 4: # 左转90°
direction = 2
elif direction == 4:
pos += 1
if matrix]] == 2: # 右转90°
direction = 2
elif matrix]] == 3: # 调头
direction = 3
elif matrix]] == 4: # 左转90°
direction = 1
points.append(pos*10+pos)
points = set(points)
print(sum( for i in points]))
# 113 A=
B=
C=
D=
E=
F=
G=
H=
I=
J=
Maze=
Path=set()
i=0
j=0
count=0
direction='E'
while i!=9 or j!=9:
print(Maze)
if Maze==1:
tuple_path=(i,j)
flage_0=len(Path)
Path.add(tuple_path)
flage_1=len(Path)
if flage_0!=flage_1:
count+=1
if direction=='E':
i+=1
elif direction=='W':
i-=1
elif direction=='N':
j-=1
else:
j+=1
elif Maze==2:
tuple_path=(i,j)
flage_0=len(Path)
Path.add(tuple_path)
flage_1=len(Path)
if flage_0!=flage_1:
count+=2
if direction=='E':
j+=1
direction='S'
elif direction=='W':
j-=1
direction='N'
elif direction=='N':
i+=1
direction='E'
else:
i-=1
direction='W'
elif Maze==3:
tuple_path=(i,j)
flage_0=len(Path)
Path.add(tuple_path)
flage_1=len(Path)
if flage_0!=flage_1:
count+=3
if direction=='E':
i-=1
direction='W'
elif direction=='W':
i+=1
direction='E'
elif direction=='N':
j+=1
direction='S'
else:
j-=1
direction='N'
else:
tuple_path=(i,j)
flage_0=len(Path)
Path.add(tuple_path)
flage_1=len(Path)
if flage_0!=flage_1:
count+=4
if direction=='E':
j-=1
direction='N'
elif direction=='W':
j+=1
direction='S'
elif direction=='N':
i-=1
direction='W'
else:
i+=1
direction='E'
count+=Maze#the last one is not calculate
print(count) maze = [
,
,
,
,
,
,
,
,
,
]
step =
nowXY =
stopXY = )-1]
record = []
path = []
while True:
now = maze]]
if nowXY not in record:
path.append(now)
record.append(nowXY)
if nowXY == stopXY:
break
if now == 1:
pass
elif now == 2:
if step == :
step =
elif step == :
step =
elif step == :
step = [-1,0]
elif step == [-1,0]:
step =
elif now == 3:
if step == :
step =
elif step == :
step = [-1,0]
elif step == :
step =
elif step == [-1,0]:
step =
elif now == 4:
if step == :
step = [-1,0]
elif step == :
step =
elif step == :
step =
elif step == [-1,0]:
step =
nowXY =
print(sum(path))
#result
113 def fun123(x):
move =
postion,footprint = ,[]
result = x]]
while not postion == :
if x]] == 1:
postion += move
postion += move
if postion not in footprint:
result += x]]
footprint.append(,postion])
continue
if x]] == 3:
move,move = move*-1,move*-1
postion += move
postion += move
if postion not in footprint:
result += x]]
footprint.append(,postion])
continue
if x]] == 2:
rotating = [,,,[-1,0],]
move = rotating
postion += move
postion += move
if postion not in footprint:
result += x]]
footprint.append(,postion])
continue
if x]] == 4:
rotating = [,[-1,0],,,]
move = rotating
postion += move
postion += move
if postion not in footprint:
result += x]]
footprint.append(,postion])
continue
return result
if __name__ == '__main__':
print(fun123([,
,
,
,
,
,
,
,
,
])) 本帖最后由 永恒的蓝色梦想 于 2019-8-18 12:59 编辑
'''
var d:#方向
1
|
0-+-2
|
3
var x,y:#坐标
|
|
--+--Y
|
|
X
'''
thelist=[for i in ''' 1 1 1 2 1 1 2 4 4 3
2 1 1 1 1 1 1 1 4 2
1 2 3 1 4 2 1 1 2 4
2 1 1 1 1 1 2 1 4 1
1 1 1 3 2 2 3 1 2 3
3 2 4 1 1 1 1 1 1 4
2 1 1 1 1 2 2 1 4 1
1 2 1 3 2 1 2 4 1 4
1 2 1 1 1 1 4 2 2 2
2 1 2 1 2 2 3 4 1 1'''.split('\n')];d=2;x,y=0,0;s={(9,9)}
while not x==9==y:
s.add((x,y));i=thelist
if i==1:pass#1:直行
elif i==2:d=(d+1)%4#2:右转
elif i==3:d=(d+2)%4#3:掉头
elif i==4:d=(d-1)%4#4:左转
if d==0:y-=1
elif d==1:x-=1
elif d==2:y+=1
elif d==3:x+=1
print(sum((thelistfor x,y in s)))
113
页:
[1]
2