POJ 1979 Red and Black dfs 难度:0
http://poj.org/problem?id=1979
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 21;
bool vis[maxn][maxn];
char maz[maxn][maxn];
int n,m;
const int dx[4] = {1,-1,0,0};
const int dy[4] = {0,0,1,-1};
int ans; bool in(int x,int y)
{
return x >= 0 && x < n && y >= 0 && y < m;
} void dfs(int x,int y)
{
for(int i = 0;i < 4;i++)
{
int tx = x + dx[i],ty = y + dy[i];
if(in(tx,ty) && !vis[tx][ty] && maz[tx][ty] != '#')
{
ans++;
vis[tx][ty] = true;
dfs(tx,ty);
}
}
} int main(){
while(scanf("%d%d",&m,&n) == 2 && (m || n))
{
ans = 0;
memset(vis,0,sizeof vis);
for(int i = 0;i < n; i++)
{
scanf("%s",maz[i]);
}
for(int x = 0;x < n; x++)
{
for(int y = 0;y < m;y++)
{
if(!vis[x][y] && maz[x][y] == '@')
{
ans++;
vis[x][y] = true;
dfs(x,y);
}
}
}
printf("%d\n",ans);
}
return 0;
}
POJ 1979 Red and Black dfs 难度:0的更多相关文章
- poj 1979 Red and Black(dfs)
题目链接:http://poj.org/problem?id=1979 思路分析:使用DFS解决,与迷宫问题相似:迷宫由于搜索方向只往左或右一个方向,往上或下一个方向,不会出现重复搜索: 在该问题中往 ...
- 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 1321 棋盘问题 dfs 难度:0
http://poj.org/problem?id=1321 注意是在'#'的地方放棋子 矩阵大小不过8*8,即使是8!的时间复杂度也足以承受,可以直接dfs求解 dfs时标注当前点的行和列已被访问, ...
- POJ 1979 Red and Black【DFS】
标准DFS,统计遍历过程中遇到的黑点个数 #include<cstdio> #include<vector> #include<queue> #include< ...
- POJ 3009 Curling 2.0 回溯,dfs 难度:0
http://poj.org/problem?id=3009 如果目前起点紧挨着终点,可以直接向终点滚(终点不算障碍) #include <cstdio> #include <cst ...
- 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)
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 ...
随机推荐
- caffe中的filler.hpp源码的作用:
filler.hpp文件:(它应该没有对应的.cpp文件,一切实现都是在头文件中定义的,可能是因为filler只分在网络初始化时用到那么一次吧) 1,首先定义了基类:Filler,它包括:一个纯虚函数 ...
- md5加密过程
import java.beans.Encoder; import java.security.MessageDigest; import java.security.NoSuchAlgorithmE ...
- hdu 4870 Rating
题目链接:hdu 4870 这题应该算是概率 dp 吧,刚开始看了好几个博客都一头雾水,总有些细节理不清楚,后来看了 hdu 4870 Rating (概率dp) 这篇博客终于有如醍醐灌顶,就好像是第 ...
- 转!!Java学习之自动装箱和自动拆箱源码分析
自动装箱(boxing)和自动拆箱(unboxing) 首先了解下Java的四类八种基本数据类型 基本类型 占用空间(Byte) 表示范围 包装器类型 boolean 1/8 true|fal ...
- android中textview设置为多行文本时,如何让文字从最顶开始显示
<span style="white-space:pre"> </span><EditText android:layout_width=" ...
- PHP开发者常犯的MySQL错误
PHP开发者常犯的MySQL错误 数据库是WEB大多数应用开发的基础.如果你是用PHP,那么大多数据库用的是MYSQL也是LAMP架构的重要部分. PHP看起来很简单,一个初学者也可以几个小时内就 ...
- dom jaxp详解
转自 http://blog.csdn.net/java958199586/article/details/7277904 一.XML解析技术概述 1.XML解析方式分为两种:dom和sax (1)d ...
- listview定位到上次显示的位置
整体思路:滑动lictview时,记录listview的位置,定位时定位到该位置. 1.添加全局变量 private int scrolledX = 0; private int scrolledY ...
- unity入门笔记
我于2010年4月1日硕士毕业加入完美时空, 至今5年整.刚刚从一家公司的微端(就是端游技术+页游思想, 具体点就是c++开发, directX渲染, 资源采取所需才会下载)项目的前端主程职位离职, ...
- Supervisor管理列队
学习了使用 Beanstalkd 管理队列之后,Supervisor 则是用来监听队列的任务,并在队列存在任务的情况下自动帮我们去执行,免去手动敲 php artisan queue:work 的命令 ...