Red and Black
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 47466   Accepted: 25523

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

 
神坑的是:xy方向需要换一下
 
 #include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
char a[][];
int n,m;
int res=;
int dx[]={,-,,};
int dy[]={,,,-};
void dfs(int x,int y)
{
res++;
a[x][y]='#';
for(int i=;i<;i++){
int nx=x+dx[i],ny=y+dy[i];
if(nx>=&&nx<n&&ny>=&&ny<m&&a[nx][ny]=='.'){
dfs(nx,ny);
}
}
return ;
}
void solve()
{
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(a[i][j]=='@'){
dfs(i,j);
}
}
}
}
int main()
{
while(cin>>m>>n&&n!=&&m!=){ res=;
for(int i=;i<n;i++){
for(int j=;j<m;j++){
cin>>a[i][j];
}
}
solve();
cout<<res<<endl;
}
return ;
}

脱离参考书自己再根据自己的理解过一遍:

 #include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
using namespace std;
int n,m;
char a[][];
int sx,sy,nx,ny;
int dx[]={,,,-};
int dy[]={,-,,};
int res;
void dfs(int x,int y)
{
res++;
a[x][y]='#';
for(int i=;i<;i++){
nx=x+dx[i],ny=y+dy[i];
if(nx>=&&nx<m&&ny>=&&ny<n&&a[nx][ny]=='.'){
dfs(nx,ny);
}
}
}
int main()
{
while(cin>>n>>m&&(n&&m)){
for(int i=;i<m;i++){
for(int j=;j<n;j++){
cin>>a[i][j];
if(a[i][j]=='@'){
sx=i,sy=j;
}
}
}
res=;
dfs(sx,sy);
cout<<res<<endl;
}
return ;
}

Poj1979 Red and Black (DFS)的更多相关文章

  1. POJ-1979 Red and Black(DFS)

    题目链接:http://poj.org/problem?id=1979 深度优先搜索非递归写法 #include <cstdio> #include <stack> using ...

  2. HDU 1312 Red and Black (dfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...

  3. HDU1312 Red and Black(DFS) 2016-07-24 13:49 64人阅读 评论(0) 收藏

    Red and Black Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

  4. poj-1979 red and black(搜索)

    Time limit1000 ms Memory limit30000 kB There is a rectangular room, covered with square tiles. Each ...

  5. Red and Black---hdu1312(dfs)

    2015-04-07http://acm.hdu.edu.cn/showproblem.php?pid=1312 Sample Input 6 9....#......#............... ...

  6. POJ 1979 Red and Black (DFS)

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

  7. HDU1312 / POJ1979 / ZOJ2165 Red and Black(红与黑) 解题报告

    题目链接:pid=1312" target="_blank">HDU1312 / POJ1979 / ZOJ2165 Red and Black(红与黑) Red ...

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

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

  9. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

随机推荐

  1. python处理文件的换行符

    我们知道在Windows平台下的换行符是\r\n,而在linux下的换行符是\n.现在写一个简单程序来测试python是如何处理这些换行符的. 准备文件data.txt,该文件在Windows平台下编 ...

  2. Debian/Ubuntu pip default install to $HOME/.local

    pip default install to $HOME/.local on Debian/Ubuntu After pip 8.1.1-2 on Debian or Ubuntu you can p ...

  3. Jmeter4.X - 使用本身自带的脚本录制功能录制脚本

    1.前言 记录对Jmeter评估研究的过程,本文记录使用apache网站提供的原Jmeter使用自带功能进行脚本录制. 本文可用于面向B/S WEB应用测试的工程师熟悉Jmeter使用.章节安排按照脚 ...

  4. 【HTML5】video 标签禁用自带的下载按钮

    h5自带的 video标签 ,会有个下载按钮,有时候想避免,就可以利用样式的方法 <style type="text/css"> video::-internal-me ...

  5. HTTPS 通讯流程

    原文地址 https://blog.csdn.net/wangweilica6/article/details/50171457 一.简介 前一篇文章,我总结了下,如何部署https服务,开通ssl通 ...

  6. sendmail邮件自动发送

    配置邮件自动发送: 1.安装软件 yum -y install sendmail mailx 2.发送邮件的邮箱授权 eg:y******@126.com 网页网易云邮箱登陆 --> 设置 -- ...

  7. TestNG 框架的运用

    TestNG这个测试框架可以很好的和基于Selenium的web自动化测试结合在一起,实现把我们写好的自动化测试用例以自定义顺序执行.下面分为十二步来对TestNG测试框架进行总结,包括环境的部署,从 ...

  8. 20.react库 入门

    vue插件: 使用方式:Vue.use(插件名称); {}/function 1.对象 export default { install(Vue,options){ } } 2.函数 export d ...

  9. 有关apk打包的东西最近正在整理

    下周将会呈现给大家完整的一套打包流程. {‘敬请期待’,}

  10. 剑指offer——python【第49题】把字符串转换成整数

    题目描述 将一个字符串转换成一个整数(实现Integer.valueOf(string)的功能,但是string不符合数字要求时返回0),要求不能使用字符串转换整数的库函数. 数值为0或者字符串不是一 ...