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,广度优先搜索,又名宽度优先搜索),与深度优先算法在一个结点"死磕到底"的思维不同,广度优先 ...
随机推荐
- 一个小面试题sql
一. 问答题 1简要说明分页是如何实现的. A:sqlserver: Select top(pagesize) * from student where id not in( ...
- IOS中微博正文开发步骤总结
微博正文开发步骤总结 1.新建正文控制器,在点击首页的某一条微博时跳转过去 2.在MainController中设置导航控制器的代理,监听所有导航控制器的跳转 1> 如果即将显示的不是根控制器 ...
- hihoCoder #1179 : 永恒游戏 (暴力枚举)
题意: 给出一个有n个点的无向图,每个点上有石头数个,现在的游戏规则是,设置某个点A的度数为d,如果A点的石子数大于等于d,则可以从A点给每个邻接点发一个石子.如果游戏可以玩10万次以上,输出INF, ...
- 学习java之利用泛型访问自己定义的类
如果有多个类,而且他们其中有一些方法是相同的,我是选择在每个类中都把这些方法实现一遍呢,还是选择泛型.我今天自己花了一点时间看了看泛型,实践了一下. Holder.java package regex ...
- dede 5.7进后台卡死解决办法
注释后台文件dede/templets/index_body.htm(大概在第18行) <script type="text/javascript"> function ...
- 【英语】Bingo口语笔记(11) - 表示“身体抱恙”
- LCS nlog(n) 但最坏情况还是比较悲剧 转载的文章;
最长公共子序列问题: 给定2个字符串,求其最长公共子串.如abcde和dbada的最长公共字串为bd. 动态规划:dp[i][j]表示A串前i个和B串前j个的最长公共子串的长度. 则 若A[i] == ...
- 《A Tour of PostgreSQL Internals》学习笔记——进程间通信
中秋节假期这么快就没了,这几天还一直下雨,索性在家看看书.这次看的是Tom Lane的<A Tour of PostgreSQL Internals>.这篇小随笔就算做学习笔记了.园子里面 ...
- Http中Cookie和Session介绍
先介绍下B/S系统的工作的完整过程.首先客户端的浏览器发出请求,服务端的webserver接受到请求后,调用相关请求的页面进行处理,处理完后将结果发送给客户端的浏览器进行显示.只能是浏览器向webse ...
- 细雨学习笔记:Jmeter集合点
设置集合点的原则 (1) 集合点设置数<=线程组线程数量(因为大于线程组线程数量的话就永远也到不了集合点) (2)线程组线程数量是集合点设置数的整数倍(因为分组有余数的话最后一组永远也到不了集合 ...