-->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. 工具:sql server profiler(分析器)

    打开profiler新建->连接数据库进行监测 任何访问该数据库的都有记录   image 对于linq的检验:sql实际如何->运行程序,查看分析器记录   image         ...

  2. ASP .NET Model

    Model是全局变量,一个页面一个 前台 @ModelWebApplication1.Models.Movie; @{ ViewBag.Title = "ModelTest"; } ...

  3. MYSQL 定时自动执行EVENT

    MySQL从5.1开始支持EVENT功能,类似Oracle和MSSQL的定时任务job功能.有了这个功能之后我们就可以让MySQL自动的执行存储过程来实现数据汇总等功能了,不用像以前哪样手动操作完成了 ...

  4. WPF 遍历 控件

    比较简单的方式是 在设计一个画面时 先添加一个grid 或其他的布局控件 确保要遍历的控件都在这个Grid中时就可以这么写 foreach (UIElement uie in Grid.Childre ...

  5. 零元学Expression Blend 4 - Chapter 31 看如何简单的把SampleData 绑进ListBox里

    原文:零元学Expression Blend 4 - Chapter 31 看如何简单的把SampleData 绑进ListBox里 前面几章连续讲到ListBox的运用,本章要讲得是如何简单的把Sa ...

  6. 解决WPF中TextBox文件拖放问题

    在WPF中,当我们尝试向TextBox中拖放文件,从而获取其路径时,往往无法成功(拖放文字可以成功).造成这种原因关键是WPF的TextBox对拖放事件处理机制的不同,具体可参考这篇文章Textbox ...

  7. Cannot read property 'substring' of undefined

    这个是在使用 jquery的ztree插件过程中遇到的错误 原因是数据的格式和规定的格式不一致,需要把数据按照模板给的样子来

  8. Spring MVC的工作原理,我们来看看其源码实现

    前言 开心一刻 晚上陪老丈人吃饭,突然手机响了,我手贱按了免提……哥们:快出来喝酒!哥几个都在呢!我:今天不行,我现在陪老丈人吃饭呢.哥们:那你抓紧喝,我三杯白酒,把我岳父放倒了才出来的,你也快点.看 ...

  9. 使用 Visual Studio 开发并调试 Mail Add-in (mail app for Outlook)

    准备工作 如果你的邮箱搭建在 Exchange Server 上,则可以创建邮件应用程序(Mail Add-in)来扩展Office本身的功能,使用 Office Add-in Model 开发的 M ...

  10. 很幽默的讲解六种Socket IO模型 Delphi版本(自己Select查看,WM_SOCKET消息通知,WSAEventSelect自动收取,Overlapped I/O 事件通知模型,Overlapped I/O 完成例程模型,IOCP模型机器人)

    很幽默的讲解六种Socket IO模型(转)本文简单介绍了当前Windows支持的各种Socket I/O模型,如果你发现其中存在什么错误请务必赐教. 一:select模型 二:WSAAsyncSel ...