这有一间铺满方形瓷砖的长方形客房。 每块瓷砖的颜色是红色或者黑色。 一个人站在一块黑色瓷砖上, 他可以从这块瓷砖移动到相邻(即,上下左右)的四块瓷砖中的一块。 但是他只能移动到黑色瓷砖上,而不能移动到红色瓷砖上。

编写一个程序,通过重复上述动作来计算他可以达到的黑色瓷砖的数量。

Input输入包含多组数据。 每组数据包含两个正整数W和H; H表示瓷砖的行数,W表示瓷砖的列数。 W和H不超过20。

瓷砖的颜色用字符表示,如下所示。

'.' - 黑色瓷砖 
'#' - 红色瓷砖 
'@' - 站在黑色瓷砖上的人(每组数据中只有一个) 
Output对于每组数据,你的程序应输出一行,其中包含他可以到达的黑色瓷砖数目。(站着的黑色瓷砖也要包含在内) 
Sample Input

6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0

Sample Output

45
59
6
13 代码:
import java.util.Scanner;

public class Main{
static int n,m,cnt;
static final int N=1005;
static char map[][]=new char[N][N];
static int dx[]={0,0,1,-1};
static int dy[]={1,-1,0,0};
static void dfs(int x,int y){
if(map[x][y]=='.'){
cnt++;
map[x][y]='#';
}
for(int i=0;i<4;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()){
m=scan.nextInt();
n=scan.nextInt();
if(n==0 && m==0) break; for(int i=0;i<n;i++)
map[i]=scan.next().toCharArray(); cnt=1;
boolean flag=false;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++)
if(map[i][j]=='@'){
dfs(i,j);
flag=true;
break;
}
if(flag) break;
}
System.out.println(cnt);
}
}
}

HDU1312 Red and Black(dfs+连通性问题)的更多相关文章

  1. HDU1312——Red and Black(DFS)

    Red and Black Problem DescriptionThere is a rectangular room, covered with square tiles. Each tile i ...

  2. 数据结构——HDU1312:Red and Black(DFS)

    题目描述 There is a rectangular room, covered with square tiles. Each tile is colored either red or blac ...

  3. HDU1312 Red and Black(DFS) 2016-07-24 13:49 64人阅读 评论(0) 收藏

    Red and Black Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

  4. HDU 1312 Red and Black DFS(深度优先搜索) 和 BFS(广度优先搜索)

    Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

  5. HDU 1312 Red and Black (DFS)

    Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...

  6. 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 ...

  7. HDOJ1312 Red and black(DFS深度优先搜索)

    There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A ...

  8. hdu1312 Red and Black

    I - Red and Black Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  9. I - Red and Black DFS

    There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A ...

随机推荐

  1. POST注入之sqlmap

    POST注入方法一加—form跑数据库sqlmap.py -u http://59.63.200.79:8815/Pass-05/index.php —form —dbs跑出数据库后查询表名 假设库名 ...

  2. RMAN中MAXSETSIZE和MAXPIECESIZE的用法

    MAXSETSIZE跟MAXPIECESIZE用法 区别:maxpiecesize设置的是备份完成后的备份片大小,对备份整体的大小没有影响,比如一个G的备份完成文件,maxpiecesize设置为10 ...

  3. 【python基础语法】函数的作用域和内置函数和关键字(第7天课堂笔记)

    一.函数的作用域: 1.全局变量 2.局部变量 3.global 二.内置函数 1.常见的内置函数 print : 输出 input : 输入 type : 查看数据类型 id : 获取数据的内存地址 ...

  4. pgspider gzip fdw试用(集成gzip+http+graphql-engine)

    gzip 也是一个在实际中比较有用的处理工具,可以减少数据传输,以下是集成gzip http 以及plv8 的处理 gzip Docker 镜像 Dockerfile FROM dalongrong/ ...

  5. Mybaits(9)MyBatis级联-2

    一.鉴别器和一对多级联 1.完善体检表,分为男雇员体检和女雇员体检表 (1)持久层dao编写 package com.xhbjava.dao; import com.xhbjava.domain.Ma ...

  6. 【sklearn决策树算法】DecisionTreeClassifier(API)的使用以及决策树代码实例 - 鸢尾花分类

    决策树算法 决策树算法主要有ID3, C4.5, CART这三种. ID3算法从树的根节点开始,总是选择信息增益最大的特征,对此特征施加判断条件建立子节点,递归进行,直到信息增益很小或者没有特征时结束 ...

  7. echarts 【图表的基本使用】

    一.柱状图 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <tit ...

  8. 正确安装Windows server 2012 r2的方法

    正确安装Windows server 2012 r2的方法,请看下面的步骤 方法/步骤 1 准备好镜像文件,安装 2 输入密钥,下一步 选择下面(带有GUI的服务器).不要选上面的(服务器核心安装), ...

  9. [NOI2003]文本编辑器 [Fhq Treap]

    [NOI2003]文本编辑器 没啥好说的 就是个板子 #include <bits/stdc++.h> // #define int long long #define rep(a , b ...

  10. wxPython学习笔记

    ------------恢复内容开始------------ 学习wxPython 资料 1.wxpython wiki Getting started with wxPython https://w ...