Red and Black

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 12433 Accepted Submission(s): 7726

Problem Description
There 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
The 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)
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).

Sample Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
Sample Output
45
59
6
13
Source
Recommend
Eddy | We have carefully selected several similar problems for you:
1016 1010 1372

pid=1242" target="_blank">
1242
1253


#include<stdio.h>
#include<string.h>
char map[22][22];
int dd[22][22];
int n,m;
void DP(int i,int j,int &res){
if(i<=0||i>n||j<0||j>=m) return ;
if(map[i][j]=='#') return;
if(dd[i][j]==0&&(map[i][j]=='.'||map[i][j]=='@')) {
dd[i][j]=1;
res++;
DP(i-1,j,res);DP(i+1,j,res);
DP(i,j-1,res);DP(i,j+1,res);
}
}
int main(){
while(scanf("%d %d",&m,&n),n||m){
memset(map,0,sizeof(map));
memset(dd,0,sizeof(dd));
int i,j,res,num=0;int tx,ty; for(i=1;i<=n;++i){
scanf("%s",map[i]);
}
for(i=1;i<=n;++i){
for(j=0;j<m;++j)
if(map[i][j]=='@')
tx=i,ty=j;
}
//printf("%d====%d\n",tx,ty);;
res=0;
DP(tx,ty,res);
printf("%d\n",res);
}
return 0;
}

hdoj-1312-Red and Black的更多相关文章

  1. Hdoj 1312.Red and Black 题解

    Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...

  2. poj-1979 && hdoj - 1312 Red and Black (简单dfs)

    http://poj.org/problem?id=1979 基础搜索. #include <iostream> #include <cstdio> #include < ...

  3. HDU 1312 Red and Black (dfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...

  4. HDU 1312 Red and Black --- 入门搜索 BFS解法

    HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...

  5. HDU 1312 Red and Black --- 入门搜索 DFS解法

    HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...

  6. HDU 1312:Red and Black(DFS搜索)

      HDU 1312:Red and Black Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & ...

  7. HDU 1312 Red and Black(最简单也是最经典的搜索)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...

  8. HDU 1312 Red and Black(bfs,dfs均可,个人倾向bfs)

    题目代号:HDU 1312 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/100 ...

  9. HDOJ 1312题Red and Black

    Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

  10. HDOJ 1312 (POJ 1979) Red and Black

    Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...

随机推荐

  1. 机器学习 LR中的参数迭代公式推导——极大似然和梯度下降

    Logistic本质上是一个基于条件概率的判别模型(DiscriminativeModel). 函数图像为: 通过sigma函数计算出最终结果,以0.5为分界线,最终结果大于0.5则属于正类(类别值为 ...

  2. 存储概念解析:NAS与SAN的区别

    目前存储网络技术领域中的两个主旋律是SAN(存储区域网络)和NAS(网络连接区域存储),两者都宣称是解决现代企业高容量数据存储需求的最佳选择. 正如在餐厅就餐时大厨不会为您传菜,跑堂不会为您烹制鲜橙烩 ...

  3. DB-MySQL:MySQL 教程

    ylbtech-DB-MySQL:MySQL 教程 1.返回顶部 1. MySQL 教程 MySQL 是最流行的关系型数据库管理系统,在WEB应用方面 MySQL 是最好的RDBMS(Relation ...

  4. checkbox复选框和div click事件重叠,点击div后复选框也被选中,同时改变div颜色,否则则不选中

     checkbox复选框和div click事件重叠,点击div后复选框也被选中,同时改变div颜色,否则则不选中 <!DOCTYPE html> <html lang=" ...

  5. 50个极好的bootstrap 后台框架主题下载

    50个极好的bootstrap 后台框架主题下载 http://sudasuta.com/bootstrap-admin-templates.html 越来越多的设计师和前端工程师开始用bootstr ...

  6. NLog日志记录

    配置NLog         NLog支持 .Net 4.5 以及以上版本!              首先去下载NLog的DLL下载地址:http://nlog-project.org/downlo ...

  7. 用VS2015创建ASP.NET Web Forms 应用程序

    在 Visual Studio 2015 中,按着以下步骤创建一个 Web Forms 应用程序项目: 1.起始页/文件--->新建项目--->已安装--->模板--->Vis ...

  8. mvel2.0语法指南

    虽然mvel吸收了大量的java语法,但作为一个表达式语言,还是有着很多重要的不同之处,以达到更高的效率,比如:mvel像正则表达式一样,有直接支持集合.数组和字符串匹配的操作符. 除了表达式语言外, ...

  9. submile 安装,汉化,插件

    /*删除以前配置文件*/ 删除以前版本sublime后,在删除以前版本的配置信息:直接在C盘 查询里面输入 Roming  然后查找里面的 sublime 文件夹,把他给删除掉 ----------- ...

  10. Codeforces Round #499 (Div. 2) F. Mars rover_dfs_位运算

    题解: 首先,我们可以用 dfsdfsdfs 在 O(n)O(n)O(n) 的时间复杂度求出初始状态每个点的权值. 不难发现,一个叶子节点权值的取反会导致根节点的权值取反当且仅当从该叶子节点到根节点这 ...