[size=24px][color=#FF0000][b]直接上代码:[/b][/color][/size]
[code=python]
积水区域是指沿上下左右或左上、左下、右上、右下可以水平或下降到洼地中心的地点
# coding=utf-8
# 设置编码方式
import xlrd
# 引入读excel的库
# 打开文件
data = xlrd.open_workbook('洼地.xlsx') # 改成自己的名字
# 查看工作表
data.sheet_names()
print("sheets:" + str(data.sheet_names()))
# 通过文件名获得工作表,获取工作表1
# table = data.sheet_by_name('工作表1')
# 打印data.sheet_names()可发现,返回的值为一个列表,通过对列表索引操作获得工作表1
table = data.sheet_by_index(0)
# 获取行数和列数
# 行数:table.nrows
# 列数:table.ncols
print("总行数:" + str(table.nrows))
print("总列数:" + str(table.ncols))
# 获取整行的值 和整列的值,返回的结果为数组
# 整行值:table.row_values(start,end)
# 整列值:table.col_values(start,end)
# 参数 start 为从第几个开始打印,
# end为打印到那个位置结束,默认为none
# print("整行值:" + str(table.row_values(0)))
# print("整列值:" + str(table.col_values(1)))
# 获取某个单元格的值,例如获取B3单元格值
# cel_B3 = table.cell(0,0).value
# print("第三行第二列的值:" + str(cel_B3))
mountain = [] # 空列表
for i in range(table.nrows):
mountain.append([]) # 增加一行
for j in range(table.ncols):
mountain[i].append(int(table.cell(i, j).value)) # 在一行里增加对应内容
print(mountain[i][j], end=' ') # 输出上一句内容
print()
deep_x = 4 # 洼地中心横坐标
deep_y = 4 # 洼地中心纵坐标
pos_ls = [(y, x) for y in range(-1, 2) for x in range(-1, 2)]
# 上、下、左、右、左上、左下、右上、右下八个方向加上本身
pos_ls.remove((0, 0)) # 去除上一步的八个方向,留下本身
success = 0 # 0表示暂时定为没有成功
flow_list = [] # 水流动过程的列表
def water_flow(y, x):
global deep_x, deep_y, success
# 把这几个变量定为全局变量
if y == deep_y and x == deep_x: # 如果到达洼地中心
print('ok') # 输出可以
success = 1 # ‘成功’设为1
flow_list.append((y, x)) # 表示该点已走过
global mountain
for yp, xp in pos_ls: # 遍历8个方向
if len(mountain[0]) - 1 >= x + xp >= 0 and len(mountain) - 1 >= y + yp >= 0:
if success != 1 and mountain[y][x] >= mountain[y + yp][x + xp] \
and (y + yp, x + xp) not in flow_list:
water_flow(y + yp, x + xp) # 搜索某个方向
if success != 1: flow_list.remove((y, x)) # 运行至此说明线路不通,退回
return success
if_cont = 1
while if_cont:
x = eval(input('x:')) # 输入需要验证的横坐标
y = eval(input('y:')) # 输入需要验证的纵坐标
success = 0
print("是积水区域" if water_flow(y, x) else "不是积水区域")
print('水流路线:' + str([(x, y) for y, x in flow_list]))
mountain_copy=[]
i=0
for line in mountain:#列表mountain的完全深复制
mountain_copy.append([])#添加一行
mountain_copy[i]=list(line)#复制刚才添加的行
i+=1
xp,yp=1,0
pos8='↖↑↗←→↙↓↘'#8个方向
for index in range(len(flow_list)-1):
c=0#c的值表示使用pos8的哪一个
for y,x in pos_ls:#(y=-1,x=-1);y,x=-1,0;y,x=-1,1......
if flow_list[index+1][xp]==flow_list[index][xp]+x and \
flow_list[index + 1][yp] == flow_list[index][yp] + y:
break
c+=1
mountain_copy[flow_list[index][yp]][flow_list[index][xp]]=pos8[c]
for line in mountain_copy:
for item in line:
if item in list(pos8):#是箭头
print(item,end='')
else:print(str(item),end=' ')#不是箭头,是数字
print()#输出回车换行
flow_list.clear()
[/code]
[size=24px][b][color=#FF0000]表格内容(洼地.xlsx)[/color][/b][/size]:
1 1 3 4 5 5 7 5 4
1 2 4 4 4 4 6 5 3
4 4 3 4 3 3 6 8 5
3 3 2 3 2 2 4 6 7
1 2 2 2 1 2 3 4 5
3 3 2 1 1 2 3 6 6
2 3 3 1 1 2 5 7 8
1 2 3 3 3 3 4 9 6
1 1 2 3 3 4 5 7 8
输出:
[code=text]
pydev debugger: process 14040 is connecting
Connected to pydev debugger (build 183.5912.18)
sheets:[‘Sheet1’, ‘Sheet2’, ‘Sheet3’]
总行数:9
总列数:9
1 1 3 4 5 5 7 5 4
1 2 4 4 4 4 6 5 3
4 4 3 4 3 3 6 8 5
3 3 2 3 2 2 4 6 7
1 2 2 2 1 2 3 4 5
3 3 2 1 1 2 3 6 6
2 3 3 1 1 2 5 7 8
1 2 3 3 3 3 4 9 6
1 1 2 3 3 4 5 7 8
x:8
y:8
ok
是积水区域
水流路线:[(8, 8), (7, 8), (6, 7), (5, 6), (4, 5), (4, 4)]
1 1 3 4 5 5 7 5 4
1 2 4 4 4 4 6 5 3
4 4 3 4 3 3 6 8 5
3 3 2 3 2 2 4 6 7
1 2 2 2 1 2 3 4 5
3 3 2 1 ↑2 3 6 6
2 3 3 1 1 ↖5 7 8
1 2 3 3 3 3 ↖9 6
1 1 2 3 3 4 5 ↖←
x:1
y:1
不是积水区域
水流路线:[]
1 1 3 4 5 5 7 5 4
1 2 4 4 4 4 6 5 3
4 4 3 4 3 3 6 8 5
3 3 2 3 2 2 4 6 7
1 2 2 2 1 2 3 4 5
3 3 2 1 1 2 3 6 6
2 3 3 1 1 2 5 7 8
1 2 3 3 3 3 4 9 6
1 1 2 3 3 4 5 7 8
x:2
y:5
ok
是积水区域
水流路线:[(2, 5), (1, 4), (2, 3), (2, 4), (3, 4), (4, 3), (5, 3), (4, 4)]
1 1 3 4 5 5 7 5 4
1 2 4 4 4 4 6 5 3
4 4 3 4 3 3 6 8 5
3 3 ↓3 →↙4 6 7
1 ↗→↗1 2 3 4 5
3 3 ↖1 1 2 3 6 6
2 3 3 1 1 2 5 7 8
1 2 3 3 3 3 4 9 6
1 1 2 3 3 4 5 7 8
x:2
y:4
ok
是积水区域
水流路线:[(2, 4), (2, 3), (1, 4), (2, 5), (3, 4), (4, 3), (5, 3), (4, 4)]
1 1 3 4 5 5 7 5 4
1 2 4 4 4 4 6 5 3
4 4 3 4 3 3 6 8 5
3 3 ↙3 →↙4 6 7
1 ↘↑↗1 2 3 4 5
3 3 ↗1 1 2 3 6 6
2 3 3 1 1 2 5 7 8
1 2 3 3 3 3 4 9 6
1 1 2 3 3 4 5 7 8
x:
进程已结束,退出代码-1
[/code]