Red and Black
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 31722   Accepted: 17298

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

Source

 
 #include "cstdio"
#include "cstring"
#include "cmath"
#include "iostream"
#include "string" using namespace std ;
const int maxN = ; const int dx[ ] = { , , , - } ;
const int dy[ ] = { , , - , } ; char mp[ maxN ][ maxN ] ;
int N , M ; void DFS ( const int xi , const int yi ) {
if ( !mp[ xi ][ yi ] )return ;
mp[ xi ][ yi ] = '%' ;
for ( int i= ; i< ; ++i ) {
int xx = xi + dx[ i ] ;
int yy = yi + dy[ i ] ;
if ( xx > && xx <= M && yy > && yy <= N && mp[ xx ][ yy ] == '.' )
DFS( xx , yy ) ;
}
} int sumerize ( const int n , const int m ) {
int ret ( ) ;
for ( int i= ; i<=n ; ++i ) {
for ( int j= ; j<=m ; ++j ) {
if ( mp[ i ][ j ] == '%' ) ++ret ;
}
}
return ret ;
} int main ( ) {
int start_x , start_y ;
while ( scanf ( "%d%d\n" , &N , &M ) == && N && M ) {
for ( int i= ; i<=M ; ++i ) {
scanf ( "%s" , mp[ i ] + ) ;
}
for ( int i= ; i<=M ; ++i ) {
for ( int j= ; j<=N+ ; ++j ) {
if ( mp[ i ][ j ] == '@' ) {
mp[ i ][ j ] = '.' ;
start_x = i ;
start_y = j ;
goto Loop ;
}
}
}
Loop :
DFS ( start_x , start_y ) ; printf ( "%d\n" , sumerize ( M , N ) ) ;
memset ( mp , , sizeof ( mp ) ) ;
} return ;
}

2016-10-21  16:28:48

POJ 1979 题解的更多相关文章

  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 题解《挑战程序设计竞赛》

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

  3. POJ 1979 Red and Black dfs 难度:0

    http://poj.org/problem?id=1979 #include <cstdio> #include <cstring> using namespace std; ...

  4. POJ 1979 dfs和bfs两种解法

      fengyun@fengyun-server:~/learn/acm/poj$ cat 1979.cpp #include<cstdio> #include<iostream&g ...

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

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

  6. poj 1979 Red and Black(dfs)

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

  7. POJ 1979 DFS

    题目链接:http://poj.org/problem?id=1979 #include<cstring> #include<iostream> using namespace ...

  8. POJ 1979 红与黑

    题目地址: http://poj.org/problem?id=1979  或者  https://vjudge.net/problem/OpenJ_Bailian-2816 Red and Blac ...

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

随机推荐

  1. 转载请注明出处: https://github.com/qiu-deqing/FE-interview

    转载请注明出处: https://github.com/qiu-deqing/FE-interview Table of Contents generated with DocToc FE-inter ...

  2. Linux kernel make 常用选项介绍

    Linux kernel 编译方法大全记录 一.这是一个我自己写的自动make脚本: #!/bin/sh export ARCH=arm export CROSS_COMPILE=arm-linux- ...

  3. Photoshop学习笔记

    视频地址:PhotoshopCS5视频教程 1.打开文件的快捷方式:软件刚启动时,双击绘图区域 2.文件->新建,弹出的新建对话框中,包含了剪切板及纸张的相关信息 3.图像缩放快捷方式:ctrl ...

  4. SpringMVC

    使用注解去完成整个项目 安装spring的一个插件,则相关的提示就会出来

  5. Python3实现火车票查询工具

    Python 实现火车票查询工具 一. 实验介绍 通过python3实现一个简单的命令行版本的火车票查询工具,用实际中的例子会更感兴趣,不管怎么样,既练习了又可以自己使用. 1.  知识点: Pyth ...

  6. I/O 函数总结

    经过一段时间的学习,发现字符处理时或者文件处理时,经常需要进行输入(读入)和输出,而可供选择的函数很多,现在反而容易搞混淆,下面就对常用的7个 输入输出函数 进行总结和比较,以便于区分和熟练掌握. 标 ...

  7. JS学习进阶中 come on!

    1,定义新的属性来扩展对象 新方法:defineProperty() 实例: var data = {}: Object.defineProperty(data,"type",{ ...

  8. jquery mobile

    页面:data-role="page"  header.content.fooder 过渡:data-transition ="slide"  反向过渡:dat ...

  9. 总结一下项目中遇到的分页问题,使用bootstrap-table来做的后台分页,大家可以借鉴一下 (分页第一篇)

    前台进入bootstrap的js和css文件,我就不多少了,另外要引进bootstrap-table的js和css 废话不多说,直接代码.   框架为ssm,代码很清楚 <div class=& ...

  10. Python之实用的IP地址处理模块IPy

    实用的IP地址处理模块IPy 在IP地址规划中,涉及到计算大量的IP地址,包括网段.网络掩码.广播地址.子网数.IP类型等 别担心,Ipy模块拯救你.Ipy模块可以很好的辅助我们高效的完成IP的规划工 ...