白羽啊


私信TA

用户名:723822380

访问量:2781

签 名:

等  级
排  名 4389
经  验 1642
参赛次数 0
文章发表 22
年  龄 0
在职情况 学生
学  校 泉师
专  业

  自我简介:

解题思路:模拟题目描述的蚂蚁行动规则

注意事项:

参考代码:

m, n = map(int, input().strip().split())
matrix = []
for i in range(m):
    matrix.append(list(map(int, input().strip().split())))
temp = input().strip().split()
x, y, s, k = int(temp[0]), int(temp[1]), temp[2], int(temp[3])
directions = {"L": [(0, -1), ("D", "U")], "U": [(-1, 0), ("L", "R")], "R": [(0, 1), ("U", "D")],
              "D": [(1, 0), ("R", "L")]} # 第一个元组对应移动,第二个元组对应(左转,右转)

while k:
    if matrix[x][y] == 0:#白格
        matrix[x][y] = 1#改黑格
        s = directions[s][1][0] #左转90->向下,即D
        if 0 <= x + directions[s][0][0] <= m and 0 <= y + directions[s][0][1] <= n:#边界就不移动,只执行前面的改颜色和改方向
            x += directions[s][0][0]
            y += directions[s][0][1]

    else:
        matrix[x][y] = 0
        s = directions[s][1][1]
        if 0 <= x + directions[s][0][0] <= m and 0 <= y + directions[s][0][1] <= n:
            x += directions[s][0][0]
            y += directions[s][0][1]
    k -= 1
print(x, y)


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区