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
 
 
 
 
代码:
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int direct [][]={-,,,,,,,-};
  4. char str[][];
  5. bool flag[][];
  6. int w,h,ans;
  7. void DFS(int x,int y){
  8. for(int i=;i<;i++){
  9. int p=x+direct[i][];
  10. int q=y+direct[i][];
  11. if(p>=&&p<h&&q>=&&q<w&&flag[p][q]==&&str[p][q]=='.'){
  12. ans++;
  13. flag[p][q]=;
  14. DFS(p,q);
  15. }
  16. }
  17. }
  18. int main(){
  19. int Dx,Dy;
  20. while(~scanf("%d%d",&w,&h)){
  21. if(w==&&h==)break;
  22. memset(flag,,sizeof(flag));
  23. getchar();
  24. for(int i=;i<h;i++){
  25. for(int j=;j<w;j++){
  26. scanf("%c",&str[i][j]);
  27. if(str[i][j]=='@'){
  28. Dx=i;
  29. Dy=j;
  30. }
  31. }getchar();
  32. }
  33. ans=;
  34. flag[Dx][Dy]=;
  35. DFS(Dx,Dy);
  36. printf("%d\n",ans);
  37. }
  38. return ;
  39. }

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. Gulp 的简单使用(原创)

    1.安装nodejs 安装省略 npm的全称是Node Package Manager,是随同NodeJS一起安装的包管理和分发工具,它很方便让JavaScript开发者下载.安装.上传以及管理已经安 ...

  2. python学习日记:day11----装饰器进阶

    1.wraps from functools import wraps def wrapper(func): #func = holiday @wraps(func)#输出holiday的函数名 de ...

  3. Linux权限分析

    我看过网上的一些有关Linux的权限分析,有些说的不够清楚,另外一些说的又太复杂.这里我尽量简单.清楚的把Linux权限问题阐述明白,Linux权限没有那么复杂. Linux权限问题要区分文件权限和目 ...

  4. boost::assign(标准容器填充库)

    boost::assign通过对"+="和","的重载非常方便的填充标准容器(std::vector,std::set,std::list,std::map), ...

  5. js获取图片的EXIF,解决图片旋转问题

    相信大家在做项目的时候会遇到在canvas里加入图片时,图片发生90°,180°的旋转.当时的你肯定时懵逼的,为毛. 其实这就是图片的EXIF搞的鬼. 什么是EXIF 简单来说,Exif 信息就是由数 ...

  6. oracle自动备份_expdp_Linux

    [oracle@hbsjxtdb1 ~]$ crontab -e 0 4 * * * /backup/script/backupexpdp.sh [oracle@hbsjxtdb1 ~]$ cront ...

  7. Webpack 2 视频教程 019 - Webpack 2 中配置多页面编译

    原文发表于我的技术博客 这是我免费发布的高质量超清「Webpack 2 视频教程」. Webpack 作为目前前端开发必备的框架,Webpack 发布了 2.0 版本,此视频就是基于 2.0 的版本讲 ...

  8. python科学计算_numpy_ufunc

    ufunc简介 ufunc指universal function,是一种能够对数组中的所有元素进行操作的函数,ufunc是针对数组进行操作的函数,对一个数组进行重复的运算时,使用ufunc比math库 ...

  9. 【转】完美解读Linux中文件系统的目录结构

    一.前 言 接触Linux也有一段时间了,不过这几天在编译开源程序时,才发现自己对linux文件系统的目录结构了解的不够透彻,很多重要目录都说不清楚是用来干嘛的,于是在网上百度了一下这方面的介绍,根据 ...

  10. angular4.0命令行汇总

    查看ng命令行 ng help 创建项目 ng new projectName ng new projectName --routing[--routing表示创建带路由的项目] 配置依赖 npm i ...