[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即可...最后答 ...
随机推荐
- iOS开发之内存缓存机制
使用缓存的目的是为了使用的应用程序能更快速的响应用户输入,是程序高效的运行.有时候我们需要将远程web服务器获取的数据缓存起来,减少对同一个url多次请求. 内存缓存我们可以使用sdk中的NSURLC ...
- UED大全
http://www.baiduux.com/ 百度UFOhttp://ued.sohu.com/ 搜狐UEDhttp://ued.taobao.com/ 淘宝UEDhttp://www.ued ...
- Linux Platform驱动模型(一)-设备信息
我在Linux字符设备驱动框架一文中简单介绍了Linux字符设备编程模型,在那个模型中,只要应用程序open()了相应的设备文件,就可以使用ioctl通过驱动程序来控制我们的硬件,这种模型直观,但是从 ...
- MySQL插入数据中文乱码问题的解决
一.使用语句 show variables like 'character%'; 来查看当前数据库的相关编码集. 1.启动cmd,登录mysql ①cd C:\Program Files\MySQL\ ...
- 最近一年多我总结的常用mate标签-常用mate标签
昨天开始上班 ,今天晚上不是太忙 ,来写篇博客了 meta元素共有三个可选属性(http-equiv.name和scheme)和一个必选属性(content),content定义与 http-equ ...
- 日月如梭,玩转JavaScript日期
一.Date对象 下面出现的源码都可以codepen在线查看. 1)时间戳毫秒计算 Date对象是基于"1970-01-01 08:00:00"到指定日期的毫秒数,不是" ...
- Omi教程-组件
组件 Omi框架完全基于组件体系设计,我们希望开发者可以像搭积木一样制作Web程序,一切皆是组件,组件也可以嵌套子组件形成新的组件,新的组件又可以当作子组件嵌套至任意组件形成新的组件... 简单组件 ...
- window nfs 服务端配置安装
windows7下面安装nfs客户端命令(首先开启windows客户端mount挂载命令): 打开或关闭windows功能>nfs服务(勾选上)重启 windows nfs共享有两种方式分别 ...
- 你需要了解的 Core Spotlight
你需要了解的 Core Spotlight -- 了解Spotlight Core Spotlight是iOS 9中苹果推出了新的Search API,可以直接搜App内的内容(in-App Sear ...
- 微信小程序image组件binderror使用例子(对应html、js中的onerror)
官方文档 binderror HandleEvent 当错误发生时,发布到 AppService 的事件名,事件对象event.detail = {errMsg: 'something wrong' ...