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)

The end of the input is indicated by a line consisting of two zeros.

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

  1. 6 9
  2. ....#.
  3. .....#
  4. ......
  5. ......
  6. ......
  7. ......
  8. ......
  9. #@...#
  10. .#..#.
  11. 11 9
  12. .#.........
  13. .#.#######.
  14. .#.#.....#.
  15. .#.#.###.#.
  16. .#.#..@#.#.
  17. .#.#####.#.
  18. .#.......#.
  19. .#########.
  20. ...........
  21. 11 6
  22. ..#..#..#..
  23. ..#..#..#..
  24. ..#..#..###
  25. ..#..#..#@.
  26. ..#..#..#..
  27. ..#..#..#..
  28. 7 7
  29. ..#.#..
  30. ..#.#..
  31. ###.###
  32. ...@...
  33. ###.###
  34. ..#.#..
  35. ..#.#..
  36. 0 0

Sample Output

  1. 45
  2. 59
  3. 6
  4. 13
  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. #include<algorithm>
  5. using namespace std;
  6. int n,m;
  7. int ans=;
  8. int vis[][];
  9. char title[][];
  10. int fx[]={-,,,},fy[]={,,,-};
  11.  
  12. void dfs(int x,int y){
  13. ans++;
  14. vis[x][y]=;
  15. for(int i=; i<; i++ ){
  16. int nx=x+fx[i];
  17. int ny=y+fy[i];
  18. if(nx>=&&nx<n&&ny>=&&ny<m&&title[nx][ny]=='.'&&!vis[nx][ny]){
  19. dfs(nx,ny);
  20. }
  21. }
  22. }
  23.  
  24. int main(){
  25. while(~scanf("%d%d",&m,&n)&&n&&m){
  26. // getchar();
  27. ans=;
  28. memset(vis,,sizeof(vis));
  29. for( int i=; i<n; i++ ){
  30. cin>>title[i];
  31. }
  32. for( int i=; i<n; i++ ){
  33. for(int j=; j<m; j++ ){
  34. if(title[i][j]=='@'&&!vis[i][j]){
  35. dfs(i,j);
  36. break;
  37. }
  38. }
  39. }
  40. printf("%d\n",ans);
  41. }
  42.  
  43. return ;
  44. }

Red and Black---POJ - 1979的更多相关文章

  1. Red and Black(poj 1979 bfs)

    Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27891   Accepted: 15142 D ...

  2. DFS:Red and Black(POJ 1979)

    红与黑 题目大意:一个人在一个矩形的房子里,可以走黑色区域,不可以走红色区域,从某一个点出发,他最多能走到多少个房间? 不多说,DFS深搜即可,水题 注意一下不要把行和列搞错就好了,我就是那样弄错过一 ...

  3. POJ 1979 Red and Black (红与黑)

    POJ 1979 Red and Black (红与黑) Time Limit: 1000MS    Memory Limit: 30000K Description 题目描述 There is a ...

  4. OpenJudge/Poj 1979 Red and Black / OpenJudge 2816 红与黑

    1.链接地址: http://bailian.openjudge.cn/practice/1979 http://poj.org/problem?id=1979 2.题目: 总时间限制: 1000ms ...

  5. poj 1979 Red and Black 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=1979 Description There is a rectangular room, covered with square tiles ...

  6. POJ 1979 Red and Black dfs 难度:0

    http://poj.org/problem?id=1979 #include <cstdio> #include <cstring> using namespace std; ...

  7. poj 1979 Red and Black(dfs)

    题目链接:http://poj.org/problem?id=1979 思路分析:使用DFS解决,与迷宫问题相似:迷宫由于搜索方向只往左或右一个方向,往上或下一个方向,不会出现重复搜索: 在该问题中往 ...

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

  9. HDOJ 1312 (POJ 1979) Red and Black

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

  10. poj 1979 Red and Black(dfs水题)

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

随机推荐

  1. zabbix3.2利用自动发现功能对fastcgi模式的php状态进行集中监控

    zabbix3.2利用自动发现功能对fastcgi模式的php状态进行集中监控 前端nginx虚拟主机引用后端多个php接口,为了方便监控,将后端服务器集中配置在nginx中,具体配置如下: [roo ...

  2. lr 中cookie的解释与用法

    Loadrunner 中 cookie 解释与用法loadrunner 中与 cookie 处理相关的常用函数如下: web_add_cookie(): 添加新的 cookie 或者修改已经存在的 c ...

  3. Gradle 同步时报错,Could not find com.android.support.constraint:constraint-layout:1.0.0-alpha8的解决方法

    Error:Could not find com.android.support.constraint:constraint-layout:1.0.0-alpha8. 原因: SDK 中可能是没有安装 ...

  4. C++中的const总结

    CONST 一.符号常量 声明: const 类型说明符 常量名 = 常量值: const float PI = 3.1415927; //可以交换const与float的位置 符号常量在声明时一定要 ...

  5. 2018-2019-2 网络对抗技术 20165323 Exp6 信息搜集与漏洞扫描

    一.实验内容 二.实验步骤 1.各种搜索技巧的应用 2.DNS IP注册信息的查询 3.基本的扫描技术 主机发现 端口扫描 OS及服务版本探测 具体服务的查点 4.漏洞扫描 三.实验中遇到的问题 四. ...

  6. 获取clock ticks per second

    #include <sys/syscall.h> #include <stdio.h> #include <unistd.h> int main() { print ...

  7. Netty5客户端源码解析

    Netty5客户端源码解析 今天来分析下netty5的客户端源码,示例代码如下: import io.netty.bootstrap.Bootstrap; import io.netty.channe ...

  8. SSM框架搭建后在tomcat部署报错lineNumber: 15; columnNumber: 59; 必须为元素类型 "beans" 声明属性 "xmlns"

    删除applicationContext.xml中的文件头上的这个就可以<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" &q ...

  9. 咸鱼入门到放弃11--Servlet+JSP+JavaBean开发模式

    本篇搬运了大佬blog:https://www.cnblogs.com/xdp-gacl/p/3902537.html 一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servle ...

  10. pycharm 记录

    一.pycharm字体放大.缩小的设置 File —> setting —> Keymap —>在搜寻框中输入:increase —> Increase Font Size(双 ...