HDU1241 Oil Deposits(dfs+连通块问题)
背景描述
ztw同志负责探测地下石油储藏.ztw现在在一块矩形区域探测石油.他通过专业设备,来分析每个小块中是否蕴藏石油.如果这些蕴藏石油的小方格相邻(横向相邻,纵向相邻,还有对角相邻),那么它们被认为是同一油藏的一部分.在这块矩形区域,可能有很多油藏.你的任务是确定有多少不同的油藏.
输入
输入可能有多个矩形区域(即可能有多组测试)。每个矩形区域的起始行包含m和n,表示行和列的数量,1<=n,m<=100,如果m=0表示输入的结束,接下来是n行,每行m个字符.每个字符对应一个小方格,并且要么是‘*’、代表没有油,要么是‘@’,表示有油.(注意多组数据,必要的数组要及时清空~)
输出
对于每一个矩形区域,输出油藏的数量.两个小方格是相邻的,当且仅当他们水平或者垂直或者对角线相邻(即8个方向)。
示例输入
示例输出
import java.util.Scanner;
public class Main{
static int n,m,cnt;
static final int N=105;
static char map[][]=new char[N][N];
static int dx[]={0,0,1,-1, 1,-1,1,-1};
static int dy[]={1,-1,0,0,1,-1,-1,1};
static void dfs(int x,int y){
if(map[x][y]=='@'){
map[x][y]='*';
}
for(int i=0;i<8;i++){
int xx=x+dx[i];
int yy=y+dy[i];
if(xx<0 ||yy<0 ||xx>=n ||yy>=m) continue;
if(map[xx][yy]=='*') continue;
dfs(xx,yy);
}
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
while(scan.hasNext()){
n=scan.nextInt();
m=scan.nextInt();
if(n==0 && m==0) break;
for(int i=0;i<n;i++){
map[i]=scan.next().toCharArray();
}
cnt=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(map[i][j]=='@'){
dfs(i,j);
cnt++;
}
System.out.println(cnt);
}
}
}
HDU1241 Oil Deposits(dfs+连通块问题)的更多相关文章
- HDU1241 Oil Deposits —— DFS求连通块
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 Oil Deposits Time Limit: 2000/1000 MS (Java/Othe ...
- HDU-1241 Oil Deposits (DFS)
Oil Deposits Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- Hdu1241 Oil Deposits (DFS)
Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground ...
- HDOJ(HDU).1241 Oil Deposits(DFS)
HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- UVa572 Oil Deposits DFS求连通块
技巧:遍历8个方向 ; dr <= ; dr++) ; dc <= ; dc++) || dc != ) dfs(r+dr, c+dc, id); 我的解法: #include< ...
- HDU - 1241 POJ - 1562 Oil Deposits DFS FloodFill漫水填充法求连通块问题
Oil Deposits The GeoSurvComp geologic survey company is responsible for detecting underground oil de ...
- DFS(连通块) HDU 1241 Oil Deposits
题目传送门 /* DFS:油田问题,一道经典的DFS求连通块.当初的难题,现在看上去不过如此啊 */ /************************************************ ...
- UVa 572 Oil Deposits(DFS)
Oil Deposits The GeoSurvComp geologic survey company is responsible for detecting underground oil ...
- [POJ] 1562 Oil Deposits (DFS)
Oil Deposits Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16655 Accepted: 8917 Des ...
- HDU1241 Oil Deposits 2016-07-24 13:38 66人阅读 评论(0) 收藏
Oil Deposits Problem Description The GeoSurvComp geologic survey company is responsible for detectin ...
随机推荐
- kong服务网关API
kong服务网关API pingforever关注 0.1762017.05.23 11:16:08字数 834阅读 7,367 kong简介 Kong 是在客户端和(微)服务间转发API通信的API ...
- Kubernetes CI/CD(1)
本文通过在kubernetes上启动Jenkins服务,并将宿主机上的docker.docker.sock挂载到Jenkins容器中,实现在Jenkins容器中直接打镜像的形式实现CI功能. Kube ...
- 服务器控件Repeater
在使用aspx开发中,如果一个页面做纯数据展示,Repeater会比GridView更适合,因为它是轻量级的 下面是最基本的用法: aspx: <table> <asp:Repea ...
- Android中创建一个BroadcastReceiver
首先创建一个java类继承BroadcastReceiver类 package com.example.service; import android.content.BroadcastReceive ...
- npm 安装与部署
nodejs 安装 查看版本 官方网址 下载linux版,64位 wget https://npm.taobao.org/mirrors/node/v12.13.1/node-v12.13.1-lin ...
- [大数据技术]Kettle从CSV文件读取清洗后到MySQL中文乱码问题
首先要知道CSV文件的编码格式 然后在文件输入编码选择编码格式, 第二步,在每个转换或者作业的DB连接中选择选项,并添加如下内容: 中文乱码问题得到解决
- 【感知机模型】手写代码训练 / 使用sklearn的Perceptron模块训练
读取原始数据 import pandas as pd import numpy as np in_data = pd.read_table('./origin-data/perceptron_15.d ...
- 阿里云搭建k8s高可用集群(1.17.3)
首先准备5台centos7 ecs实例最低要求2c4G 开启SLB(私网) 这里我们采用堆叠拓扑的方式构建高可用集群,因为k8s 集群etcd采用了raft算法保证集群一致性,所以高可用必须保证至少3 ...
- spawn id exp6 not open
原因 脚本没有被执行 解决方案 手动执行该条命令参看报错的原因
- 使用Python库paramiko登录远程设备
前言 手动下载paramiko库的安装包.在PyPi库中查找即可,但是不到是我的电脑问题还是网络问题,2.0.0以上版本我都安装不了,因此我自己是安装的paramiko 1.17.0版本,此版本经过测 ...