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. oracle 视图的创建,游标,left join

    视图的创建: create or replace view dmv_mat_contract_stock_in_bill as select csib.*, sib.STOCK_IO_, sib.CO ...

  2. a标签产生间隙,<a> 包裹 <img> 产生 4px 间隙

    图片文字等inline元素默认是和父级元素的baseline对齐的,而baseline又和父级底边有一定距离(这个距离和 font-size,font-family 相关),所以设置 vertical ...

  3. input事件与change事件

    输入框的change事件: 必须等到输入框失去焦点的时候才会触发,鼠标在空白的地方点一下: 输入框的input事件: 在输入内容变化的同时,实时的触发,不需要等到失去焦点.

  4. Best Time to Buy and Sell Stock1,2,3,4

    找到最低值和最高值 int maxProfit(vector<int>& prices) { ); ; ]; ;i<prices.size();i++) { profit=m ...

  5. SAP SMARTFORM 记录实际打印次数

    http://blog.csdn.net/wangjolly/article/details/8334008

  6. JSP复习整理(三)基本语法续

    查看注册后的信息: TestBean.java package com.hai.xaio.cn; public class TestBean { public String userName; pub ...

  7. 一篇很好的Java、C、PHP、前端、Android、IOS的文章

    http://www.cctime.com/html/2016-11-8/1238265.htm 很好的讲了这些技术的学习路线,其中的文档资料很丰富,值得学习参考

  8. C# extended toolkit propertygrid 隐藏部分属性

    该方法是通过更改ShowDetail的状态来实现的隐藏属性 首先在引用里右键 管理NuGet程序包 查找extended wpf toolkit 并安装 然后在MainWindow.xaml添加引用 ...

  9. Visual Studio将std::cout输出到Output窗口

    在debug的时候,输出到Output需要使用OutputDebugString函数,但部分库的log是采用std::cout输出的,需要用控制台(黑窗)程序来查看输出.有没有一种使用GUI和Outp ...

  10. php五种常见的设计模式(转载)

    很多人都想着写博客来记录编程生活中的点滴,我也不例外,但想了好长时间不知道写什么........万事开头难,先转载一篇吧..... 设计模式 一书将设计模式引入软件社区,该书的作者是 Erich Ga ...