poj 1979 Red and Black(dfs)
题目链接: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)的更多相关文章
- POJ 1979 Red and Black dfs 难度:0
http://poj.org/problem?id=1979 #include <cstdio> #include <cstring> using namespace std; ...
- POJ 1979 Red and Black (红与黑)
POJ 1979 Red and Black (红与黑) Time Limit: 1000MS Memory Limit: 30000K Description 题目描述 There is a ...
- 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 ...
- poj 1979 Red and Black(dfs水题)
Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...
- POJ 1979 Red and Black【DFS】
标准DFS,统计遍历过程中遇到的黑点个数 #include<cstdio> #include<vector> #include<queue> #include< ...
- POJ 1979 Red and Black (DFS)
Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...
- POJ 1979 Red and Black (简单dfs)
题目: 简单dfs,没什么好说的 代码: #include <iostream> using namespace std; typedef long long ll; #define IN ...
- poj 1979 Red and Black 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=1979 Description There is a rectangular room, covered with square tiles ...
- OpenJudge/Poj 1979 Red and Black / OpenJudge 2816 红与黑
1.链接地址: http://bailian.openjudge.cn/practice/1979 http://poj.org/problem?id=1979 2.题目: 总时间限制: 1000ms ...
随机推荐
- c# 遍历文件夹及其所有文件
利用VS创建一个winform应用程序,遍历指定文件夹(photos)内的所有文件夹及其文件.具体程序如下: namespace 遍历文件夹及其所有文件 { public partial class ...
- 介绍 - OC中的代理模式
一,代理设计模式的场合: 当对象A发生了一些行为,想告知对象B (让对象B成为对象A的代理对象) 对象B想监听对象A的一些行为 (让对象B成为对象A的代理对象) 当对象A无法处理某些行为的时候,想让对 ...
- Android应用开发提高篇(3)-----传感器(Sensor)编程
链接地址:http://www.cnblogs.com/lknlfy/archive/2012/02/29/2373420.html 一.概述 Android支持的传感器种类越来越多了,这确实是一件可 ...
- poj1084Square Destroyer(LDX解重复覆盖)
题目请戳这里 题目大意:给一个n*n的用单位长度的木棍拼起来的网格图,给每个木棍按图示编号,编号范围1~2*n*(n+1).现在已知图中已经去掉了k个木棍,求还要至少去掉几根木棍能使网格图中不存在正方 ...
- bzoj 1040: [ZJOI2008]骑士 树形dp
题目链接 1040: [ZJOI2008]骑士 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3054 Solved: 1162[Submit][S ...
- 10min系列之二日志可视化进阶
10min系列之二日志可视化进阶(作者原创,同步发布在github) 本文需要有一定的python和前端基础,如果没基础的,请关注我后续的基础教程系列博客 本文所有的demo,都是浏览器下展示的 原创 ...
- step_by_step_G+入门-在线服务
第一步:先大概介绍下我们的窗体的布局框架,窗体大体分为以下3大块: 顶部:也就是大的模块划分(比如首页,软件管家,在线服务等) 内容区域:根据选择的不同的顶部模块,进行不同的内容展示: 底部:设置,下 ...
- 一,PHP 语法
基本的 PHP 语法 PHP 的脚本块以 <?php 开始,以 ?> 结束.您可以把 PHP 的脚本块放置在文档中的任何位置. 当然,在支持简写的服务器上,您可以使用 <? 和 ?& ...
- HDU2577:How to Type(DP)
Problem Description Pirates have finished developing the typing software. He called Cathy to test hi ...
- Qt5 FOR WINCE7, Visual Studio 2008环境的搭建
Qt5 FOR WINCE7, Visual Studio 2008环境的搭建 Qt5发布时,试过配置Qt5 for wince的环境,原因是暂时不支持WINCE.前几天意外发现官方博客说明已经开始支 ...