当前位置:网站首页>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 None
The 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 !
边栏推荐
- Force deduction 152 question multiplier maximum subarray
- Strengthen basic learning records
- String ABC = new string ("ABC"), how many objects are created
- canvas基础2 - arc - 画弧线
- Zatan 0516
- Yugu p1012 spelling +p1019 word Solitaire (string)
- [the Nine Yang Manual] 2018 Fudan University Applied Statistics real problem + analysis
- 力扣152题乘数最大子数组
- Read only error handling
- Leetcode.3 无重复字符的最长子串——超过100%的解法
猜你喜欢
QT meta object qmetaobject indexofslot and other functions to obtain class methods attention
强化学习基础记录
关于双亲委派机制和类加载的过程
2. First knowledge of C language (2)
canvas基础1 - 画直线(通俗易懂)
[面试时]——我如何讲清楚TCP实现可靠传输的机制
(original) make an electronic clock with LCD1602 display to display the current time on the LCD. The display format is "hour: minute: Second: second". There are four function keys K1 ~ K4, and the fun
Principles, advantages and disadvantages of two persistence mechanisms RDB and AOF of redis
PriorityQueue (large root heap / small root heap /topk problem)
Intensive literature reading series (I): Courier routing and assignment for food delivery service using reinforcement learning
随机推荐
1143_ SiCp learning notes_ Tree recursion
Thoroughly understand LRU algorithm - explain 146 questions in detail and eliminate LRU cache in redis
[MySQL database learning]
SRC挖掘思路及方法
【MySQL数据库的学习】
Read only error handling
关于双亲委派机制和类加载的过程
Intensive literature reading series (I): Courier routing and assignment for food delivery service using reinforcement learning
Package bedding of components
7-6 矩阵的局部极小值(PTA程序设计)
这次,彻底搞清楚MySQL索引
Have you encountered ABA problems? Let's talk about the following in detail, how to avoid ABA problems
Strengthen basic learning records
Miscellaneous talk on May 27
Experiment 9 input and output stream (excerpt)
Experiment 6 inheritance and polymorphism
2022 Teddy cup data mining challenge question C idea and post game summary
为什么要使用Redis
Experiment 4 array
(original) make an electronic clock with LCD1602 display to display the current time on the LCD. The display format is "hour: minute: Second: second". There are four function keys K1 ~ K4, and the fun