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
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
 
Sample Output
45
59
6
13
要小心它的输入
行列是相反的
坑在这里找了好多次TAT
 #include<stdio.h>
#include<iostream>
using namespace std;
char map[][];
int n,m;
int num;
int rx[]={,,,-};
int ry[]={,-,,};
void dfs(int i,int j)
{
map[i][j]='#';
num++;
int t,x,y;
for(t=;t<;t++)
{
x=i+rx[t];
y=j+ry[t];
if(x>= && x<n && y>= && y<m && map[x][y]=='.')
dfs(x,y);
}
}
int main()
{
int i,j;
int si,sj;
while(scanf("%d%d",&m,&n)!=EOF)//原来是行数和列数反了...我真是醉了....
{ if(m==&&n==)break;
for(i=;i<n;i++)
for(j=;j<m;j++)
{
cin>>map[i][j];
if(map[i][j]=='@')
{
si=i;
sj=j;
}
}
num=;
dfs(si,sj);
printf("%d\n",num);
}
return ;
}

HDU 1312 Red and Black (DFS)的更多相关文章

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

  2. HDU 1312 Red and Black (DFS & BFS)

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:有一间矩形房屋,地上铺了红.黑两种颜色的方形瓷砖.你站在其中一块黑色的瓷砖上,只能向相 ...

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

  4. HDU 1312 Red and Black --- 入门搜索 DFS解法

    HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...

  5. HDU 1312:Red and Black(DFS搜索)

      HDU 1312:Red and Black Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & ...

  6. HDU 1312 Red and Black --- 入门搜索 BFS解法

    HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...

  7. HDU 1312 Red and Black (dfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...

  8. HDU 1312 Red and Black(bfs,dfs均可,个人倾向bfs)

    题目代号:HDU 1312 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/100 ...

  9. HDU 1312 Red and Black(最简单也是最经典的搜索)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...

随机推荐

  1. iOS -OC调用js页面

    我们这边和h5商量好传递一个结构体参数过去,然后由于解析的问题,导致我这里传递的参数,到h5那边不执行那边的方法 -(void)loginCallBack { NSDictionary *dic; u ...

  2. JavaScript中String.prototype.replace() 方法的使用

    摘抄于:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/replace ...

  3. nginx服务器上遇到了acces denied,报错是fastCGI只要好好修改配置就行了

    猜想: 懵逼 实践: 首先通用的方法,并没有解决问题. 1.修改php-fpm配置文件vim /etc/php-fpm.d/www.confuser = nginx        编辑用户为nginx ...

  4. python爬虫框架scrapy初试(二)

    将该导航网站搜索出结果的页面http://www.dmoz.org/Computers/Programming/Languages/Python/Books/里面标题,及标题的超链接和描述爬下来. 使 ...

  5. 双数组trie树的基本构造及简单优化

    一 基本构造 Trie树是搜索树的一种,来自英文单词"Retrieval"的简写,可以建立有效的数据检索组织结构,是中文匹配分词算法中词典的一种常见实现.它本质上是一个确定的有限状 ...

  6. 摘抄来自论坛的一些DDD讨论

    先说说之前几次DDD项目失败的案例,其实也不能算是失败,只是没有领会DDD的思想. 之前的DDD是建立在数据层之上的,首先是每张数据表对应一个数据实体,每个数据实体由泛型的DAO管理,DAO又被数据上 ...

  7. CoreJavaE10V1P3.7 第3章 Java的基本编程结构-3.7 输入输出(Input ,Output)

    3.7.1 读取输入 Scanner in = new Scanner(System.in); System.out.print("What is your name? "); S ...

  8. svn用Cornerstone上传项目丢失静态库.a问题的解决

    最近电脑重装系统后,装上Cornerstone上传项目后,发现又有丢失.a文件的问题,这里说一下解决办法,打开Preferences-->Subversion-->General,将Use ...

  9. POJ 3139 Balancing the Scale

    枚举. 看了这个方法:$http://www.cppblog.com/shiming413/archive/2008/12/21/29671.html$ 将数字归类的地方不能用$vector$,会超时 ...

  10. 初遇locust

    大概有四个月没有用过PYTHON的我. 今天差点都不知道怎么运行了. 说起来真是丢人呐. 幸好还是存留着一点点印象,再加上看了一下以前写的几篇文章, 还是比较快的想起来了.不然真的是要崩溃了. 刚开始 ...