HDU 1312 Red and Black DFS(深度优先搜索) 和 BFS(广度优先搜索)
Red and Black
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
  
 
 
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
'.' - a black tile
  
 '#' - a red tile
  
 '@' - a man on a black tile(appears exactly once in a data set)
  
 
 
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
59
6
13
方法一 DFS(深度优先搜素)
import java.io.*;
import java.util.*;
public class Main {
int M=22,w,h,sx,sy;
char ch[][];
int fx[]={1,-1,0,0};
int fy[]={0,0,1,-1};
int number;
boolean boo[][]=new boolean[100][100];
public static void main(String[] args) {
new Main().work();
}
void work(){
Scanner sc=new Scanner(new BufferedInputStream(System.in));
while(sc.hasNext()){
w=sc.nextInt();
h=sc.nextInt();
if(h==0&&w==0)
System.exit(0);
ch=new char[h][w];
for(int i=0;i<h;i++){
String s=sc.next();
ch[i]=s.toCharArray();
Arrays.fill(boo[i], false);
}
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
if(ch[i][j]=='@'){
sx=i;
sy=j;
}
}
}
number=1;
boo[sx][sy]=true;
DFS(sx,sy);
System.out.println(number);
}
}
void DFS(int sx,int sy){
for(int i=0;i<4;i++){
int px=sx+fx[i];
int py=sy+fy[i];
if(check(px,py)&&!boo[px][py]){
number++;
boo[px][py]=true;
DFS(px,py);
}
}
}
boolean check(int px,int py){
if(px<0||px>h-1||py<0||py>w-1||ch[px][py]!='.')
return false;
return true;
}
}
方法二 BFS( 广度优先搜索)
import java.io.*;
import java.util.*; public class Main {
Queue<Node> que = new LinkedList<Node>();
boolean boo[][] = new boolean[100][100];
char ch[][];
int w, h;
int fx[] = { 1, -1, 0, 0 };
int fy[] = { 0, 0, 1, -1 };
int number; public static void main(String[] args) {
new Main().work();
} void work() {
Scanner sc = new Scanner(new BufferedInputStream(System.in));
while (sc.hasNext()) {
w = sc.nextInt();
h = sc.nextInt();
if(h==0&&w==0)
System.exit(0);
ch = new char[h][w];
for (int i = 0; i < h; i++) {
String s = sc.next();
ch[i] = s.toCharArray();
Arrays.fill(boo[i], false);
}
Node node = new Node();
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (ch[i][j] == '@') {
node.x = i;
node.y = j;
node.number = 1;
}
}
}
boo[node.x][node.y] = true;
que.add(node);
number = 1;
BFS();
System.out.println(number); }
} void BFS() {
while (!que.isEmpty()) { Node node = que.poll();
for (int i = 0; i < 4; i++) {
int px = node.x + fx[i];
int py = node.y + fy[i];
if (check(px, py) && !boo[px][py]) {
number++;
Node td = new Node();
td.x = px;
td.y = py;
boo[px][py] = true;
ch[px][py] = 'S';
que.add(td);
}
}
}
} boolean check(int px, int py) {
if (px < 0 || px > h - 1 || py < 0 || py > w - 1 || ch[px][py] != '.')
return false;
return true;
} class Node {
int x;
int y;
int number; }
}
HDU 1312 Red and Black DFS(深度优先搜索) 和 BFS(广度优先搜索)的更多相关文章
- HDU 1312 Red and Black(DFS,板子题,详解,零基础教你代码实现DFS)
		
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
 - HDU 1312 Red and Black (DFS & BFS)
		
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:有一间矩形房屋,地上铺了红.黑两种颜色的方形瓷砖.你站在其中一块黑色的瓷砖上,只能向相 ...
 - HDU 1312 Red and Black (DFS)
		
Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...
 - HDU 1312 Red and Black --- 入门搜索 DFS解法
		
HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...
 - HDU 1312:Red and Black(DFS搜索)
		
HDU 1312:Red and Black Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & ...
 - HDU 1312 Red and Black --- 入门搜索 BFS解法
		
HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...
 - 0算法基础学算法 搜索篇第二讲 BFS广度优先搜索的思想
		
dfs前置知识: 递归链接:0基础算法基础学算法 第六弹 递归 - 球君 - 博客园 (cnblogs.com) dfs深度优先搜索:0基础学算法 搜索篇第一讲 深度优先搜索 - 球君 - 博客园 ( ...
 - BFS广度优先搜索 poj1915
		
Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 25909 Accepted: 12244 Descri ...
 - 图的遍历BFS广度优先搜索
		
图的遍历BFS广度优先搜索 1. 简介 BFS(Breadth First Search,广度优先搜索,又名宽度优先搜索),与深度优先算法在一个结点"死磕到底"的思维不同,广度优先 ...
 
随机推荐
- 基于Flume的美团日志收集系统(一)架构和设计
			
美团的日志收集系统负责美团的所有业务日志的收集,并分别给Hadoop平台提供离线数据和Storm平台提供实时数据流.美团的日志收集系统基于Flume设计和搭建而成. <基于Flume的美团日志收 ...
 - 通过运行时动态给OC分类添加属性
			
#import <UIKit/UIKit.h> /** iOS 开发中,分类默认不允许保存属性 如果在分类中,定义一个属性,需要自己实现 getter & setter 方法,而且 ...
 - PHP中括号“{}”的3个作用
			
1,将多个独立语句合并为一个复合语句,例如“if .... else ....”中推荐如此使用. 2,在变量的间接引用中进行定界,避免歧义.例如“${$my_var[8]}”与“${$my_var}[ ...
 - postgresql - mac 启动 关闭 postgresql
			
/Library/PostgreSQL/9.3/bin/pg_ctl -D /Library/PostgreSQL/9.3/data stop /Library/PostgreSQL/9.3/bin/ ...
 - ecshop 模版商品详情页,不同商品调用不同模板
			
1.在goods.php中找到以下代码 $smarty->display('goods.dwt', $cache_id); 改为下面的代码 switch ($goods['cat_id']){ ...
 - 计算机视觉入门 Intorduction To Computer Vision
			
本文将主要介绍图像分类问题,即给定一张图片,我们来给这张图片打一个标签,标签来自于预先设定的集合,比如{people,cat,dog...}等,这是CV的核心问题,图像分类在实际应用中也有许多变形,而 ...
 - 深入理解ob_flush/flush
			
ob_flush/flush在手册中的描述, 都是刷新输出缓冲区, 并且还需要配套使用, 所以会导致很多人迷惑… 其实, 他们俩的操作对象不同, 有些情况下, flush根本不做什么事情.. ob_* ...
 - Android Studio 学习 - AndroidManifest.xml文件学习
			
首先,今天发现了一个很牛逼的教程网站:慕课网(http://www.imooc.com/).有很多大牛发布的教学视频.值得收藏.学习. 今天主要参照陈启超老大的视频,学习了多个Activity之间的切 ...
 - 【英语】Bingo口语笔记(27) - 如何培养口语语感
 - 【转】Xcode添加静态库以及编译选项配置常见问题
			
原文网址:http://www.cnblogs.com/Quains/p/3276425.html 一,Xcode编译出现Link错误,出现"duplicate symbols for ar ...