题目链接: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. Auto-Layout 的各种坑Unable to create description in descriptionForLayoutAttribute_layoutItem_coefficient. Something is nil'

    我们的很多人现在都在使用autolayout,用着也是非常爽但是有了这个东西以后更爽 很省事,什么都不用自己搞.Xcode完全搞定了,但是我终于为自己的懒惰付出了代价,再iphone4怎么运行怎么cr ...

  2. SQL Server 取前一天的0点和23点59分59秒

    DECLARE @startDate1 DATE; DECLARE @startDate DATETIME; ,@startDate1); ,CONVERT(DATETIME,@startDate1) ...

  3. 浏览器 窗口 scrollTop 的兼容性问题

    window.pageYOffset 被所有浏览器支持除了 IE 6, IE 7, IE 8, 不关doctype的事, 注IE9 开始支持此属性. window.scrollY 被Firefox, ...

  4. openGL 旋转的图形 矩阵操作

    #include <windows.h> #ifdef __APPLE__ #include <GLUT/glut.h> #else #include <GL/glut. ...

  5. 手机测试Android程序

    手机测试Android程序   上传者:sanpi329     我也要“分享赚钱” 2014/7/9 关注(23) 评论(0)   声明:此内容仅代表网友个人经验或观点,不代表本网站立场和观点.   ...

  6. thinkphp phpexcel导入

    上次做了一个基于tp3.2.3的phpexcel导出,这次是phpexcel导入,准备材料phpexcel(不知道下载地址的查看我上一篇博文),虽说是基于thinkphp3.2.3来的,也只不过是引入 ...

  7. thinkphp phpexcel导出

    近期做一个项目涉及到商品信息的批量导出与导入,遂记录了下来,框架是tp框架3.2.3(tp5.0性质是一样的,无非是加载方法与所放目录不一样罢了),运用的是phpexcel,闲话不多说,上代码 1.首 ...

  8. 一种CentOS图形界面的修复方法

    刚跳槽来这个公司,第一个任务是一块PCIE8120卡的应用开发.尼玛,别人来培训过.演示过的,现在居然没一个人能把别人演示的东西演示给我看!只好自己折腾去了.把服务器搬到自己旁边空位方便折腾,结果发现 ...

  9. Java 多线程 socket 取款例子 runnable callable

    socket部分参考 http://blog.csdn.net/kongxx/article/details/7259465 取款部分参考 http://blog.csdn.net/dayday198 ...

  10. 前端笔试题 JS部分

    题目 http://www.itmian4.com/forum.php?mod=viewthread&tid=4540 http://www.itmian4.com/forum.php?mod ...