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. HTTP协议----ETag

    Etag是URL的Entity Tag,用于标示URL对象是否改变,区分不同语言和Session等等.具体内部含义是服务器控制的,就像Cookie那样. 服务器首先产生ETag,服务器可在稍后使用它来 ...

  2. 实际开发中,后台回传的错误格式的Json数据处理

    现在很多学习刚学习移动开发的同学,相信在培训机构,拿到后台数据的时候,格式都是正确的,甚至有的还是plist文件.但是实际开发中,我们获取数据都是通过网络接口从服务器获取数据,这些数据的格式都是后台写 ...

  3. jQuery的css

    1.css(name|pro|[,val|fn]) 访问匹配元素的样式属性. 参数name 描述: 取得第一个段落的color样式属性的值. $("p").css("co ...

  4. dev gridcontrol 绑定int型及日期型的列默认当值为0时显示空白及格式化日期显示方式

    xmlns:sys="clr-namespace:System;assembly=mscorlib" 如只显示日期的时间部分 <dxg:GridColumn Header=& ...

  5. 关于在框架中使用curl的思考,以及,curl其实很好用

    初步猜想: 在接触到框架文档的第一阶段时,会觉得控制器调用模型就是一件很简单的事,tp中用D方法或者M方法来实例化模型,laravel中用命名空间来加载模型,CI中用$this->load-&g ...

  6. radio里面value值与其他字符进行比较

    本题旨在创建一个具有及时反馈的选择题,当选完其中一个选项后,会有弹窗来提醒你选择的答案是正确还是错误的.<div id="text" style="display: ...

  7. 如何通过fpmmm和zabbix来监控客户机上MariaDB数据库运行情况

    首先在客户机安装MariaDB和zabbix,参考上一篇 安装fpmmm的过程主要参考[1]. 安装fpmmm的依赖 shell> yum install php-cli php-process ...

  8. php中switch语句case后表达式写法记录一

    可作等级评价: $var = 95; switch(true){ case $var < 100; $level = 1; break; case $var < 95; $level = ...

  9. 分享基于.NET MVC+EF CodeFirst+IOC+EasyUI的框架设计

    **注:要做工,没什么时间,等有空时会上传到GIT,项目结构如上,简单的说一下: **支持IOC及多数据库等,各项目由MVC区域隔离: 主要使用基于接口与抽象类进行高度的抽象与接口隔离,与其它框架比较 ...

  10. php dday1... web服务器的搭建 数据库的安装....