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. redisTemplate注入为空

    springboot2.*集成redis时,redis工具类中的redisTemplate注入后总是为空. 问题代码还原: 1.工具类定义成静态工具类,@Resource注入redisTemplate ...

  2. P1003 我要通过!

    转跳点:

  3. UVA - 10305 Ordering Tasks(拓扑排序)

    题意:给定优先关系进行拓扑排序. 分析:将入度为0的点加入优先队列,并将与之相连的点入度减1,若又有度数为0的点,继续加入优先队列,依次类推. #pragma comment(linker, &quo ...

  4. 指令——pwd

    完整的指令的标准格式:Linux通用的格式 #指令主体(空格) [选项](空格) [操作对象] 一个指令可以包含多个选项,操作对象也可以是多个. 指令pwd: 用法:#pwd(print workin ...

  5. Python练习题3

    1.九九乘法表 li = [1,2,3,4,5,6,7,8,9] for i in li: for j in li: if i >= j: print(i,'*',j,'=',i*j,end=& ...

  6. 洛谷 三月月赛 C

    呵呵呵呵,这个sb题做了好久,然并卵,还是不对. 挖坑++ 然而我感觉我做的对了,偷瞄了一下题解应该没什么问题. 这个题有n个点,n条边,所以是个基环树(我也不知道是不是这个名) 要每个点有联通,就是 ...

  7. 103-PHP定义一个类

    <?php class ren{ //定义人类 } class mao{ //定义猫类 } new ren(); //实例化人类 new mao(); //实例化猫类 new mao(); // ...

  8. 4. Retrieving a mapper(检索映射器)

    Retrieving a mapper(检索映射器) 4.1. The Mappers factory(映射工厂) 可以通过 org.mapstruct.factory.Mappers 类检索映射器实 ...

  9. Docker 网络详解及 pipework 源码解读与实践

    转载自:https://www.infoq.cn/article/docker-network-and-pipework-open-source-explanation-practice/ Docke ...

  10. 将已有微信小程序转换为多端应用

    文档地址 https://nervjs.github.io/taro/