人工智能之地形导航系统

发布于:2022-12-24 ⋅ 阅读:(413) ⋅ 点赞:(0)

#include<iostream>

#include<Windows.h>

#include<string>

#include<fstream>

using namespace std;

#define N 128

 

 

//判断峰值函数

bool isPark(int grid[N][N], int r, int c)

{

 if ((grid[r][c]>grid[r-1][c])&& (grid[r][c] > grid[r+1][c])&&

  (grid[r][c] > grid[r][c-1])&& (grid[r][c] > grid[r][c+1]))

 {

  return true;

 }

 else

 {

  return false;

 }

}

 

int main()

{

 int nrows, ncols;

 int map[N][N];

 string filename;

 ifstream file; //对文件进行操作

 

 cout << "请输入要打开的文件名" << endl;

 cin >> filename;

 

 file.open(filename.c_str()); //打开文件

 if (file.fail())

 {

  cout << "打开错误,请进行调试" << endl;

  exit(1);

 }

 //将文件中的数据输入到二维数组中

 file >> nrows >> ncols;//从文件中输入行数和列数

 if (nrows>N || ncols>N)

 {

  cout << "网格太大,请调试程序" << endl;

 }

 for (int i = 0; i < nrows; i++)

 {

  for (int j = 0; j < ncols; j++)

  {

   file >> map[i][j];

  }

 }

 //判断并打印峰值的位置

 for (int i = 1; i < nrows-1; i++)

 { 

  for (int j = 1; j < ncols-1; j++)

  {

   if (isPark(map,i ,j))

   {

    cout << "行" << i << "列" << j << endl;

   }

  }

 }

 system("pause");

 return 0;

}

05e7f478c49d4752a95ed4d1bf814322.png

 63fe1ea4e2a64e2eaacfa2eb972881f6.png

 注解:要将map.txt文件放置在与main.cpp相同的文件夹下面。


网站公告

今日签到

点亮在社区的每一天
去签到