时间限制: 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 <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <string.h>
#include <queue>
using namespace std;
int a[55][55]; //地图
int v[55][55]; //标记是走过
int n,m,l;
int maxint;
struct nome
{
int x,y;
}s,t,x;
int b[8][2]={{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1},{0,1}}; //八个方向
void bfs()
{
int i;
queue<nome>q;
q.push(s);
while (!q.empty())
{
t=q.front();l=0;
for (i=0;i<8;i++)
{
x.x=t.x+b[i][0];
x.y=t.y+b[i][1];
if (a[t.x][t.y]==a[x.x][x.y]) // 如果有相同
l++;
if (l!=0&&a[t.x][t.y]>maxint)
{
maxint=a[t.x][t.y];
}
if (x.x>=0&&x.x<m&&x.y>=0&&x.y<n&&v[x.x][x.y]==0)
{
v[x.x][x.y]++;
q.push(x);
}
}
q.pop();
}
}
int main()
{
int k,i,j;
cin>>k;
while (k--)
{
maxint=0;
cin>>n>>m;
for(i=0;i<n;i++) //输入地图
for (j=0;j<m;j++)
{
cin>>a[i][j];
v[i][j]=0;
}
s.x=0,s.y=0,v[0][0]=1; //从0,0开始检索全图
bfs();
cout<<maxint<<endl;
}
}