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<cstdio>
#include<string.h>
int px[]={-,,,};
int py[]={,,-,};
int ans;
bool key[][];
char a[][];
int n,m,x,y;
void f(int x,int y)
{
int nx,ny;
for(int i = ;i < ; i++)
{
nx=x+px[i];
ny=y+py[i];
if(nx >= && ny >= && a[nx][ny] == '.' && key[nx][ny] == false && nx < m && ny < n)
{
ans++;
key[nx][ny]=true;
f(nx,ny);
}
}
}
int main()
{ while(scanf("%d %d",&n,&m) && n && m)
{
memset(key,false,sizeof(key));
for(int i = ; i < m ; i++)
{
getchar();
for(int j = ; j < n ; j++)
{
scanf("%c",&a[i][j]);
if(a[i][j] == '@')
{
x=i;
y=j;
}
} }
key[x][y]=true;
ans=;
f(x,y);
printf("%d\n",ans+);
}
}

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

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

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

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

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

  3. 【POJ - 3984】迷宫问题(dfs)

    -->迷宫问题 Descriptions: 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0 ...

  4. POJ-1979 Red and Black(DFS)

    题目链接:http://poj.org/problem?id=1979 深度优先搜索非递归写法 #include <cstdio> #include <stack> using ...

  5. poj 3009 Curling 2.0 (dfs )

    Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11879   Accepted: 5028 Desc ...

  6. 【POJ - 1321】棋盘问题 (dfs)

    棋盘问题 Descriptions: 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘 ...

  7. 【POJ - 1970】The Game(dfs)

    -->The Game 直接中文 Descriptions: 判断五子棋棋局是否有胜者,有的话输出胜者的棋子类型,并且输出五个棋子中最左上的棋子坐标:没有胜者输出0.棋盘是这样的,如图 Samp ...

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

  9. Poj1979 Red and Black (DFS)

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

随机推荐

  1. TC学习总结

    带宽管理: TC中规定描述带宽: mbps = 1024 kbps = 1024 * 1024 bps => byte/s mbit = 1024 kbit => kilo bit/s m ...

  2. 继续(3n+1)猜想 (25)

    #include <algorithm> #include <iostream> using namespace std; int main(){ ] = { }; ], nu ...

  3. AtCoder Grand Contest 017 F - Zigzag

    题目传送门:https://agc017.contest.atcoder.jp/tasks/agc017_f 题目大意: 找出\(m\)个长度为\(n\)的二进制数,定义两个二进制数的大小关系如下:若 ...

  4. Asp.net WebApi 异常处理解决方案

    一.使用异常筛选器捕获所有异常 我们知道,一般情况下,WebApi作为服务使用,每次客户端发送http请求到我们的WebApi服务里面,服务端得到结果输出response到客户端.这个过程中,一旦服务 ...

  5. Analyzing Polyline CodeForces - 195D

    Analyzing Polyline CodeForces - 195D 题意:有n个函数,第i个函数yi(x)=max(ki*x+bi,0).定义函数s(x)=y1(x)+y2(x)+...+yn( ...

  6. js 获取最后一个字符

    方法一: str.charAt(str.length - 1) 方法二: str.subStr(str.length-1,1) 方法三:    var str = "123456" ...

  7. P1554 梦中的统计

    题目背景 Bessie 处于半梦半醒的状态.过了一会儿,她意识到她在数数,不能入睡. 题目描述 Bessie的大脑反应灵敏,仿佛真实地看到了她数过的一个又一个数.她开始注意每一个数码(0..9):每一 ...

  8. [转]Android专家级别的面试总结

    Android专家级别的面试总结 2017年02月15日 16:56:28 阅读数:1225 1.. 自定义View流程 onMeasure, onLayout, onDraw, 采用深度优先,因为必 ...

  9. STM32 modbus CRC16校验

    typedef unsigned char uint8_t; typedef unsigned short uint16_t; typedef int int32_t; const uint16_t ...

  10. iOS html格式解析

    使用TFHpple解析html https://github.com/topfunky/hpple 前期准备工作 引入静态库文件 添加库文件的 header search paths(注意,必须选中 ...