当前位置:网站首页>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 !
边栏推荐
- .Xmind文件如何上传金山文档共享在线编辑?
- Reinforcement learning series (I): basic principles and concepts
- (原创)制作一个采用 LCD1602 显示的电子钟,在 LCD 上显示当前的时间。显示格式为“时时:分分:秒秒”。设有 4 个功能键k1~k4,功能如下:(1)k1——进入时间修改。
- Thoroughly understand LRU algorithm - explain 146 questions in detail and eliminate LRU cache in redis
- MySQL事务及实现原理全面总结,再也不用担心面试
- 7-4 散列表查找(PTA程序设计)
- The difference between cookies and sessions
- [during the interview] - how can I explain the mechanism of TCP to achieve reliable transmission
- 强化学习基础记录
- 7-6 矩阵的局部极小值(PTA程序设计)
猜你喜欢

Leetcode.3 无重复字符的最长子串——超过100%的解法

Leetcode. 3. Longest substring without repeated characters - more than 100% solution

关于双亲委派机制和类加载的过程

7-5 走楼梯升级版(PTA程序设计)

使用Spacedesk实现局域网内任意设备作为电脑拓展屏

Nuxtjs快速上手(Nuxt2)

Custom RPC project - frequently asked questions and explanations (Registration Center)

仿牛客技术博客项目常见问题及解答(一)
![[VMware abnormal problems] problem analysis & Solutions](/img/64/f44864da600b61a1a646a5865a2083.jpg)
[VMware abnormal problems] problem analysis & Solutions

Safe driving skills on ice and snow roads
随机推荐
Force deduction 152 question multiplier maximum subarray
强化学习系列(一):基本原理和概念
.Xmind文件如何上传金山文档共享在线编辑?
[the Nine Yang Manual] 2020 Fudan University Applied Statistics real problem + analysis
String ABC = new string ("ABC"), how many objects are created
[data processing of numpy and pytoch]
Analysis of penetration test learning and actual combat stage
简单理解ES6的Promise
2. First knowledge of C language (2)
强化学习基础记录
实验五 类和对象
FAQs and answers to the imitation Niuke technology blog project (II)
Why use redis
【数据库 三大范式】一看就懂
FAQs and answers to the imitation Niuke technology blog project (III)
Custom RPC project - frequently asked questions and explanations (Registration Center)
Canvas foundation 1 - draw a straight line (easy to understand)
实验七 常用类的使用(修正帖)
Experiment 6 inheritance and polymorphism
4. Branch statements and loop statements