Red and Black

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19690    Accepted Submission(s): 11965

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
Sample Output
45
59
6
13
 
 
 
 
代码:
 #include<bits/stdc++.h>
using namespace std;
int direct [][]={-,,,,,,,-};
char str[][];
bool flag[][];
int w,h,ans;
void DFS(int x,int y){
for(int i=;i<;i++){
int p=x+direct[i][];
int q=y+direct[i][];
if(p>=&&p<h&&q>=&&q<w&&flag[p][q]==&&str[p][q]=='.'){
ans++;
flag[p][q]=;
DFS(p,q);
}
}
}
int main(){
int Dx,Dy;
while(~scanf("%d%d",&w,&h)){
if(w==&&h==)break;
memset(flag,,sizeof(flag));
getchar();
for(int i=;i<h;i++){
for(int j=;j<w;j++){
scanf("%c",&str[i][j]);
if(str[i][j]=='@'){
Dx=i;
Dy=j;
}
}getchar();
}
ans=;
flag[Dx][Dy]=;
DFS(Dx,Dy);
printf("%d\n",ans);
}
return ;
}

HDU1312-Red and Black-DFS的更多相关文章

  1. HDU1312——Red and Black(DFS)

    Red and Black Problem DescriptionThere is a rectangular room, covered with square tiles. Each tile i ...

  2. 数据结构——HDU1312:Red and Black(DFS)

    题目描述 There is a rectangular room, covered with square tiles. Each tile is colored either red or blac ...

  3. 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 ...

  4. HDU1312 Red and Black(dfs+连通性问题)

    这有一间铺满方形瓷砖的长方形客房. 每块瓷砖的颜色是红色或者黑色. 一个人站在一块黑色瓷砖上, 他可以从这块瓷砖移动到相邻(即,上下左右)的四块瓷砖中的一块. 但是他只能移动到黑色瓷砖上,而不能移动到 ...

  5. 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 ...

  6. HDU 1312 Red and Black (DFS)

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

  7. 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 ...

  8. HDOJ1312 Red and black(DFS深度优先搜索)

    There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A ...

  9. hdu1312 Red and Black

    I - Red and Black Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  10. I - Red and Black DFS

    There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A ...

随机推荐

  1. Python3 学习Python流程--试水中

    二.基础语法之后可以搭载服务器练习: 教程 一.1.Python 搭建环境. 初学基本语法 :Python基本语法 2.推荐 IDE :  PyCharm CE 下载 菜鸟教程都是基础语法,可以对py ...

  2. 监听键盘弹起View上调

    可以用三方库IQKeyboardManager 用这个第三方 http://www.jianshu.com/p/f8157895 #pragma mark - keyboard events - // ...

  3. 637. Average of Levels in Binary Tree

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...

  4. bzoj 2756: [SCOI2012]奇怪的游戏

    Description Blinker最近喜欢上一个奇怪的游戏. 这个游戏在一个 N*M 的棋盘上玩,每个格子有一个数.每次 Blinker 会选择两个相邻 的格子,并使这两个数都加上 1. 现在 B ...

  5. Sublime Text 2 Plugin Installation

    For Package Control installation, see the Installation Guide.   To install Emmet(ex Zen Coding), do ...

  6. 初学ssm框架的信息

    ssm框架,就是Spring ,SpringMVC ,mybstis 的简称,我们是从mybstis 开始学起的,mybatis的作用作为一个连接数据库的框架,可以很好配置连接好数据库, 有mybat ...

  7. Tomcat在windows系统中的防火墙设置

    在Win7下安装Tomcat后,其他机器无法访问到Tomcat服务,需要修改防火墙设置. 控制面板->window防火墙->允许程序通过Windows防火墙通信 将Tomcat目录下\bi ...

  8. tomcat发布项目绑定域名总结

    现在很多的公司的网站都是用tomcat作为应用服务区的,可是对于初学者,8080端口号是如何去掉的,这些网站是如何和域名绑定到一起的呢?一个tomcat是如何绑定多域名?并且这些域名是如何对应不同的项 ...

  9. C# .net中json字符串和对象之间的转化方法

    http://blog.csdn.net/xuexiaodong009/article/details/46998069 json作为作为一种最常用的数据,应用很广泛,在.net中如何把一个对象转化为 ...

  10. permission denied for window type 2003

    今天在做系统悬浮窗的时候出现权限拒绝,类型是2003,这里要说下,做系统悬浮窗需要申请权限,6.0以上的 还需要动态申请下,这里我就不过多描述了, 我在申请完权限后仍然不行,这里主要是出现在了这个类型 ...