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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1014 //hdu1014 0ms #include<stdio.h> #include&l ...

  2. VS2010 自动跳过代码现象

    今日,发现在Release的调试模式下,VS2010会“莫名其妙”的跳过一些代码不执行. 经实验,初步判断可能的原因:预处理器指令或编译器/连接器优化. 20150703更新: 部分代码: m_pTa ...

  3. 解决postgresql -- ERROR: 42601: query has no destination for result data

    I am learning Npgsql and PostgreSQL. I am unable to define the output parameter correctly. What am I ...

  4. Datagard產生gap

    本文轉載自無雙的小寶的博客:http://www.cnblogs.com/sopost/archive/2010/09/11/2190085.html 有時候因為網路或備份故障等原因,主機所產生的歸檔 ...

  5. Zero Copy 简介

    转自:http://blog.csdn.net/zzz_781111/article/details/7534649 许多web应用都会向用户提供大量的静态内容,这意味着有很多data从硬盘读出之后, ...

  6. Linux下配置OpenCV1.0环境

    自己一直嚷嚷着打算学学图像识别,识别个简单的,车牌号,验证码之类的,之前查过资料,OpenCV可以实现.昨天花了一个下午终于配置好环境了,今天写下总结. OpenCV这一名称包含了Open和Compu ...

  7. cocoaPads 安装及出现Analyzing dependencies之后卡死解决方案

    1.安装 a. 查看源 gem sources -l b. 设置源: sudo gem sources -a http://ruby.taobao.org c. 删除源:sudo gem source ...

  8. 《大话》之 装饰模式 Vs 建造者模式

    一.简介: 装饰模式:     背景:小菜要见美女娇娇,感慨自己不会着装,怕给娇娇留下坏印象               内容:动态的给一个对象添加一些额外职责               图文并茂: ...

  9. 多个$(document).ready()函数的执行顺序问题,(未解决)

    今天遇到了一个问题: jQuery获取不了动态添加的元素,我使用的是append添加的.寻求了帮助,得到解决方案: 在文件开头写上这样一段代码来获取,写在$(document).ready()里面. ...

  10. POJ 1840 HASH

    题目链接:http://poj.org/problem?id=1840 题意:公式a1x1^3+ a2x2^3+ a3x3^3+ a4x4^3+ a5x5^3=0,现在给定a1~a5,求有多少个(x1 ...