专题二1011
2016-04-23 10:01
381 查看
*题意:相同油田打一个井,求oil deposits的个数
*思路:利用返回,使用dfs求解。DFS搜索了几次,就有几个满足题目要求的Oil Deposits。
*AC源码
#include <iostream>
using namespace std;
char s[102][102];
int m,n;
int i,j;
void dfs(int x,int y)
{
if(x<1||x>m||y<1||y>n)
return ;
if(s[x][y]!='@')
return ;
s[x][y]='*';
for(int i=-1;i<=1;i++)
for(int j=-1;j<=1;j++)
{
dfs(x+i,y+j);
}
}
int main()
{
while(cin>>m>>n&&m)
{
int c=0;
for(i=1;i<=m;++i)
for(j=1;j<=n;j++)
cin>>s[i][j];
for(i=1;i<=m;++i)
for(j=1;j<=n;++j)
{
if(s[i][j]=='@')
{
dfs(i,j);
c++;
}
}
cout<<c<<endl;
}
}
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 107 Accepted Submission(s) : 54
Problem 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. <br>
Input
The input file 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.<br>
Output
For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.<br>
Sample Input
Sample Output
Source
Mid-Central USA 1997
Statistic | Submit | Back
*思路:利用返回,使用dfs求解。DFS搜索了几次,就有几个满足题目要求的Oil Deposits。
*AC源码
#include <iostream>
using namespace std;
char s[102][102];
int m,n;
int i,j;
void dfs(int x,int y)
{
if(x<1||x>m||y<1||y>n)
return ;
if(s[x][y]!='@')
return ;
s[x][y]='*';
for(int i=-1;i<=1;i++)
for(int j=-1;j<=1;j++)
{
dfs(x+i,y+j);
}
}
int main()
{
while(cin>>m>>n&&m)
{
int c=0;
for(i=1;i<=m;++i)
for(j=1;j<=n;j++)
cin>>s[i][j];
for(i=1;i<=m;++i)
for(j=1;j<=n;++j)
{
if(s[i][j]=='@')
{
dfs(i,j);
c++;
}
}
cout<<c<<endl;
}
}
Oil Deposits
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 107 Accepted Submission(s) : 54
Problem 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. <br>
Input
The input file 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.<br>
Output
For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.<br>
Sample Input
1 1<br>*<br>3 5<br>*@*@*<br>**@**<br>*@*@*<br>1 8<br>@@****@*<br>5 5 <br>****@<br>*@@*@<br>*@**@<br>@@@*@<br>@@**@<br>0 0 <br>
Sample Output
0<br>1<br>2<br>2<br>
Source
Mid-Central USA 1997
Statistic | Submit | Back
相关文章推荐
- 【USACO题库】3.3.5 A Game游戏
- latex pieces
- jQuery ajax问题
- 解决windows蓝屏 STOP:c000021a {Fatal System Error} (by 星空武哥)
- requestAnimationFrame 的用法
- java中的栈内存和堆内存
- 北京Uber优步司机奖励政策(4月23日)
- AndroidStudio如何导入第三方包以及开源库
- [进制 贪心] BZOJ 1110 [POI2007]砝码Odw
- JSONModel解析数据成Model
- 指针函数和函数指针、指针数组和数组指针
- Android5.0采用新的CameraAPI
- 收藏各种大牛博客
- java多线程模式ThreadLocal原理简述及其使用详解
- Error with command: gdb --version Cannot run program "gdb": Launching failed
- ArcEngine SpatialRelDescription
- 吊扇项目总结(五)— STM8 GPIO的探讨
- iOS JSONModel解析数据成Model
- HDU 5671 Matrix (BestCoder Round #81 (div.2) 1002)
- bzoj 2159: Crash 的文明世界 树形dp