HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)
Oil Deposits
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
1
2
2
方法一:DFS(深度优先搜索)
import java.io.*;
import java.util.*;
public class Main {
public char ch[][];
public int fx[]={1,1,1,-1,-1,-1,0,0};
public int fy[]={0,1,-1,0,1,-1,1,-1};
public int m,n;
public static void main(String[] args) {
new Main().work();
}
public void work(){
Scanner sc=new Scanner(new BufferedInputStream(System.in));
while(sc.hasNext()){
m=sc.nextInt();
n=sc.nextInt();
ch=new char[m][n];
if(m==0)
System.exit(0);
for(int i=0;i<m;i++){
String s=sc.next();
ch[i]=s.toCharArray();
}
int toal=0;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(ch[i][j]=='@'){
toal++;
dfs(i,j);
}
}
}
System.out.println(toal);
}
}
public void dfs(int x,int y){
for(int i=0;i<8;i++){
int px=x+fx[i];
int py=y+fy[i];
if(check(px,py)){
ch[px][py]='*';
dfs(px,py);
}
}
}
public boolean check(int px,int py){
if(px<0||px>m-1||py<0||py>n-1||ch[px][py]!='@')
return false;
return true;
}
}
方法二BFS(广度优先搜索)
import java.io.*;
import java.util.*;
public class Main {
public int m,n;
public char ch[][];
public Queue<Bnode> list=new LinkedList<Bnode>();
public int fx[]={1,1,1,-1,-1,-1,0,0};
public int fy[]={0,1,-1,0,1,-1,1,-1};
public static void main(String[] args) {
new Main().work();
}
public void work(){
Scanner sc=new Scanner(new BufferedInputStream(System.in));
while(sc.hasNext()){
list.clear();
m=sc.nextInt();
n=sc.nextInt();
if(m==0)
System.exit(0);
ch=new char[m][n];
for(int i=0;i<m;i++){
String s=sc.next();
ch[i]=s.toCharArray();
}
int total=0;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(ch[i][j]=='@'){
total++;
Bnode bnode=new Bnode();
bnode.x=i;
bnode.y=j;
list.add(bnode); BFS();
}
}
}
System.out.println(total);
}
}
public void BFS(){
while(!list.isEmpty()){
Bnode bnode=list.poll();
for(int i=0;i<8;i++){
int px=bnode.x+fx[i];
int py=bnode.y+fy[i];
if(check(px,py)){
ch[px][py]='*';
Bnode t=new Bnode();
t.x=px;
t.y=py;
list.add(t);
}
}
}
}
public boolean check(int px,int py){
if(px<0||px>m-1||py<0||py>n-1||ch[px][py]!='@')
return false;
return true;
}
}
class Bnode {
int x;
int y;
}
HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)的更多相关文章
- HDOJ(HDU).1241 Oil Deposits(DFS)
HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- HDU 1241 Oil Deposits (DFS/BFS)
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- HDU 1241 Oil Deposits DFS搜索题
题目大意:给你一个m*n的矩阵,里面有两种符号,一种是 @ 表示这个位置有油田,另一种是 * 表示这个位置没有油田,现在规定相邻的任意块油田只算一块油田,这里的相邻包括上下左右以及斜的的四个方向相邻的 ...
- HDU 1241 Oil Deposits (DFS or BFS)
链接 : Here! 思路 : 搜索判断连通块个数, 所以 $DFS$ 或则 $BFS$ 都行喽...., 首先记录一下整个地图中所有$Oil$的个数, 然后遍历整个地图, 从油田开始搜索它所能连通多 ...
- 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 --- 入门DFS
HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...
- DFS(连通块) HDU 1241 Oil Deposits
题目传送门 /* DFS:油田问题,一道经典的DFS求连通块.当初的难题,现在看上去不过如此啊 */ /************************************************ ...
- HDU 1241 Oil Deposits(石油储藏)
HDU 1241 Oil Deposits(石油储藏) 00 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Probl ...
- hdu 1241:Oil Deposits(DFS)
Oil Deposits Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
随机推荐
- Ajaxload动态加载动画生成工具的实现(ajaxload的本地移植)
前言 前段时间看到一个国外的网站,在线生成ajax loading动画.觉得很实用,于是动起了移植到自己网站的念头(一直以来的习惯,看到好的工具总想着移植到本地好好研究).根据以往移植的经验最终把 这 ...
- 整理一些js中常见的问题
原文链接 1.js获取select标签选中的值 原生js var obj = document.getElementByIdx_x(”testSelect”); //定位id var index = ...
- WM8962 HPOUT 信号强度 时间周期
/*************************************************************************** * WM8962 HPOUT 信号强度 时间周 ...
- python练习程序(c100经典例3)
题目: 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少? for i in range(1,100000): a=i+100; b=a+168; sa=int ...
- python练习程序(显示图像)
import matplotlib as mpl import Image import numpy as np import matplotlib.pyplot as plt im=Image.op ...
- [转] gc tips(1)
所有应用软件都需要管理内存,一个应用软件的内存管理系统包括了如下准则:什么时候派发内存,要派发多少内存,什么时候把东西放到回收站,以及什么时候清空回收站.MMgc是Flash Player几乎所有内存 ...
- Android的两种上下文的区别
1.Activity.this,Activity是间接继承自Context 2.getApplicationContext()返回来的就是Context 3.getBaseContext()返回的也是 ...
- 构建通过 Database.com 提供技术支持的 PhoneGap 应用程序
要求 其他必要产品 Database.com account 用户级别 全部 必需产品 PhoneGap Build 范例文件 Database.Com-PhoneGap-Sample 在这篇文章中, ...
- GET与POST在什么情况下使用
GET与POST 你可能想了解GET和POST之间有什么区别,并想知道什么时候使用它们.从理论上讲,如果请求是幂等的就可以使用GET,所谓幂等是指多个请求返回相同的结果.实际上,相应的服务器方法可能会 ...
- poj 3270(置换群)
题意:给定n头母牛的脾气大小,然后让你通过交换任意两头母牛的位置使得最后的母牛序列的脾气值从小到大,交换两头母牛的代价是两个脾气之和,使得代价最小. 分析:以前做过一道题,只有一个地方和这道题不同,但 ...