1562.Oil Deposits
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 9214 | Accepted: 5066 |
Description
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.
Input
The input contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.
Output
are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.
Sample Input
1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 0 0
Sample Output
0 1 2 2
Source
#include <stdio.h>
#include<memory.h>
char a[111][111];
int m,n,f;
void dfs(int i,int j)
{
if(a[i][j]=='*'||i<0||j<0||i>m-1||j>n-1) return ;
a[i][j]='*';
dfs(i,j+1);
dfs(i,j-1);
dfs(i+1,j);
dfs(i+1,j-1);
dfs(i+1,j+1);
dfs(i-1,j);
dfs(i-1,j+1);
dfs(i-1,j-1);
}
int main()
{
while(scanf("%d%d",&m,&n)!=EOF,m!=0)
{
f=0;
memset(a,0,sizeof(a));
for(int i=0;i<m;i++)
scanf("%s",a[i]);
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
if(a[i][j]=='@')
{
f++;
dfs(i,j);
}
}
printf("%d\n",f);
}
return 0;
}
1088.滑雪
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 59487 | Accepted: 21652 |
Description
Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个区域中最长底滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子
一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-...-3-2-1更长。事实上,这是最长的一条。
1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9
一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-...-3-2-1更长。事实上,这是最长的一条。
Input
输入的第一行表示区域的行数R和列数C(1 <= R,C <= 100)。下面是R行,每行有C个整数,代表高度h,0<=h<=10000。
Output
输出最长区域的长度。
Sample Input
5 5 1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9
Sample Output
25
Source
#include <cstdio>
#include <iostream>
using namespace std;
int m,n,a[101][101],vis[101][101];
int dfs(int i,int j)
{
int max=0;
if(vis[i][j]) return vis[i][j];
if(i+1<n&&a[i][j]>a[i+1][j]&&max<dfs(i+1,j))
max=dfs(i+1,j);
if(i-1>=0&&a[i][j]>a[i-1][j]&&max<dfs(i-1,j))
max=dfs(i-1,j);
if(j-1>=0&&a[i][j]>a[i][j-1]&&max<dfs(i,j-1))
max=dfs(i,j-1);
if(j+1<m&&a[i][j]>a[i][j+1]&&max<dfs(i,j+1))
max=dfs(i,j+1);
return vis[i][j]=max+1;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
scanf("%d",&a[i][j]);
vis[i][j]=0;
}
int max=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(max<dfs(i,j))
max=dfs(i,j);
printf("%d\n",max);
return 0;
}