hduoj 1241 经典dfs
来源:互联网 发布:司法淘宝拍卖网房产 编辑:程序博客网 时间:2024/06/07 22:37
Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25489 Accepted Submission(s): 14673
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.
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.
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.
Sample Input
1 1*3 5*@*@***@***@*@*1 8@@****@*5 5 ****@*@@*@*@**@@@@*@@@**@0 0
Sample Output
0122
题意:给出一个矩阵,找出连通块的数量。(连通块是八个方向的相邻块,一开始我一直以为是四个,WA了几发)需要注意的是每走过一个点要标记,避免重复。
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#define SIZE 100
using namespace std;
char Map[110][110];
int vis[110][110];
int m, n;
void dfs(int i,int j,int id)
{
if(i < 0||j < 0||i >= m||j >= n) return;
if(vis[i][j] > 0||Map[i][j] != '@') return ;
vis[i][j] = id;
for(int r = -1;r <= 1;r++)
{
for(int c = -1;c <= 1;c++)
{
if(r != 0||c != 0) dfs(i+r,j+c,id);
}
}
}
int main()
{
while(cin >> m >>n&&m)
{
memset(vis,0,sizeof(vis));
for(int i = 0;i < m;i++)
{
cin >> Map[i];
}
int ans = 0;
for(int i = 0;i < m;i++)
{
for(int j = 0;j < n;j++)
{
if(Map[i][j] == '@'&&vis[i][j] == 0)
{
dfs(i,j,++ans);
}
}
}
cout << ans << endl;
}
return 0;
}
#include<algorithm>
#include<cstring>
#define SIZE 100
using namespace std;
char Map[110][110];
int vis[110][110];
int m, n;
void dfs(int i,int j,int id)
{
if(i < 0||j < 0||i >= m||j >= n) return;
if(vis[i][j] > 0||Map[i][j] != '@') return ;
vis[i][j] = id;
for(int r = -1;r <= 1;r++)
{
for(int c = -1;c <= 1;c++)
{
if(r != 0||c != 0) dfs(i+r,j+c,id);
}
}
}
int main()
{
while(cin >> m >>n&&m)
{
memset(vis,0,sizeof(vis));
for(int i = 0;i < m;i++)
{
cin >> Map[i];
}
int ans = 0;
for(int i = 0;i < m;i++)
{
for(int j = 0;j < n;j++)
{
if(Map[i][j] == '@'&&vis[i][j] == 0)
{
dfs(i,j,++ans);
}
}
}
cout << ans << endl;
}
return 0;
}
0 0
- hduoj 1241 经典dfs
- hduoj-5311【dfs】
- hduoj-1501【dfs】【dp】
- hdu 1241 经典DFS
- HDU 1241 DFS经典
- 【HDUOJ 6035】 Colorful Tree 【DFS】
- HDUoj 5113 Black And White (dfs&剪枝
- 【HDUOJ]1010-Tempter of bone DFS+剪枝
- HDUOJ
- HDUoj
- hdu 1241 Oil Deposits(经典dfs)
- HDOJ/HDU 1241 Oil Deposits(经典DFS)
- HDUoj 5723 Abandoned country(最小生成树+dfs
- 经典迷宫问题DFS
- poj2488(经典dfs)
- HDU 1518 经典DFS
- hdu1501Zipper -- 经典DFS
- HDU1455-经典DFS剪枝
- 2016中国大学生程序设计竞赛(长春)-重现赛
- 对象作用域与servlet事件监听器
- 两字符串最长公共子序列--动态规划
- 变量
- RxJava 详解
- hduoj 1241 经典dfs
- java静态代理和动态代理
- 高并发等大型网站架构常见问题
- R-CNN论文详解
- 安卓1110_四大组件-ContentProvider内容提供者 备份短信 查询联系人 插入联系人
- 手机按了home键以后,再返回程序变成重新启动
- Ubuntu 11.04 LAMP+JSP环境安装过程
- HDU:1172 猜数字
- 10个快乐习惯来源于美国哈佛大学