Commit 4a3795c3 by BellCodeEditor

auto save

parent e7aeaf76
Showing with 148 additions and 5 deletions
......@@ -3,8 +3,15 @@ student2 = {'语文': 97, '数学': 98, '英语': 90}
student3 = {'语文': 95, '数学': 100, '英语': 93}
score = {'悟空': student1, '诺依': student2, '小贝': student3}
name = input("名字:")
# 查询并打印出输入的名字对应的所有科目的成绩
while True:
name = input("名字:")
if name in score:
info = score[name]
# 查询并打印出输入的名字对应的所有科目的成绩
for k , v in info.items():
print(k,v)
elif name == 'q':
break
else:
print('名字不存在')
print('-'*10)
\ No newline at end of file
class Cat:
def __init__(self,type,color,price):
#属性
self.footNum =4 #4条腿
self.type =type #养品种
self.color = color #颜色
self.price = price #价格
def say(self): #说出价格的方法
print('猫的价格是',self.price)
def running(self):
print('小猫跑起来了')
bell = Cat('波斯猫','black',90)
bell.say()
bell.running()
'''
int #整形 1 2 3 4 -1 -2 -3 -4 0
float # 浮点 1.2 8.4 5.2 5.1
str #字符串 "123" 'qwe' '王宇鹏'
list # 列表 ['123','qwe'] [[123,'123',['qweq','t5653efr']]]
dict #字典 {'qwe':123,'12':'wqe','15':123} 键唯一
'''
'''
'''
8皇后问题
'''
n = 8
x = [] # 一个解(n元数组)
X = [] # 一组解
# 冲突检测:判断 x[k] 是否与前 x[0~k-1] 冲突
def conflict(k):
global x
for i in range(k): # 遍历前 x[0~k-1]
if x[i]==x[k] or abs(x[i]-x[k])==abs(i-k): # 判断是否与 x[k] 冲突
return True
return False
# 套用子集树模板
def queens(k): # 到达第k行
global n, x, X
if k >= n: # 超出最底行
#print(x)
X.append(x[:]) # 保存(一个解),注意x[:]
else:
for i in range(n): # 遍历第 0~n-1 列(即n个状态)
x.append(i) # 皇后置于第i列,入栈
if not conflict(k): # 剪枝
queens(k+1)
x.pop() # 回溯,出栈
# 解的可视化(根据一个解x,复原棋盘。'X'表示皇后)
def show(x):
global n
for i in range(n):
print('. ' * (x[i]) + 'X ' + '. '*(n-x[i]-1))
# 测试
queens(0) # 从第0行开始
print(X[-1], '\n')
show(X[-1])
\ No newline at end of file
# 迷宫(1是墙,0是通路)
# 迷宫(1是墙,0是通路)
maze = [[1,1,1,1,1,1,1,1,1,1],
[0,0,1,0,1,1,1,1,0,1],
[1,1,0,1,0,1,1,0,1,1],
[1,0,1,1,1,0,0,1,1,1],
[1,1,1,0,0,1,1,0,1,1],
[1,1,0,1,1,1,1,1,0,1],
[1,0,1,0,0,1,1,1,1,0],
[1,1,1,1,1,0,1,1,1,1]]
m, n = 8, 10 # 8行,10列
entry = (1,0) # 迷宫入口
path = [entry] # 一个解(路径)
paths = [] # 一组解
# 移动的方向(顺时针8个:N, EN, E, ES, S, WS, W, WN)
directions = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
# 冲突检测
def conflict(nx, ny):
global m,n,maze
# 是否在迷宫中,以及是否可通行
if 0 <= nx < m and 0 <= ny < n and maze[nx][ny]==0:
return False
return True
# 套用子集树模板
def walk(x, y): # 到达(x,y)格子
global entry,m,n,maze,path,paths,directions
if (x,y) != entry and (x % (m-1) ==0 or y % (n-1) == 0): # 出口
#print(path)
paths.append(path[:]) # 直接保存,未做最优化
else:
for d in directions: # 遍历8个方向(亦即8个状态)
nx, ny = x+d[0], y+d[1]
path.append((nx,ny)) # 保存,新坐标入栈
if not conflict(nx, ny): # 剪枝
maze[nx][ny] = 2 # 标记,已访问(奇怪,此两句只能放在if区块内!)
walk(nx, ny)
maze[nx][ny] = 0 # 回溯,恢复
path.pop() # 回溯,出栈
# 解的可视化(根据一个解x,复原迷宫路径,'2'表示通路)
def show(path):
global maze
import pprint, copy
maze2 = copy.deepcopy(maze)
for p in path:
maze2[p[0]][p[1]] = 2 # 通路
pprint.pprint(maze) # 原迷宫
print()
pprint.pprint(maze2) # 带通路的迷宫
# 测试
walk(1,0)
print(paths[-1], '\n') # 看看最后一条路径
show(paths[-1])
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment