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)

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

  1. Sample Input
  2. 6 9
  3. ....#.
  4. .....#
  5. ......
  6. ......
  7. ......
  8. ......
  9. ......
  10. #@...#
  11. .#..#.
  12. 11 9
  13. .#.........
  14. .#.#######.
  15. .#.#.....#.
  16. .#.#.###.#.
  17. .#.#..@#.#.
  18. .#.#####.#.
  19. .#.......#.
  20. .#########.
  21. ...........
  22. 11 6
  23. ..#..#..#..
  24. ..#..#..#..
  25. ..#..#..###
  26. ..#..#..#@.
  27. ..#..#..#..
  28. ..#..#..#..
  29. 7 7
  30. ..#.#..
  31. ..#.#..
  32. ###.###
  33. ...@...
  34. ###.###
  35. ..#.#..
  36. ..#.#..
  37. 0 0
  38. Sample Output
  39. 45
  40. 59
  41. 6
  42. 13
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. char d[30][30];
  6. bool vis[30][30];
  7. int str[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
  8. int n,m,s;
  9. using namespace std;
  10. void dfs(int x,int y){
  11. for(int i=0;i<4;i++){
  12. int xx=x+str[i][0];
  13. int yy=y+str[i][1];
  14. if(xx>=0&&yy>=0&&xx<m&&yy<n&&d[xx][yy]=='.'&&vis[xx][yy]==0){
  15. s++;
  16. // printf("%d\n",s);
  17. vis[xx][yy]=1;
  18. dfs(xx,yy);
  19. }
  20. if(i==3)
  21. return ;
  22. }
  23. }
  24. int main()
  25. {
  26. int a,b;
  27. while(~scanf("%d%d",&n,&m)&&(n||m)){
  28. for(int i=0;i<m;i++){
  29. scanf("%s",&d[i]);
  30. for(int j=0;j<n;j++){
  31. if(d[i][j]=='@'){
  32. a=i;
  33. b=j;
  34. }
  35. }
  36. }
  37. memset(vis,0,sizeof(vis));
  38. s=1;
  39. d[a][b]='#';
  40. dfs(a,b);
  41. printf("%d\n",s);
  42. }
  43. return 0;
  44. }

HDOJ 1312 (POJ 1979) Red and Black的更多相关文章

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

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

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

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

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

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

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

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

  5. poj 1979 Red and Black(dfs)

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

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

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

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

  8. POJ 1979 Red and Black (DFS)

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

  9. POJ 1979 Red and Black 四方向棋盘搜索

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

随机推荐

  1. 浅谈iOS开发的协议(protocol)和代理(delegate)

    协议和代理对于一个新手来说确实不讨好理解,也有很多的iOS开发的老手对此是懂非懂的.网上的很多博文只是讲了怎么使用,并没有说的很明白.下面我谈一下我的理解. 1.你要先搞明白,协议和代理为什么会出现, ...

  2. React学习笔记(一) 基础知识

    现在最热门的前端框架有AngularJS.React.Bootstrap等.自从接触了ReactJS,ReactJs的虚拟DOM(Virtual DOM)和组件化的开发深深的吸引了我. React的基 ...

  3. Java 设计模式_复合模式(2016-08-31)

    一.什么是复合模式? 在形式上,复合模式确实是多个模式的组合,但满足了这一条并不一定是复合模式,注意它的定义: 将多个模式结合起来形成一个“框架”,以解决一般性问题 一提到“框架”,可能最容易联想到的 ...

  4. EDM推送

    一.需求描述:        日前,做了一个发送客户账单的功能,邮件模板采用自定义,生成vm文件,保存至redis,    采用jodd-mail发送邮件,查询用户账单数据,账单明细,缓存加载模板并渲 ...

  5. mysql主要应用场景 转载

    前言 据说目前MySQL用户已经达千万级别了,其中不乏企业级用户.可以说是目前最为流行的开源数据库管理系统软件了.任何产品都不可能是万能的,也不可能适用于所有的应用场景.那么MySQL到底在什么场景下 ...

  6. python细节

    1.assert 断言语句,可判断一句话真假,若为假会抛出AssertionError. eg. assert 1==1     assert 1==2则AssertionError 2.单引号双引号 ...

  7. PHP实现简易的模板引擎

    PHP实现简易的模板引擎 1.MVC简介 MVC 是一种使用 MVC(Model View Controller 模型-视图-控制器)设计创建 Web 应用程序的模式(详情自己百度): 1. Mode ...

  8. JavaScript学习心得(八)

    Cookie是Netscape发明的技术,是动态网站必不可少的部分,用于浏览器请求Web页面的超文本传输协议是一种无状态的协议. 两种方法维护状态:使用会话(session)(使用服务器技术实现,数据 ...

  9. [python]随机数

    import random()testlist = [1,3,4,5]a,b = 1,5random().random()() 生成0至1之间的随机浮点数,结果大于等于0.0,小于1.0random. ...

  10. linux创建交换分区

    一.SWAP 概述 当系统的物理内存不够用的时候,就需要将物理内存中的一部分空间释放出来,以供当前运行的程序使用.那些被释放的空间可能来自一些很长时间没有什么操作的程序,这些被释放的空间被临时保存到S ...