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 ...
随机推荐
- Docker之dockerfile
一.什么是dockerfile Docker通过对于在dockerfile中的一系列指令的顺序解析实现自动的Image的构建: 通过使用build命令,根据dockerfile的描述来构建镜像: bu ...
- 1.语言概述-JavaScript权威指南笔记
上周三的时候交给老板目前的项目第一个迭代回顾会的总结.原本是以综述性的表述方式写的,交给他之后表示程序员不要长篇大论.总结要分为优点缺点期望等等块,每块列出条目,简明扼要的表达出来.这里也用这种风格. ...
- PowerDesigner 基础使用
建表使用基础 1.打开软件,点击create model(如下图一)or 右上角 文件→建立新模型 or 快捷键 Ctrl+N(如下图二) 2.选择要生成脚本的数据库类型(见上图二) 3.建表(图三) ...
- 今天碰到的angular 中的一个小坑
最近在自个儿研究angular,在写一个demo的时候总是有问题,最后发现居然是大小写的问题,卧槽 特tm的坑爹了,代码如下: <!DOCTYPE html> <html lang= ...
- WINFORM Tootip使用小结
toolTip1.Active = true; //激活工具提示,只有激活才会显示提示 toolTip1.IsBalloon = true; //toolTip以气泡形式出现 toolTip ...
- iOS实践03
主要目标:版本新特性界面,新浪授权界面(登录界面)的处理 任务基本完成了,基本的框架也就到这了,接下来的应该是首页获取微博了. 1.版本新特性,可以单独作为一个model,写完之加入到项目中.我们新建 ...
- vector中resize和reserve的区别,代码验证
#include <vector> using namespace std; int main() { vector<int> resizeVect; vector<in ...
- 【Cocos2D-x 3.5实战】坦克大战(2)游戏开始界面
关于游戏的素材都是在网上到处搜集到的,然后自己再用二流的ps技术修修改改的,所以有可能混在一起有点不搭调(没有办法啊,没有美工Orz.. 项目已经建立好了,然后我们需要把我们下载的素材放到Resour ...
- 关于yield创建协程的理解
先上利于理解的代码: #coding:utf-8 def consumer(): c_r = '' while 1: m = yield c_r if not m: return print(&quo ...
- [LeetCode]题解(python):135-Candy
题目来源: https://leetcode.com/problems/candy/ 题意分析: 有N个孩子站成一条线.每个孩子有个排名.要求1.每个孩子至少一个糖果,2.相邻的孩子,那么较高排名的孩 ...