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
#include<iostream>
using namespace std;
char mapp[][];
int m,n,k=,c=,d=;
void dfs(int x,int y)
{
if(mapp[x][y]=='#'||x<||y<||x>=m||y>=n)
{
return;
}
mapp[x][y]='#';
dfs(x+,y);
dfs(x-,y);
dfs(x,y+);
dfs(x,y-);
}
int main()
{
int i,j,k;
while(cin>>n>>m)
{
c=;
d=;
if(m==&&n==)
break;
for(i=;i<m;i++)
{
for(j=;j<n;j++)
{
cin>>mapp[i][j];
}
}
for(i=;i<m;i++)
for(j=;j<n;j++)
{
if(mapp[i][j]=='#')
c++;
}
k=;
for(i=;i<m;i++)
{
for(j=;j<n;j++)
{
if(mapp[i][j]=='@')
{
mapp[i][j]='.';
dfs(i,j);
}
}
}
for(i=;i<m;i++)
for(j=;j<n;j++)
{
if(mapp[i][j]=='#')
d++;
}
cout<<d-c<<endl;
}
return ;
}

hdu 1312 Red and Black的更多相关文章

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

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

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

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

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

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

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

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

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

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

  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. HDU 1312 Red and Black(经典DFS)

    嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 一道很经典的dfs,设置上下左右四个方向,读入时记下起点,然后跑dfs即可...最后答 ...

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

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

随机推荐

  1. ajax实例1

    前台: function getDetail(index){ $.post("<%=request.getContextPath() %>/member/dbcenter!get ...

  2. MVC学习笔记--跟小静学MVC相关语法特性小补习

    http://www.cnblogs.com/janes/archive/2012/10/15/2721101.html http://www.cnblogs.com/h82258652/p/4795 ...

  3. Android 中this、getContext()、getApplicationContext()、getApplication()、getBaseContext() 之间的区别

    : 知之为知之,不知为不知是知也! 使用this, 说明当前类是context的子类,一般是activity application等; this:代表当前,在Activity当中就是代表当前的Act ...

  4. 【转载】 Python 方法参数 * 和 **

    Python的函数定义中有两种特殊的情况,即出现*,**的形式. 如:def myfun1(username, *keys)或def myfun2(username, **keys)等. 他们与函数有 ...

  5. (C#基础) byte[] 之初始化, 赋值,转换。(转)

    byte[] 之初始化赋值 用for loop 赋值当然是最基本的方法,不过在C#里面还有其他的便捷方法. 1. 创建一个长度为10的byte数组,并且其中每个byte的值为0. byte[] myB ...

  6. Android基本认识

    AndroidManifest.xml file missing! 是因为开始想当然的用中文当project名 no launcher activity found 第一次运行出了点问题,no lau ...

  7. 长按事件jquery mobile

    chat_enlarge.addEventListener('touchend', function(event) { if(fingers == 1){ event.preventDefault() ...

  8. 【criteria CascadeType】级联的不同情况

    使用criteria进行增删改查操作,可能会发生级联删除的情况,例如对员工表进行删除,可能会级联删除掉部门表中的某一条信息[类似这样的情况] 对此,我们可以在实体类中对级联的关系进行管理: 对于cri ...

  9. loadrunner通过C语言实现自定义字符出现次数截取对应字符串

    void lr_custom_string_delim_save(char inputStr[500], char* outputStr, char *delim, int occrNo, int s ...

  10. Android MVP理解

    Android默认采用的是MVC: View:对应于布局文件 Model:业务逻辑和实体模型 Controllor:对应于Activity 但是却存在很多问题: 1.这个View对应于布局文件,其实能 ...