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

本文介绍了一种用于探测湖泊最大深度的算法。通过雷达设备获取网格化的湖底深度数据,算法需找出至少出现两次的相邻最大深度值。使用递归搜索策略,并通过合理剪枝提高效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
	}

}


 

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值