-->Red and Black

Descriptions:

有个铺满方形瓷砖的矩形房间,每块瓷砖的颜色非红即黑。某人在一块砖上,他可以移动到相邻的四块砖上。但他只能走黑砖,不能走红砖。

敲个程序统计一下这样可以走到几块红砖上。


Input 

多组测试用例。每组数组开头有两个正整数W和H;W与H分别表示 x- 与 y- 方向上瓷砖的数量。W和W均不超过20。

还有H行数据,每行包含W个字符。每个字符表示各色瓷砖如下。

'.' - 一块黑砖 
'#' - 一块红砖 
'@' - 一个黑砖上的人(一组数据一个人) 
输入以一行两个零为结束。 


Output

对于每组测试用例,输出他从起始砖出发所能抵达的瓷砖数量(包括起始砖)。


Sample Input 

6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0

Sample Output

45
59
6
13

题目链接:

https://vjudge.net/problem/POJ-1979

最近在写dfs之类的题,我发现,dfs之类的题配合染色非常方便,这题亦是如此

先把所有地图都标为0,起点为1,找到"."后编号加一即可,最后输出编号

详情看代码

AC代码

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define MEM(x,y) memset(x,y,sizeof(x))
#define Maxn 25
using namespace std;
int n,m;
int ans;
int sx,sy;//起点横纵坐标
int vis[Maxn][Maxn];//染色计数
char mp[Maxn][Maxn];//原始地图
int dt[][]= {{,},{-,},{,},{,-}};//4个方向
void dfs(int x,int y)
{
if(vis[x][y])//染过色了
return;
vis[x][y]=++ans;//没染色,给他染色
for(int i=; i<; i++)//四个方向
{
int tx=x+dt[i][];
int ty=y+dt[i][];
if(mp[tx][ty]=='.')//满足条件
dfs(tx,ty);
}
}
int main()
{
while(cin>>n>>m,n+m)
{
ans=;//初始化
MEM(vis,);
MEM(mp,'#');
for(int i=; i<m; i++)//输入地图,找到起点
for(int j=; j<n; j++)
{
cin>>mp[i][j];
if(mp[i][j]=='@')
sx=i,sy=j;
}
dfs(sx,sy);
cout<<ans<<endl;
}
}

【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(dfs)

    题目链接:http://poj.org/problem?id=1979 思路分析:使用DFS解决,与迷宫问题相似:迷宫由于搜索方向只往左或右一个方向,往上或下一个方向,不会出现重复搜索: 在该问题中往 ...

  3. POJ 1979 Red and Black (红与黑)

    POJ 1979 Red and Black (红与黑) Time Limit: 1000MS    Memory Limit: 30000K Description 题目描述 There is a ...

  4. 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 ...

  5. poj 1979 Red and Black(dfs水题)

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

  6. POJ 1979 Red and Black【DFS】

    标准DFS,统计遍历过程中遇到的黑点个数 #include<cstdio> #include<vector> #include<queue> #include< ...

  7. POJ 1979 Red and Black (DFS)

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

  8. POJ 1979 Red and Black (简单dfs)

    题目: 简单dfs,没什么好说的 代码: #include <iostream> using namespace std; typedef long long ll; #define IN ...

  9. poj 1979 Red and Black 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=1979 Description There is a rectangular room, covered with square tiles ...

  10. OpenJudge/Poj 1979 Red and Black / OpenJudge 2816 红与黑

    1.链接地址: http://bailian.openjudge.cn/practice/1979 http://poj.org/problem?id=1979 2.题目: 总时间限制: 1000ms ...

随机推荐

  1. WPF 在一个dll创建一个Window(包含xaml),在另一个dll中再次继承 会出错

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/e92390eb-bbfa-42fb-baa9-2286444c0dca/the-comp ...

  2. CNN(卷积神经网络)、RNN(循环神经网络)和DNN(深度神经网络)

    本文转载修改自:知乎-科言君 感知机(perceptron) 神经网络技术起源于上世纪五.六十年代,当时叫感知机(perceptron),拥有输入层.输出层和一个隐含层.输入的特征向量通过隐含层变换达 ...

  3. nltk处理文本

    nltk(Natural Language Toolkit)是处理文本的利器. 安装 pip install nltk 进入python命令行,键入nltk.download()可以下载nltk需要的 ...

  4. Linux之mysql安装

    查看文件内容的命令有很多:cat, tac, more, less, head, tail, nl. cat由第一行开始显示档案内容:tac从最后一行开始显示,可以看出tac是cat的倒着写:more ...

  5. 迁移到MSYS2 与 Qt 工具链注意的几个事情(g++在链接时,符号依赖项查找遵循从左至右的顺序,但qmake会自动合并造成错误。使用脚本给Mingw32-make创造出一个局部的VC编译环境)

    Microsoft Visual Studio 2015社区版提供了强大的开发体验,且 Qt 提供了预编译版本.然而,由于客户提出兼容Windows XP ~ Windows 8.1 这样宽泛的环境要 ...

  6. CSS3 GENERATOR可以同时为一个元素完成border-radius、box-shadow、gradient和opacity多项属性的设置

    CSS3 GENERATOR可以同时为一个元素完成border-radius.box-shadow.gradient和opacity多项属性的设置 CSS3 GENERATOR 彩蛋爆料直击现场 CS ...

  7. QT5---应用程序发布(使用windeployqt和NSIS)

      采用动态编译的方式发布程序,即release版本. 找齐动态依赖库(.dll) 方法一   用Dependency Walker这个工具去找少了那些dll,不过这个工具也不怎么靠谱,一个比较靠谱但 ...

  8. Python连载14-random模块&函数式编程

    ​一.random模块 1.函数:random() (1)用法:获取0~1之间的随即小数 (2)格式:random.random() (3)返回值:随机0~1之间的小数 2.函数:choice() ( ...

  9. 【数据结构】31、hashmap=》resize 扩容,不测不知道,一测吓一跳

    来来来,今天就跟hashmap杠到底... 不要叫我杠精了,主要是还是被问到hashmap的时候,我并不能很清晰明了得告知这种数据结构到底是一个什么构造,里面细节并不了解 既然这样,我们就把他解析一波 ...

  10. jvm异常记录

    1.如果出现java.lang.OutOfMemoryError: Java heap space异常.原因:Java虚拟机的堆内存不够. 具体如下:                a.Java虚拟机 ...