HDU1312 Red and Black(dfs+连通性问题)
编写一个程序,通过重复上述动作来计算他可以达到的黑色瓷砖的数量。
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+连通性问题)的更多相关文章
- HDU1312——Red and Black(DFS)
Red and Black Problem DescriptionThere is a rectangular room, covered with square tiles. Each tile i ...
- 数据结构——HDU1312:Red and Black(DFS)
题目描述 There is a rectangular room, covered with square tiles. Each tile is colored either red or blac ...
- 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 ...
- 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 ...
- 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,板子题,详解,零基础教你代码实现DFS)
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDOJ1312 Red and black(DFS深度优先搜索)
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A ...
- hdu1312 Red and Black
I - Red and Black Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- I - Red and Black DFS
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A ...
随机推荐
- 初识OpenSSH--1
note:保护你的报文(communique)安全 ! 最安全!!! 简介:OpenSSH使用SSH协议进行远程登录的主要连接工具.它对传输数据进行加密,以消除窃听,连接劫持和其他攻击.此外,Ope ...
- C# WPF联系人列表(1/3)
微信公众号:Dotnet9,网站:Dotnet9,问题或建议:请网站留言, 如果对您有所帮助:欢迎赞赏. C# WPF联系人列表(1/3) 阅读导航 本文背景 代码实现 本文参考 1.本文背景 聊天软 ...
- 吴裕雄--天生自然HADOOP操作实验学习笔记:hbase学生选课案例
实验目的 复习hbase的shell操作和javaAPI操作 了解javaWeb项目的MVC设计 学会dao(数据库访问对象)和service层的代码编写规范 学会设计hbase表格 实验原理 前面我 ...
- 代数式到c语言表达式和常用的c语言数学库函数_pow_sqrt_exp_fabs_abs
数学知识来源于生活,因此我们需要把相关的数学的知识在自己生活找到实例. #include "common.h" #include <stdio.h> #include ...
- java【第三课 条件语句】
一.java条件语句 import java.util.Scanner; //导入扫描仪 public class demo{ public static void main(String[] arg ...
- linux下安装setuptools
wget https://pypi.python.org/packages/07/a0/11d3d76df54b9701c0f7bf23ea9b00c61c5e14eb7962bb29aed866a5 ...
- 如何开发自己的第一个 Serverless Component
前言 上一篇 基于 Serverless Component 的全栈解决方案 介绍 Serverless Component 是什么和如何使用 Serverless Component 开发一个全栈应 ...
- Nuxt服务端使用Axios调用接口时传递cookies
个人博客 地址:http://www.wenhaofan.com/article/20190321183709 介绍 在做单点登录时,后端需要根据cookie获取登录用户,由于前端项目使用了Nuxt做 ...
- Redis基础详解
1. Redis是什么.特点.优势 Redis是一个开源的使用C语言编写.开源.支持网络.可基于内存亦可持久化的日志型.高性能的Key-Value数据库,并提供多种语言的API. 它通常被称为 数据结 ...
- 项目部署 ubuntu Django uwsgi配置
1.进入项目文件夹 mkdir uwsgi_file vim uwsgi.ini 写入保存 [uwsgi] chdir = /home/mysite/my_project # 项目目录 module ...