[HDUOJ1312]Red And Black (经典的DFS)
Red and Black
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16129 Accepted Submission(s): 9939
is a rectangular room, covered with square tiles. Each tile is colored
either red or black. A man is standing on a black tile. From a tile, he
can move to one of four adjacent tiles. But he can't move on red tiles,
he can move only on black tiles.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
input consists of multiple data sets. A data set starts with a line
containing two positive integers W and H; W and H are the numbers of
tiles in the x- and y- directions, respectively. W and H are not more
than 20.
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)
each data set, your program should output a line which contains the
number of tiles he can reach from the initial tile (including itself).
import java.util.Scanner;
public class Main {
public static int count = 0;
public static int maxN = 0;
public static int maxM = 0;
public static void main( String[] args ) {
Scanner sc = new Scanner( System.in );
int n, m;
while( sc.hasNext() ) {
count = 0;
n = sc.nextInt();
m = sc.nextInt();
int si = 0;
int sj = 0;
if( n == 0 || m == 0 )
return;
else {
maxN = n;
maxM = m;
char[][] maps = new char[ m ][ n ];
for( int i = 0; i < m; i++ ) {
String s = sc.next();
for( int j = 0; j < s.length(); j++ ) {
char c = s.charAt( j );
maps[ i ][ j ] = c;
if( c == '@' ) {
si = i;
sj = j;
}
}
}
dfs( maps, si, sj );
System.out.println( count );
}
}
}
private static void dfs( char[][] maps, int i, int j ) {
if( !cons( i, j ) )
return;
if( maps[ i ][ j ] == '@' || maps[ i ][ j ] == '.' ) {
count++;
maps[ i ][ j ] = '#';
dfs( maps, i + 1, j );
dfs( maps, i - 1, j );
dfs( maps, i, j + 1 );
dfs( maps, i, j - 1 );
} else {
return;
}
}
public static boolean cons( int i, int j ) {
if( i < 0 || j < 0 || i >= maxM || j >= maxN )
return false;
return true;
}
}
[HDUOJ1312]Red And Black (经典的DFS)的更多相关文章
- HDU 1312 Red and Black --- 入门搜索 DFS解法
HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...
- Red and Black(BFS or DFS) 分类: dfs bfs 2015-07-05 22:52 2人阅读 评论(0) 收藏
Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...
- 题解报告:hdu 1312 Red and Black(简单dfs)
Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...
- 78. Subsets(中等,集合的子集,经典问题 DFS)
Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must not ...
- 蓝桥杯 历届试题 约数倍数选卡片 (经典数论+DFS)
闲暇时,福尔摩斯和华生玩一个游戏: 在N张卡片上写有N个整数.两人轮流拿走一张卡片.要求下一个人拿的数字一定是前一个人拿的数字的约数或倍数.例如,某次福尔摩斯拿走的卡片上写着数字“6”,则接下来华生可 ...
- POJ 1321 棋盘问题(非常经典的dfs,入门题)
棋盘问题 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 66277 Accepted: 31639 Descriptio ...
- poj(1011)——Sticks(经典的dfs+剪枝)
题目的大致意思是: 如今有n根木棍,然后须要把它们拼成相同长度的木棍,问满足这个条件的最短的长度是多少? 想法嘛:那肯定是dfs把长度搜一遍就好,但问题的关键是这里会超时.那么就要用到剪枝的原理了. ...
- POJ 1979 Red and Black (zoj 2165) DFS
传送门: poj:http://poj.org/problem?id=1979 zoj:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...
- HDU 1312 Red and Black(经典DFS)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 一道很经典的dfs,设置上下左右四个方向,读入时记下起点,然后跑dfs即可...最后答 ...
随机推荐
- 查看Eclipse版本号,及各个版本区别
查看Eclipse版本号,及各个版本区别 参考:http://blog.csdn.net/gaojinshan/article/details/38903043 查看Eclipse版本号的方法:1.找 ...
- ubuntu开机自动关闭独显,使用集成显卡
我的本子是联想y470p-ise,因为是有双显卡,而ubuntu在开机后,双显卡默认是同时工作,会产生巨大的发热,导致很不爽.而且在ubuntu下基本我也不用独显,所以有开机关闭独显的需求. ubun ...
- C++ 头文件系列(unordered_map、unordered_set)
简介 很明显,这两个头文件分别是map.set头文件对应的unordered版本. 所以它们有一个重要的性质就是: 乱序 如何乱序 这个unorder暗示着,这两个头文件中类的底层实现----Hash ...
- 笑谈ArcToolbox (2) 开启ArcToolbox的钥匙
笑谈ArcToolbox (2) 开启ArcToolbox的钥匙 by 李远祥 GIS人遇到ArcToolbox就像找到了宝藏一样兴奋,但并不是每个找到宝藏的人都具备开启宝藏的钥匙.有时候功能强大并不 ...
- css3 3d变换和动画——回顾
1.transform-style 属性指定嵌套原始是怎样在三维空间中呈现. 语法:transform-style: flat | preserve-3d flat 表示所有子元素在2D平面呈现. p ...
- 导入AS项目出现类文件全部报红色J 原因
大家可能遇到过这么一个问题 就是用androidStudio 导入一个新的demo的时候会出现下图的字样 看了是不是很懵逼 我当时看了也是一脸懵逼 这是什么玩意啊.也不报错.也不提示哪里错了 后来我 ...
- 关于JavaScript中的编码和解码函数
js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent 1 ...
- 纯js实现html转pdf
项目开发中遇到了一个变态需求,需要把一整个页面导出为pdf格式,而且要保留页面上的所有的表格.svg图片和样式.简而言之,就是希望像截图一样,把整个页面截下来,然后保存成pdf.咋不上天呢--查了一下 ...
- [Kafka] - Kafka基本概念介绍
Kafka官方介绍:Kafka是一个分布式的流处理平台(0.10.x版本),在kafka0.8.x版本的时候,kafka主要是作为一个分布式的.可分区的.具有副本数的日志服务系统(Kafka™ is ...
- devexpress实现单元格根据条件显示不同的样式(颜色、字体、对齐方式,大小等)
1.devexpress控件库之所以被大家所喜爱,是因为它将许多常用的东西都封装成了属性.可以通过一些简单的配置,将以前某些需要大篇幅代码才可实现的效果展示出来.这里是一个实现了将[第二列数据在表格0 ...