当前位置:网站首页>7-6 local minimum of matrix (PTA program design)
7-6 local minimum of matrix (PTA program design)
2022-07-06 13:56:00 【Programming Lindaiyu】
Given M That's ok N The integer matrix of columns A, among 3≤M,N≤10, If A The non boundary elements of A[i][j] Smaller than the adjacent upper, lower, left and right 4 Elements , So it's called the element A[i][j] Is the local minimum of the matrix . It is required to write a program to output all local minima of a given matrix and their positions . Each line follows “ Element value Line number Column number ” Output a local minimum in the format of , One line 、 Column number from 1 Start . The output is required to be incremented according to the line number ; If there are more than 1 A local minimum , Then the row is incremented by the column number . If there is no local minimum , The output “None”.
Input format :
First enter the number of rows of the matrix in the first row M And number of columns N, Then enter the integer matrix from the second line A All elements of .
Output format :
Output all local minima of a given matrix and their positions according to the requirements of the topic .
sample input :
Here's a set of inputs . for example :
4 5
9 9 9 9 9
9 3 9 5 9
9 5 3 5 9
9 9 9 9 9
sample output :
Here is the corresponding output . for example :
3 2 2
3 3 3
Code (Python):
m,n=map(int,input().split()) # Enter the number of lines m And number of columns n
list1=[] #list1 Used to store matrix
count=0 # Used to record several qualified values
for i in range(m): # Pay attention to the input method of two-dimensional array
s = input() # Line by line input
list1.append([int(n) for n in s.split()]) # Separate the numbers on each line with spaces ,split() The return value of the function is a list , That is, each line is treated as an element , After casting , Add to list1 in
for i in range(1,m-1): # Traverse every internal element of the matrix
for j in range(1,n-1):
if list1[i][j]<list1[i-1][j] and list1[i][j]<list1[i][j-1] and list1[i][j]<list1[i+1][j] and list1[i][j]<list1[i][j+1]: # Judge whether it is smaller than its upper, lower, left and right 4 Elements
print(list1[i][j],i+1,j+1) # If it is less than up, down, left and right 4 One element , Is the local minimum of the matrix , Neglect the local minimum and its position , Because the list is from 0 Start , The rows and columns of the matrix are from 1 Start , So add 1
count=1 # For the convenience of judging whether there is a qualified value
if count==0: #count=0 Indicates that there is no qualified value
print("None") # If not, output NoneThe above program gives more detailed comments , For novice Xiaobai's reference . The idea of program design or code implementation is not optimal , You are welcome to correct your mistakes or give better ideas .
I am a rookie who wants to be Kunpeng , Everyone's encouragement is my driving force , Welcome to like collection comments !
边栏推荐
- Yugu p1012 spelling +p1019 word Solitaire (string)
- 编写程序,模拟现实生活中的交通信号灯。
- 7-8 7104 约瑟夫问题(PTA程序设计)
- js判断对象是否是数组的几种方式
- Callback function ----------- callback
- 实验七 常用类的使用
- Experiment 9 input and output stream (excerpt)
- [graduation season · advanced technology Er] goodbye, my student days
- 7-5 走楼梯升级版(PTA程序设计)
- 扑克牌游戏程序——人机对抗
猜你喜欢

【VMware异常问题】问题分析&解决办法
![[au cours de l'entrevue] - Comment expliquer le mécanisme de transmission fiable de TCP](/img/d6/109042b77de2f3cfbf866b24e89a45.png)
[au cours de l'entrevue] - Comment expliquer le mécanisme de transmission fiable de TCP

Intensive literature reading series (I): Courier routing and assignment for food delivery service using reinforcement learning

Callback function ----------- callback

Strengthen basic learning records

About the parental delegation mechanism and the process of class loading

仿牛客技术博客项目常见问题及解答(二)

Thoroughly understand LRU algorithm - explain 146 questions in detail and eliminate LRU cache in redis

一段用蜂鸣器编的音乐(成都)

Meituan dynamic thread pool practice ideas, open source
随机推荐
TypeScript快速入门
重载和重写的区别
Attach the simplified sample database to the SQLSERVER database instance
Difference and understanding between detected and non detected anomalies
[VMware abnormal problems] problem analysis & Solutions
简单理解ES6的Promise
稻 城 亚 丁
Experiment 4 array
Strengthen basic learning records
Renforcer les dossiers de base de l'apprentissage
The difference between overloading and rewriting
Canvas foundation 1 - draw a straight line (easy to understand)
Strengthen basic learning records
About the parental delegation mechanism and the process of class loading
[experiment index of educator database]
FAQs and answers to the imitation Niuke technology blog project (III)
C language Getting Started Guide
Meituan dynamic thread pool practice ideas, open source
仿牛客技术博客项目常见问题及解答(一)
记一次猫舍由外到内的渗透撞库操作提取-flag