06:尋找山頂
- 總時間限制:
- 1000ms
- 內存限制:
- 65536kB
- 描述
-
在一個m×n的山地上,已知每個地塊的平均高程,請求出所有山頂所在的地塊(所謂山頂,就是其地塊平均高程不比其上下左右相鄰的四個地塊每個地塊的平均高程小的地方)。
- 輸入
- 第一行是兩個整數,表示山地的長m(5≤m≤20)和寬n(5≤n≤20)。
其後m行爲一個m×n的整數矩陣,表示每個地塊的平均高程。每行的整數間用一個空格分隔。 - 輸出
- 輸出所有上頂所在地塊的位置。每行一個。按先m值從小到大,再n值從小到大的順序輸出。
- 樣例輸入
-
10 5 0 76 81 34 66 1 13 58 4 40 5 24 17 6 65 13 13 76 3 20 8 36 12 60 37 42 53 87 10 65 42 25 47 41 33 71 69 94 24 12 92 11 71 3 82 91 90 20 95 44
- 樣例輸出
-
0 2 0 4 2 1 2 4 3 0 3 2 4 3 5 2 5 4 7 2 8 0 8 4 9 3
- 查看
- 提交
- 統計
- 提問
-
#include<stdio.h> #define N 20 int main() { int m,n; int high[N][N]; int flag[N][N]; int (*f)[N]=flag; int (*ph)[N] = high; scanf("%d %d",&m,&n); for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { scanf("%d",*(ph+i)+j); } } for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { /* if(i=0) ph[i-1][j]= 0;//這四句沒用 if(i=m-1) ph[i+1][j]= 0; if(j=0) ph[i][j-1]= 0; if(j=n-1) ph[i][j+1]= 0; printf("i-1 = %d\n",ph[i-1][j]); if((ph[i][j]>ph[i-1][j])&&(ph[i][j]>ph[i+1][j])&&(ph[i][j]>ph[i][j-1])&&(ph[i][j]>ph[i][j+1])) { printf("%d %d\n",i,j); }*/ bool shan = true; if (shan && i-1>=0) { if (ph[i][j]<ph[i-1][j]) { shan = false; } } if (shan && i+1<m) { if (ph[i][j]<ph[i+1][j]) { shan = false; } } if (shan && j-1>=0) { if (ph[i][j]<ph[i][j-1]) { shan = false; } } if (shan && j+1<n) { if (ph[i][j]<ph[i][j+1]) { shan = false; } } if (shan) { printf("%d %d\n", i, j); } } } return 0; }