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. springmv返回JSON数据格式

    1.先导入依赖 <!-- springmvc使用@responseBody start--> <dependency> <groupId>com.fasterxml ...

  2. tab选项卡,不带自动切换定时器

    <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content ...

  3. es6 中的 Promise

    var promise = new Promise( function( resolve, reject ){             function onServiceSuccess( data ...

  4. 基于springboot实现Java阿里短信发送

    1.接口TestController import java.util.Random; import com.aliyuncs.DefaultAcsClient; import com.aliyunc ...

  5. dede:list 与 dede:arclist 的区别

    1.{dede:list}是用于列表页的文章列表调用,通常是用于list_article.htm页面,这个文章列表是可以分页的. 功能说明:表示列表模板里的分页内容列表适用范围:仅列表模板 list_ ...

  6. maven手动安装ojdbc6.jar包到本地仓库

    需要jar文件 ojdbc6.jar jar下载地址1   下载地址2 本地执行: mvn install:install-file -Dfile=D:/ojdbc6.jar -DgroupId=co ...

  7. 149-PHP大小写转换函数

    <?php $str='PHP is a very good programming language.'; //定义一个字符串 echo "未经处理的字符串:<br /> ...

  8. 8. Redis 持久化对生产环境的灾难恢复的意义

    1.故障发生的时候会怎么样2.如何应对故障的发生 很多同学,自己也看过一些redis的资料和书籍,当然可能也看过一些redis视频课程 所有的资料,其实都会讲解redis持久化,但是有个问题,我到目前 ...

  9. js基础学习之-js包装对象

    var test = "test"; test.a = "hello"; console.log(test.a); //undifined 定义: 在JavaS ...

  10. Ubuntu 安装phpmyadmin (9.17第六天)

    PhpMyAdmin 是一个用 PHP 编写的软件工具,可以通过 web方式控制和操作 MySQL 数据库.通过 phpMyAdmin 可以完全对数据库进行操作,例如建立.复制和删除数据等等,这样 M ...