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. Python 爬虫实战(一):使用 requests 和 BeautifulSoup

    Python 基础 我之前写的<Python 3 极简教程.pdf>,适合有点编程基础的快速入门,通过该系列文章学习,能够独立完成接口的编写,写写小东西没问题. requests requ ...

  2. 用C#写入Excel表并保存

    想用C#操作Excel表,首先要做一些准备工作. 如果要操作 microsoft office Excel 2003表,就需要引入Microsoft office 11.0 object librar ...

  3. 张高兴的 Xamarin.Forms 开发笔记:Android 快捷方式 Shortcut 应用

    一.Shortcut 简介 Shortcut 是 Android 7.1 (API Level 25) 的新特性,类似于苹果的 3D Touch ,但并不是压力感应,只是一种长按菜单.Shortcut ...

  4. js-使用JavaScript、jQuery两种方式实现全选/全不选

    html代码 <input type='checkbox' value="10" name="frust"/>苹果10元 <br/> & ...

  5. Spring任务调度之Quartz集成

    推荐一个博客:http://blog.csdn.net/column/details/14251.html 基本概念 Job:是一个接口,只有一个方法void execute(JobExecution ...

  6. whereis 命令详解

    whereis 作用:whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b).man说明文件(参数-m)和源代码文件(参数-s).如果省略参数,则返回所有信息.  参数:-b 定位可 ...

  7. Java_Date_02_截断日期到日

    oracle 的 trunc 函数能很方便的将日期截断.现在有个需求,需要用java实现与 oracle 的 trunc 函数 相同的功能. 1.需求:将日期截断到日 即 将格式为 2018-01-0 ...

  8. java 泛型基础问题汇总

    泛型是Java SE 1.5的新特性,泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数.这种参数类型可以用在类.接口和方法的创建中,分别称为泛型类.泛型接口.泛型方法. Java语言引 ...

  9. 转:JAVA常见错误处理方法 和 JVM内存结构

    OutOfMemoryError在开发过程中是司空见惯的,遇到这个错误,新手程序员都知道从两个方面入手来解决:一是排查程序是否有BUG导致内存泄漏:二是调整JVM启动参数增大内存.OutOfMemor ...

  10. 基于div表单模拟右对齐

    基于div表单模拟右对齐 --------------------------------------------------------- ----------------------------- ...