校OJ 10536: the depth of lake ---搜索

来源:互联网 发布:电子秤数据存在 编辑:程序博客网 时间:2024/06/10 23:05

10536: the depth of lake

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 89  Solved: 34
[Submit][Status][WebBoard]

Description

There is a mysterious lake in the north of Tibet. Asthe sun shines, the surface of the lake is colorful and colorful. The lake wasunfathomable in rainy weather.  After the probe, Ithas an interesting bottom in that it is full of little hills and valleys. .Scientistswonders how deep the bottom ofthe lake is.

 

Scientists use the most advanced radar equipment to detect the bottom ofthe 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.

 

Tofacilitate computing, scientists have put the lake as M * N grids .The depth  reading of each gridis already known. some readings might be 0-- It's a small island on the lake.

 

Find the greatest depth that appears in at leasttwo 'adjacent'readings (where 'adjacent' means in any of the potentially eightsquares that border a square on each of its sides and its diagonals). The lakehas at least one pair of positive, adjacent readings.

Input

The first line of the input contains one integers T,which is the nember of  test cases (1<=T<=5).  Each test casespecifies:

 

* Line 1:  Two space-separatedintegers: M and N   (1 ≤ M,  N ≤ 50)

* Lines2..M+1: Line i+1 contains N space-separated integers that  represent thedepth of the lake across row i: Dij    (0 <= Dij<=1,000,000);

Output

For each test case generate a single line:  a single integer that isthe depth of the lake determined.

 

Sample Input

1

4 3

0 1 0

1 2 0

1 5 1

2 3 4

Sample Output

1

 

题意:
找湖的最深深度,当前深度和周围八个方向的深度,才可认为是湖的深度。

搜索即可,注意减枝。


#include<algorithm>#include<cstdio>#include<iostream>using namespace std;int dx[8] = { -1,-1,-1,0,0,1,1,1 };int dy[8] = { -1,0,1,-1,1,-1,0,1 };int depth;int field[55][55];int n, m;void searcch(int x,int y){      for(int i=0;i<8;i++)  {int nx = x + dx[i]; int ny = y + dy[i];if (nx >= 0 && nx < n && ny >= 0 && ny < m){if (field[nx][ny] == field[x][y]){depth = field[x][y];field[x][y] = -1;  //标记已经被访问过field[nx][ny] = -1;}}}}int main(){int T; cin >> T;while (T--){depth = 0;    cin >> n >> m;for (int i = 0; i < n; i++)for (int j = 0; j < m; j++)cin >> field[i][j];for (int i = 0; i < n; i++)for (int j = 0; j < m; j++)if (field[i][j] > depth)  //减枝:保证进入的深度比当前depth大,并且没有被访问过。searcch(i,j); cout << depth<<endl;}}