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 ...
随机推荐
- 使用UCSC Genome Browser下载人类所有mRNA序列
打开UCSC Genome Browser官网.网址:http://genome.ucsc.edu/ 点击导航栏的Genome Data 在新的页面中,点击human,可快速定位至页面中人类基因组数据 ...
- day17 二分查找
# 什么叫算法 # 计算的方法 # 99 * 13 = 1287 = 13 * 100 - 13 # 查找 : 找数据 # 排序 : # 最短路径 # 我们学习的算法,都是过去时 # 了解基础的算法, ...
- dir()和vars()的区别就是
------------恢复内容开始------------ dir()只打印属性(属性,属性......) 而vars()则打印属性与属性的值(属性:属性值......) >> a='a ...
- Notability
Notability 上课记笔记.听网课→Notability 有录音功能, 在原来笔记中新添加空白行(选中之后下移) Notability常用的功能总结 1.纸张有颜色2.荧光笔会盖住文字3.套索工 ...
- Mysql连接字符,字段函数concat()
Mysql连接字符,字段函数concat() 可将多个字符串或字段连接,多个参数以逗号隔开 select concat('现在是:',new_date) from work
- windows 2008r2+php5.6.28搭建详细过程
安装IIS7 1.打开服务器管理器(开始-计算机-右键-管理-也可以打开),添加角色 直接下一步 勾选Web服务器(IIS),下一步,有个注意事项继续下一步(这里我就不截图了) 勾选ASP.NET会弹 ...
- 一种使用SOC精确控制脉冲的方法
在emfi测试中需要精确的控制脉冲时间.控制器产生的脉冲信号会经过控压的MOS管,这些组件会造成很严重的延时,但是尽管如此,控制系统的高精度也是必须的,因为控制系统的误差会逐级下延,引起更大的误差. ...
- bootstrap 兼容 IE8
在 html 中引用 <!-- bootstrap 兼容 IE8 --> <script src="../../jsapi/js/html5shiv.min.js" ...
- Easyui-Tree和Combotree使用注意事项-sunziren
版权声明:本文为sunziren原创文章,博客园首发,转载务必注明出处以及作者名称. Easyui-Tree和Combotree所使用的数据结构是类似的,在我的上一篇文章<Easyui-Tree ...
- @Value注解没有起作用的梳理
今天在使用@Value注解的时候遇到其不起作用的现象,先把场景说明一下:现在有A类和B类,而A类对象是通过new操作生成的临时对象,而B类对象是在A类中使用的:调试步骤如下: (1)将B类的属性字段都 ...