问题 G: Plumbing the depth of lake
时间限制: 1 Sec 内存限制: 128 MB题目描述
There is a mysterious lake in the north of Tibet. As the sun shines, the surface of the lake is colorful and colorful. The lake was unfathomable in rainy weather. After the probe, It has an interesting bottom in that it is full of little hills and valleys. . Scientists wonders how deep the bottom of the lake is.
Scientists use the most advanced radar equipment to detect the bottom of the lake. It is the discovery that the deepest part is relatively flat. Thet want to know the largest depth number only if it is verified by the fact that the same depth appears in an adjacent reading.
To facilitate computing, scientists have put the lake as M * N grids . The depth reading of each grid is already known. some readings might be 0-- It's a small island on the lake.
Find the greatest depth that appears in at least two 'adjacent'readings (where 'adjacent' means in any of the potentially eight squares that border a square on each of its sides and its diagonals). The lake has at least one pair of positive, adjacent readings.
输入
The first line of the input contains one integers T, which is the nember of test cases (1<=T<=5). Each test case specifies:
* Line 1: Two space-separated integers: M and N (1 ≤ M, N ≤ 50)
* Lines 2..M+1: Line i+1 contains N space-separated integers that represent the depth of the lake across row i: Dij (0 <= Dij <=1,000,000);
输出
For each test case generate a single line: a single integer that is the depth of the lake determined.
样例输入
1 4 3 0 1 0 1 2 0 1 5 1 2 3 4
样例输出
1
分析
代码
#include <algorithm> #include <iostream> #include <string> #include <vector> #include <stack> #include <queue> #include <set> #include <map> #include <cstdlib> #include <cstring> #include <cstdio> #include <cmath> using namespace std; int d[55][55],M,N,depth; int dir[8][2]={
{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};///相邻的八个方向 int main() {int T;scanf("%d",&T);while(T--){depth=0;scanf("%d %d",&M,&N);for(int i=0; i<M; i++)for(int j=0; j<N; j++)scanf("%d",&d[i][j]);///输入湖深信息for(int i=0; i<M; i++){for(int j=0; j<N; j++){if(d[i][j]==-1)continue;///用-1来记录是否判断过for(int k=0;k<8;k++)///判断8个方向{if(i+dir[k][1]>=0&&i+dir[k][1]<M&&j+dir[k][0]>=0&&j+dir[k][0]<M)///确保该位置在矩阵之内{if(d[i+dir[k][1]][j+dir[k][0]]==d[i][j]){if(d[i][j]>depth)depth=d[i][j];///depth存放最深水深d[i+dir[k][1]][j+dir[k][0]]=-1;///判断过的位置赋值为-1d[i][j]=-1;///判断过的位置赋值为-1}}}}}printf("%d\n",depth);} }