列表

详情


NC24835. [USACO 2009 Mar B]Plumbing the Pond

描述

Bessie's drinks water from a pond in the northwest part of the farm. It has an interesting bottom in that it is full of little hills and valleys. She wonders how deep it is.
She trolls across the pond in her little boat with a very old radar set that tends to have spurious readings. She knows the deepest part is relatively flat and has decided that she'll believe the largest depth number only if it is verified by the fact that the same depth appears in an adjacent reading.
The pond is modeled as an R x C (1 <= R <= 50; 1 <= C <= 50) grid of (positive integer) depth readings (0 <= <= 1,000,000); some readings might be 0 -- those are not part of the pond. A depth reading of 10 means "depth of 10".
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). She knows the pond has at least one pair of positive, adjacent readings.

输入描述

* Line 1: Two space-separated integers: R and C
* Lines 2..R+1: Line i+1 contains C space-separated integers that represent the depth of the pond across row i:

输出描述

* Line 1: A single integer that is the depth of the pond determined by following Bessie's rules.

示例1

输入:

4 3
0 1 0
1 2 0
1 5 1
2 3 4

输出:

1

说明:

The pond has 4 rows, 3 columns.
Even though 5 is the deepest reading Bessie gets, and the number 2 occurs twice, 1 is the largest number that occurs in two adjacent cells.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 4ms, 内存消耗: 616K, 提交时间: 2019-09-15 21:05:50

#include<bits/stdc++.h>
#define MAX_INT  ((unsigned)(-1)>>1)
#define MIN_INT  (~MAX_INT)
#define pi 3.1415926535898
using namespace std;
int a[55][55];
int main(void)
{
      int n,m;cin>>n>>m;
      for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                  cin>>a[i][j];
            }
      }
      int max1=0;
      for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                  if(a[i-1][j-1]==a[i][j]||a[i-1][j]==a[i][j]||a[i-1][j+1]==a[i][j]||
                     a[i][j-1]==a[i][j]||a[i][j+1]==a[i][j]||a[i+1][j-1]==a[i][j]||
                     a[i+1][j]==a[i][j]||a[i+1][j+1]==a[i][j])
                        max1=max(max1,a[i][j]);
            }
      }
      cout<<max1;
      return 0;
}

C++11(clang++ 3.9) 解法, 执行用时: 4ms, 内存消耗: 604K, 提交时间: 2019-09-07 09:42:40

#include<bits/stdc++.h>
using namespace std;
int a[501][501],r,c,ans;
int x[8]={-1,-1,-1,0,0,1,1,1};
int y[8]={-1,0,1,-1,1,-1,0,1};
int main()
{
	cin>>r>>c;
	for(int i=1;i<=r;i++)
	{
		for(int j=1;j<=c;j++)
		{
			cin>>a[i][j];
		}
	}
	for(int i=1;i<=r;i++)
	{
		for(int j=1;j<=c;j++)
		{
			for(int q=0;q<8;q++) if(a[i][j]==a[i+x[q]][j+y[q]]) ans=max(ans,a[i][j]);
		}
	}
	cout<<ans;
} 

上一题