题目链接:http://poj.org/problem?id=1979

思路分析:使用DFS解决,与迷宫问题相似;迷宫由于搜索方向只往左或右一个方向,往上或下一个方向,不会出现重复搜索;

在该问题中往四个方向搜索,会重复搜索,所以使用vis表来标记访问过的点,避免重复搜索。

代码如下:

#include <iostream>
using namespace std; const int MAX_N = ;
int vis[MAX_N][MAX_N];
char map[MAX_N][MAX_N];
int red_count, W, H; int Search( int i, int j )
{
if ( i == || i == H +
|| j == || j == W + )
return ;
else
if ( map[i][j] == '#' )
return ;
else
if ( !vis[i][j] )
{
vis[i][j] = ;
red_count++; Search( i-, j );
Search( i+, j );
Search( i, j- );
Search( i, j+ );
} return ;
} int main()
{ while ( scanf( "%d %d\n", &W, &H ) != EOF )
{
int i_start, j_start;
red_count = ; memset( map, , sizeof(map) );
memset( vis, , sizeof(vis) ); if ( W == && H == )
break; for ( int h = ; h <= H; ++h )
for ( int w = ; w <= W; ++w )
{
scanf( "%c", &map[h][w] );
if ( map[h][w] == '@' )
{
i_start = h;
j_start = w;
}
scanf( "\n" );
} Search( i_start, j_start );
cout << red_count << endl;
} return ;
}

poj 1979 Red and Black(dfs)的更多相关文章

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

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

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

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

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

  4. poj 1979 Red and Black(dfs水题)

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

  5. POJ 1979 Red and Black【DFS】

    标准DFS,统计遍历过程中遇到的黑点个数 #include<cstdio> #include<vector> #include<queue> #include< ...

  6. POJ 1979 Red and Black (DFS)

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

  7. POJ 1979 Red and Black (简单dfs)

    题目: 简单dfs,没什么好说的 代码: #include <iostream> using namespace std; typedef long long ll; #define IN ...

  8. poj 1979 Red and Black 题解《挑战程序设计竞赛》

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

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

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

随机推荐

  1. 史上最简单的Hibernate入门简单介绍

    事实上Hibernate本身是个独立的框架,它不须要不论什么web server或application server的支持.然而,大多数的Hibernate入门介绍都加入了非常多非Hibernate ...

  2. Lable 控件 -- 用代码改变要显示字体的颜色

    lable控件怎么改变显示字体的颜色 代码如下: string color = "#B72C34"; this.lbl.ForeColor = System.Drawing.Col ...

  3. Android应用开发基础篇(5)-----Handler与多线程

    链接地址:http://www.cnblogs.com/lknlfy/archive/2012/02/19/2358155.html 一.概述 Handler这个类主要用来发送和处理消息的.它有多个发 ...

  4. Lucence.net索引技术 一

    1.建立索引 为了对文档进行索引,Lucene 提供了五个基础的类,他们分别是 Document, Field, IndexWriter, Analyzer, Directory.下面我们分别介绍一下 ...

  5. 10min系列之二日志可视化进阶

    10min系列之二日志可视化进阶(作者原创,同步发布在github) 本文需要有一定的python和前端基础,如果没基础的,请关注我后续的基础教程系列博客 本文所有的demo,都是浏览器下展示的 原创 ...

  6. perl5 第十三章 Perl的面向对象编程

    第十三章 Perl的面向对象编程 by flamephoenix 一.模块简介二.Perl中的类三.创建类四.构造函数 实例变量 五.方法六.方法的输出七.方法的调用八.重载九.析构函数十.继承十一. ...

  7. kinect for windows - SkeletonBasics-D2D详解之二

    首先看骨骼追踪例子代码的结构:

  8. Windows Azure 成为业内首家被授权为 FedRAMP JAB P-ATO 的供应商

    编辑人员注释:本文章由 Windows Azure 业务和运营部门产品市场营销总监 Sarah Fender 撰写 我们高兴地宣布,Windows Azure 被 FedRAMP 联合授权董事会 (J ...

  9. D - 楼下水题(kmp+Manacher)

    D - 楼下水题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Statu ...

  10. poj1236 Network of Schools【强连通分量(tarjan)缩点】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4316263.html  ---by 墨染之樱花 [题目链接]http://poj.org/pr ...