Yonisy


私信TA

用户名:dotcpp0721441

访问量:46

签 名:

等  级
排  名 17354
经  验 730
参赛次数 0
文章发表 1
年  龄 0
在职情况 学生
学  校 中国计量大学现代科技学院
专  业

  自我简介:

解题思路:

把蚂蚁的行动、动作当作对象,创建关于mayi()的的对象,并对蚂蚁行动关于黑格、白格等转向设置函数point(self),在主函数中对行走后原位置黑白格取反

注意事项:
此代码中,每个格子的参数类型为str,如果是int型,会导致一直导致答案错误,还有在使用map_1时,与map函数很类似,可能导致程序出错
参考代码

class mayi():
   def __init__(self,head,x,y):
       self.head=head
       self.x=x
       self.y=y
   def point(self):
       p=self.head
       i=self.x
       j=self.y
       if p=='L':
           if map_1[i][j]=='1':
               self.x=i-1
               self.head='U'
           else:
               self.x=i+1
               self.head='D'
       if p=='U':
           if map_1[i][j]=='1':
               self.y=j+1
               self.head='R'
           else:
               self.y=j-1
               self.head='L'
       if p=='R':
           if map_1[i][j]=='1':
               self.x=i+1
               self.head='D'
           else:
               self.x=i-1
               self.head='U'
       if p=='D':
           if map_1[i][j]=='1':
               self.y=j-1
               self.head='L'
           else:
               self.y=j+1
               self.head='R'
m, n = map(int, input().split())
map_1 = []
for i in range(0, m):
   map_1.append(list(input().split()))
x,y,S,k=input().split()
x=int(x)
y=int(y)
k=int(k)
yi_1=mayi(S,x,y)

for w in range(0,k):
   i=yi_1.x
   j=yi_1.y
   yi_1.point()
   if map_1[i][j]=='1':
       map_1[i][j] = '0'
   elif map_1[i][j]=='0':
       map_1[i][j] = '1'
print(yi_1.x,yi_1.y)


 

0.0分

0 人评分

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

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区