hud 1241 Oil Deposits
Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11842 Accepted Submission(s): 6873
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.
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.
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
0
1
2
2
#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
char date[110][110];
bool map[110][110];
int n,m;
int dfs(int a,int b)
{
if(map[a][b]==0) return 0;
else {
map[a][b]=0;
if(a>0){
if(b>0) dfs(a-1,b-1);
if(b<n-1) {
dfs(a-1,b+1);
}
dfs(a-1,b);
}
if(a<m-1)
{
if(b>0) dfs(a+1,b-1);
if(b<n-1)dfs(a+1,b+1);
dfs(a+1,b);
}
if(b>0) dfs(a,b-1);
if(b<n-1) dfs(a,b+1);
return 1;
}
}
int main()
{
char chare;
int ans;
//freopen("in.txt","r",stdin);
while (~scanf("%d%d",&m,&n))
{
//printf("%d %d\n",m,n);
if(n==0&&m==0) break;
ans=0;
chare='a';
for (int i=0;i<m;i++) cin>>date[i];
for (int i=0;i<m;i++){
for (int j=0;j<n;j++)
{
map[i][j]=date[i][j]=='@'?true:false;
}//puts("");
}
for (int i=0;i<m;i++)
{
for (int j=0;j<n;j++)
{
ans+=dfs(i,j);
}
}
printf("%d\n",ans);
}
return 0;
}
bfs版
#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
char map[110][110];
int n,m;
queue<pair<int,int> > q;
int bfs(int a,int b)
{
if(map[a][b]=='*') return 0;
else {
q.push(make_pair(a,b));
while (!q.empty())
{
int x,y;
x=q.front().first;
y=q.front().second;
if(x>0){
if(y>0) if(map[x-1][y-1]=='@'){
map[x-1][y-1]='*';
q.push(make_pair(x-1,y-1));
}//bfs(a-1,b-1);
if(y<n-1) if(map[x-1][y+1]=='@')
{
map[x-1][y+1]='*';
q.push(make_pair(x-1,y+1));
}//bfs(a-1,b+1);
if(map[x-1][y]=='@')
{
map[x-1][y]='*';
q.push(make_pair(x-1,y));
}//bfs(a-1,b);
}
if(x<m-1)
{
if(y>0) if(map[x+1][y-1]=='@'){
map[x+1][y-1]='*';
q.push(make_pair(x+1,y-1));
}//bfs(a-1,b-1);
if(y<n-1) if(map[x+1][y+1]=='@')
{
map[x+1][y+1]='*';
q.push(make_pair(x+1,y+1));
}//bfs(a-1,b+1);
if(map[x+1][y]=='@')
{
map[x+1][y]='*';
q.push(make_pair(x+1,y));
}//bfs(a-1,b);
}
if(y>0)
if(map[x][y-1]=='@')
{
map[x][y-1]='*';
q.push(make_pair(x,y-1));
}//bfs(a,b-1);
if(y<n-1)
if(map[x][y+1]=='@')
{
map[x][y+1]='*';
q.push(make_pair(x,y+1));
}//bfs(a,b+1);
q.pop();
}
return 1;
}
}
int main()
{
char chare;
int ans;
//freopen("in.txt","r",stdin);
while (~scanf("%d%d",&m,&n))
{
if(n==0&&m==0) break;
ans=0;
chare='a';
for (int i=0;i<m;i++){ cin>>map[i];}
for (int i=0;i<m;i++)
{
for (int j=0;j<n;j++)
{
ans+=bfs(i,j);
}
}
printf("%d\n",ans);
}
return 0;
}
hud 1241 Oil Deposits的更多相关文章
- HDU 1241 Oil Deposits --- 入门DFS
HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...
- hdu 1241 Oil Deposits(DFS求连通块)
HDU 1241 Oil Deposits L -DFS Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & ...
- HDU 1241 Oil Deposits(石油储藏)
HDU 1241 Oil Deposits(石油储藏) 00 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Probl ...
- HDOJ(HDU).1241 Oil Deposits(DFS)
HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- DFS(连通块) HDU 1241 Oil Deposits
题目传送门 /* DFS:油田问题,一道经典的DFS求连通块.当初的难题,现在看上去不过如此啊 */ /************************************************ ...
- hdu 1241:Oil Deposits(DFS)
Oil Deposits Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- 杭电1241 Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission ...
- HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- HDU 1241 Oil Deposits (DFS/BFS)
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
随机推荐
- WINDOWS 2008 SERVER域用户自动登陆
The user I wanted to auto-logon as didn’t have a password, this reg hack worked instead: HKEY_LOCAL_ ...
- easyui源码翻译1.32--Pagination(分页)
前言 使用$.fn.pagination.defaults重写默认值对象下载该插件翻译源码 该分页控件允许用户导航页面的数据.它支持页面导航和页面长度选择的选项设置.用户可以在分页控件上添加自定义按钮 ...
- vcastr2.0插件超级简单使用
Vcastr2.0简单使用 友情提示:1.蓝色文字为必修改内容.2.#字符后面是解释该代码段的主要内容 1. 引用swfobject.js文件 #public/videoplu ...
- UL LI 布局 TAB 切换条
web页面实现tab的功能有几种实现方式,下面是使用UL LI DIV方式实现的tab. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tr ...
- UAC新解(有非正常手段可以绕过)
360第一次注册是需要弹,可是以后就不弹了开机自启动不弹框,开机自启动不弹框 服务是system权限再说一句,一般程序也不需要过UAC系统启动项白名单.UAC有一个白名单机制.还有UAC也可以通过wu ...
- 165. Compare Version Numbers
题目: Compare two version numbers version1 and version2.If version1 > version2 return 1, if version ...
- Android 设置按钮背景透明与半透明_图片背景透明
Button或者ImageButton的背景设为透明或者半透明 半透明<Button android:background="#e0000000" ... /> 透明 ...
- Android 应用自动更新功能的代码
由于Android项目开源所致,市面上出现了N多安卓软件市场.为了让我们开发的软件有更多的用户使用,我们需要向N多市场发布,软件升级后,我们也必须到安卓市场上进行更新,给我们增加了工作量.因此我们有必 ...
- 【HDOJ】1254 推箱子
来一发搜索.数据量比较小,这么玩儿都能ac.搞个优先队列.先扫描从起点可以到达箱子的上下左右中哪些位置,并针对这些位置进行bfs.所谓推,就是箱子和人同向移动一个单位.bfs的时候注意一些限制条件就好 ...
- hdu1054Strategic Game(树形DP)
链接 归属简单树形DP 挺简单的 跟第一道一样 就是我跑偏了题意..以为要覆盖点 纠结啊 推了N久 推不出啊 然后就郁闷了 打了局游戏 边想边打 实在想不出 看下题解 跑偏了 分两种情况D 方程见代码 ...