Red and Black
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 26058   Accepted: 14139

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) 

The end of the input is indicated by a line consisting of two zeros. 

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>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int row,col,sum;
char value[30][30];
int flag[30][30]; void dfs(int x,int y)
{
flag[x][y]=1; if(x>1&&flag[x-1][y]==0&&value[x-1][y]=='.')
{
dfs(x-1,y);
}
if(y>1&&flag[x][y-1]==0&&value[x][y-1]=='.')
{
dfs(x,y-1);
}
if(x<row&&flag[x+1][y]==0&&value[x+1][y]=='.')
{
dfs(x+1,y);
}
if(y<col&&flag[x][y+1]==0&&value[x][y+1]=='.')
{
dfs(x,y+1);
}
} void solve1()
{
int i,j;
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
if(value[i][j]=='@')
{
dfs(i,j);
return;
}
}
}
} void solve2()
{
int i,j;
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
if(flag[i][j])
{
sum++;
}
}
}
} int main()
{
int i,j;
while(cin>>col>>row)
{
if(col+row==0)
break;
memset(flag,0,sizeof(flag));
sum=0;
for(i=1;i<=row;i++)
{
cin>>value[i]+1;
}
solve1();
solve2(); cout<<sum<<endl;
} return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1979:Red and Black的更多相关文章

  1. 【POJ - 1979 】Red and Black(dfs+染色)

    -->Red and Black Descriptions: 有个铺满方形瓷砖的矩形房间,每块瓷砖的颜色非红即黑.某人在一块砖上,他可以移动到相邻的四块砖上.但他只能走黑砖,不能走红砖. 敲个程 ...

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

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

  3. 转:Red Hat JBoss团队发布WildFly 8,全面支持Java EE 7并包含全新的嵌入式Web服务器

    原文来自于:http://www.infoq.com/cn/news/2014/02/wildfly8-launch Red Hat的JBoss部门今天宣布WildFly 8正式发布.其前身是JBos ...

  4. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  5. POJ 3252:Round Numbers

    POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...

  6. OpenJudge/Poj 1979 Red and Black / OpenJudge 2816 红与黑

    1.链接地址: http://bailian.openjudge.cn/practice/1979 http://poj.org/problem?id=1979 2.题目: 总时间限制: 1000ms ...

  7. poj 1979 Red and Black(dfs)

    题目链接:http://poj.org/problem?id=1979 思路分析:使用DFS解决,与迷宫问题相似:迷宫由于搜索方向只往左或右一个方向,往上或下一个方向,不会出现重复搜索: 在该问题中往 ...

  8. POJ 1979 Red and Black (zoj 2165) DFS

    传送门: poj:http://poj.org/problem?id=1979 zoj:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...

  9. poj 1979 Red and Black 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=1979 Description There is a rectangular room, covered with square tiles ...

随机推荐

  1. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-user

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  2. 使用Def文件导出Dll文件

    模块定义 (.def) 文件是包含一个或多个描述 DLL 各种属性的 Module 语句的文本文件.如果不使用 __declspec(dllexport) 关键字导出 DLL 的函数,则 DLL 需要 ...

  3. bzoj 2451 Uyuw's Concert

    裸的半平面交.感觉这些东西,纯属在考代码能力啊.. #include<cstdio> #include<algorithm> #include<cmath> #de ...

  4. vuejs+thinkphp5+phpsocketIO+timer数据及时更新

    1.安装thinkphp5.0以上版本包含workerman框架2.composer安装composer require workerman/phpsocket.io3.vue中调用需要加载weapp ...

  5. Python 比较 相等性 真值

    1 == 操作符测试 值 的相等性: is 测试对象的一致性.注意短字符串的is相等性测试,PVM会缓存短字符串,s1 is s2 将返回true. 2 false:"", [], ...

  6. C++ 把数组的元素乘以2在输出

    #include<iostream> using namespace std; int main(){ ] = { , , , , }; ; ; i < ; i++) { ) { s ...

  7. mysql安装--window版

    一.下载 二.解压 三.配置 四.环境变量 五.安装MySQL服务 六.启动MySQL服务 七.停止MySQL 一.下载 第一步:打开网址,https://www.mysql.com,点击downlo ...

  8. 学习如何在maven建立一个javaweb环境

    https://blog.csdn.net/MaNongXf/article/details/83418353 这个写的真的清楚认真.

  9. P 1024 科学计数法

    转跳点:

  10. ZOJ - 3961 Let's Chat(区间相交)

    题意:给定一个长度为n的序列,A和B两人分别给定一些按递增顺序排列的区间,区间个数分别为x和y,问被A和B同时给定的区间中长度为m的子区间个数. 分析: 1.1 ≤ n ≤ 109,而1 ≤x, y  ...