M×N 整數矩陣(3≤M,N≤20)A[i][j] 若大於上下左右四鄰居,則為局部極大值值 行 列(1-based),按行優先、列遞增序None M Ni=1..M-2, j=1..N-2(跳過邊界)a[i][j] > a[i-1][j] && > a[i+1][j] && > a[i][j-1] && > a[i][j+1]found=true!found 輸出 None#include <stdio.h>
#include <stdbool.h>
void findLocalMax(int M, int N) {
int a[20][20];
for (int i=0; i a[i-1][j] && a[i][j] > a[i+1][j] &&
a[i][j] > a[i][j-1] && a[i][j] > a[i][j+1]) {
printf("%d %d %d\n", a[i][j], i+1, j+1);
found = true;
}
}
}
if (!found) printf("None %d %d\n", M, N);
}
int main() {
int M, N; scanf("%d%d", &M, &N);
findLocalMax(M, N);
return 0;
}
i=1..M-2 對應第 2 行到倒數第 2 行bool 需 stdbool.h(C99)9 3 9 9 中間 3 不大於左右 9,故無極大值O(M×N) 時間,O(M×N) 空間,規模極小無壓力。