#include<iostream> #include<vector> #include<algorithm> using namespace std; int getNum(vector<vector<int>> data, int row, int col) { int court = 0,shang,xia,zuo,you; vector <int> a; for (int i = row,j=col;; ) { if (i + 1 != data.size()&& data[i + 1][j] - data[i][j]>=0) { shang = data[i + 1][j] - data[i][j]; a.push_back(shang); } if (i - 1 >= 0 && data[i - 1][j] - data[i][j] >= 0) { xia = data[i - 1][j] - data[i][j]; a.push_back(xia); } if (j - 1 >= 0 && data[i ][j-1] - data[i][j] >= 0) { zuo = data[i][j - 1] - data[i][j]; a.push_back(zuo); } if (j + 1 != data[i].size() && data[i][j + 1] - data[i][j] >= 0) { you = data[i][j + 1] - data[i][j]; a.push_back(you); } sort(a.begin(), a.end()); if (i + 1 != data.size()&& data[i+1][j ] - data[i][j] >= 0 && shang==a[0]) { i++; court++; a.clear(); continue; } if (i - 1 >= 0&& data[i-1][j ] - data[i][j] >= 0 && xia==a[0]) { i--; court++; a.clear(); continue; } if (j - 1 >= 0 && data[i][j - 1] - data[i][j] >= 0 && zuo == a[0]) { j--; court++; a.clear(); continue; } if (j + 1 != data[i].size() && data[i][j + 1] - data[i][j] >= 0 && you == a[0]) { j++; court++; a.clear(); continue; } if(a.size()==0) { break; } } return court; } int main(){ int row, col; while (cin >> row >> col) { vector<vector<int>> data(row);//设置二维数组行数 int get=0,court=0,temp; for (int i = 0; i < row; i++)//初始化每行列数 { data[i].resize(col); } for (int i = 0; i < row; i++)//二维数组赋值 { for (int j = 0; j < col; j++) { cin >> get; data[i][j] = get; } } for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if(court< getNum(data, i, j))court=getNum(data, i, j); } } cout << court<<endl; } } |
Double click to view unformatted code.