题目链接: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. raphael入门到精通---属性和事件篇

    属性的使用 上一篇文章我们介绍了raphael如何生成基本的图形(元素),对于每个元素来讲,我们可以添加很多的元素(attr) 下面我就来简单的介绍下元素属性的使用(path元素属性我后面单独列出来介 ...

  2. UVa1584 Circular Sequence

    #include <stdio.h>#include <string.h> int less(char* str, size_t len, size_t p, size_t q ...

  3. Integer Inquiry(大数相加)

    Description One of the first users of BIT's new supercomputer was Chip Diller. He extended his explo ...

  4. Problem A: A + B

    Problem A: A + BTime Limit: 1 Sec Memory Limit: 128 MBSubmit: 17 Solved: 10[Submit][Status][Web Boar ...

  5. .htaccess Rewrite apache重写和配置

    首先: 必须要空间支持 Rewrite 以及对站点目录中有 .htaccess 的文件解析,才有效. 如何让空间支持Rewrite 和 .htaccess 的文件解析呢 往下看 第一步:要找到apac ...

  6. wsdl透明解析

    1.逐个分析wsdl文件中的元素: <types>:数据类型定义的容器,一般使用 xml schema类型系统. <message>:通信消息的数据结构的抽象化定义,使用< ...

  7. 调试存储过程时提示ORA-20000: ORU-10027: buffer overflow

    下午的时候在 PL/SQl Developer 10.0.5.1710 上调试壹個存储过程,在调试的时候使用了比较多的 DBMS_OUTPUT.PUT_LINE 作为打印日志的方式,结果没过多久 PL ...

  8. 安装 Rational Rose 启动报错:无法启动此程序,因为计算机中丢失 suite objects.dll

    安装完以后提示找不到 suite objects.dll: 经查找,该 dll 存在: 找不到的原因是,安装程序自动设置在 Path 中的环境变量有误: 把最后的 common 改成 Common: ...

  9. Win32 SecuritySetting

    http://flylib.com/books/en/2.21.1.207/1/ http://blogs.technet.com/b/heyscriptingguy/archive/2011/11/ ...

  10. Angulajs 定时器使用

    在Angulajs的control中,不能利用 window.setInterval和 setTimeout 来操作定时器,因为这样做,虽然设置定时器会成功,但对$scope中的数据进行设置时,不会自 ...