-------------代码实现:--------------------
#include
#include
#include
#define N 64
using namespace std;
bool peak(int grid[N][N],int h,int l);
int main(void) {
int nrows,nclos;
int map[N][N];
string filename;
ifstream file;
cout<<"请输入要打开的文件名:";
cin>>filename;
file.open(filename.c_str());
if(file.fail()) {
cout<<"文件打开失败\n";
exit(1);
}
file>>nrows>>nclos;
if(nrows > N || nclos > N) {
cout<<"文件网格太大,请调整程序\n";
exit(1);
}
//把文件里数据导入二维数组
for(int i=0; i<nrows; i++) {
for(int j=0; j<nclos; j++) {
file>>map[i][j];
}
}
//开始比较峰值并打印输出
for(int i=0; i<nrows-1; i++) {
for(int j=0; j<nclos-1; j++){
if(peak(map,i,j)) {
cout<<"峰值是在:"<<i<<"行"<<j<<"列"<<endl;
}
}
}
system("pause");
file.close();
return 0;
}
bool peak(int grid[64][64],int h,int l) {
if(grid[h][l] > grid[h-1][l]) &&
(grid[h][l] > grid[h+1][l]) &&
(grid[h][l] > grid[h][l-1]) &&
(grid[h][l] > grid[h][l+1])) {
return true;
} else {
return false;
}
}