题目
Description
给定M行N列的整数矩阵A,如果A的非边界元素A[i][j]大于相邻的上下左右4个元素,那么就称元素A[i][j]是矩阵的局部极大值。本题要求给定矩阵的全部局部极大值及其所在的位置。
Input
输入在第一行中给出矩阵的个数t
输入在第2行中给出矩阵A的行数M和列数N(3<=M,N<=20);最后M行,每行给出A在该行的N个元素的值。数字间以空格分隔。
Output
每行按照“元素值 行号 列号”的格式输出一个局部极大值,其中行、列编号从1开始。要求按照行号递增输出;若同行有超过1个局部极大值,则该行按列号递增输出。若没有局部极大值,则输出“None 总行数 总列数”。
Sample Input
2
4 5
1 1 1 1 1
1 3 9 3 1
1 5 3 5 1
1 1 1 1 1
3 5
1 1 1 1 1
9 3 9 9 1
1 5 3 5 1
Sample Output
9 2 3
5 3 2
5 3 4
None 3 5
代码块
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cn = new Scanner(System.in);
int t = cn.nextInt();
while (t-- > 0) {
int m = cn.nextInt();
int n = cn.nextInt();
int[][] a = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = cn.nextInt();
}
}
boolean flag = true;
for (int i = 1; i < m - 1; i++) {
for (int j = 1; j < n - 1; j++) {
if (a[i][j] > a[i][j + 1] && a[i][j] > a[i][j - 1]
&& a[i][j] > a[i + 1][j] && a[i][j] > a[i - 1][j]) {
System.out.println(a[i][j] + " " + (i + 1) + " "
+ (j + 1));
flag = false;
}
}
}
if (flag) {
System.out.println("None " + m + " " + n);
}
}
}
}