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 ...
随机推荐
- console 打印消息时,可以使用 %c 指定随后的文本样式; %s 可引用参数变量。
1.console.log 使用 加%c console.log('%c Merry Christmas!!', 'color:green;background:yellow;text-shadow: ...
- 关于apt-get remove 与 apt-get purge
今天在Ubuntu服务器上安装supervisor,部署没成功想卸载重来,sudo apt-get remove supervisor 后发现配置文件还在,便手动删除了配置文件.再次安装,提示配置文件 ...
- opencv —— Laplacian 拉普拉斯算子、二阶导数用于边缘检测
Laplacian 算子简介 求多元函数的二阶导数的映射又称为 Laplacian 算子: 计算拉普拉斯变换:Laplacian 函数 void Laplacian(InputArray src, ...
- 数据库MySQL中关于“多表关联更新”的那些事
在常见的sql中,我们经常在查询中进行多表关联查询,用的比较熟练.今天在开发中遇到一个实际业务场景是多表关联更新,一时不知所措.本着多学习的态度,没有直接写java代码去实现,终于把多表关联更新的sq ...
- ubuntu---CUDA版本与NVIDIA显卡驱动版本对应关系查询
https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html ,如果不是CUDA 10.2 版本的,可以类似的查找 CUDA x ...
- c++并发编程之进程创建(给那些想知道细节的人)
关于多进程创建,此处只讲解一个函数fork(). 1.进程创建 先上代码: #include"iostream" #include<unistd.h> //unix标准 ...
- SQL Server 2012 下载和安装详细教程
https://blog.csdn.net/qq_37591637/article/details/93102794 选择图片中的三个,然后点击下载 ,文件内存很大 下载以后,如图所示,双击.exe程 ...
- lvs使用进阶
之前lvs基础篇(https://www.cnblogs.com/ckh2014/p/10855002.html)中介绍了lvs-dr的搭建,下面我们再复习一下,架构如下: 相关配置 director ...
- 网站后门shell-----eval
我们先来看看网站被攻击的代码: <?php error_reporting(E_ERROR); unlink('user.php'); unlink('../member/login.php') ...
- 与soul上的一个妹子聊天有感
写此篇的原因: 妹子说,我考上公务员了~,当时自己自己顿时哽咽了,不知道说什么,习惯性的说了句,恭喜恭喜啊.感受到妹子的欢喜与喜悦,我也没必要打扰她的兴致,她开心就好了嘛. 每个人的成就都是自己奋斗的 ...