-->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. vs调试cordova app时 scriptedsandbox64.exe已停止工作的错误处理方法

    1.把ie更新到版本11 2.去掉 选项->调试->调试时启动诊断工具 有时候调试会启动失败,提示版本过低,再一次点击调试就可以了.

  2. delphi Stomp客户端连接 RabbitMQ(1)

    最近公司想上个消息推送系统,网上搜了很多,因公司主要产品是Delphi,我选择了开源的RabbitMQ,Erlang语言开发,天生并行. 代码下载地址:delphistomp下载地址 windows上 ...

  3. Redis 高可用之哨兵模式

    参考   : https://mp.weixin.qq.com/s/Z-PyNgiqYrm0ZYg0r6MVeQ 一.redis高可用解决方案 redis主从 优点:1.高可靠性,主从实时备份,有效解 ...

  4. Android应用开机自启动问题

    本文主要介绍Android应用如何实现开机自启动.自启动失败的原因以及通过ADB命令模拟发送BOOT_COMPLETED开机广播. 1.Android应用如何实现开机自启动 (1) 实现一个广播类,接 ...

  5. MacOS X编译OpenSceneGraph

    本文主要记录在MacOS X上编译OpenSceneGraph,方便日后查阅.所使用的环境如下: MacOS X 10.10 Yosemite XCode 6.3.2 CMake 3.3.0 Open ...

  6. 转换GMT秒数为日期时间格式-Delphi源码

    转换GMT秒数为日期时间格式-Delphi源码.收藏最近在写PE分析工具的时候,需要转换TimeDateStamp字段值为日期时间格式,这是Delphi的源码. //把GMT时间的秒数转换成日期时间格 ...

  7. 在Delphi中编辑res文件

    先用记事本编写一个rc的文件.如内容为:_Comms RCData Comms.jpg Comms.jpg为图片名称,然后在这个rc文件和图片拷贝到delphi安装路径的bin文件夹里面,选中这两个文 ...

  8. Qt技术优势

    1. Qt这个C++的图形库由Trolltech在1994年左右开发.它可以运行在Windows,Mac OS X, Unix,还有像Sharp Zaurus这类嵌入式系统中.Qt是完全面向对象的. ...

  9. 三个臭皮匠,顶上一个诸葛亮——在Google Ideathon上Design Thinking分享

    4月26日很荣幸的被邀请参加Google Ideathon做Design Thinking的分享. 这次主要分享了Design Thinking的基本方法流程,以及在真实项目的运用.现在整理一下当时选 ...

  10. Codility---Brackets

    Task description A string S consisting of N characters is considered to be properly nestedif any of ...