红黑地板问题
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.

Write a program to count the number of black tiles which he can reach by repeating the moves described above.

Input

The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.

There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.

'.' - a black tile 
'#' - a red tile 
'@' - a man on a black tile(appears exactly once in a data set) 
The end of the input is indicated by a line consisting of two zeros. 

Output

For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).

Sample Input

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

Sample Output

45
59
6
13
题目大意就是给定一个矩阵图形,初始位置为@,只能走. 不能走#,问最多能走多少.
这是一道基础的dfs,bfs水题,然而,刚接触dfs和bfs的萌新敲得就很难受,看到这题,首先反应过来的就是用bfs解决,上下左右四个方向遍历,走过的就不能再走,好吧,写了两个bfs,第一个不知道哪错了,先放上WR代码吧
WR:把走过的点直接变为#,把要走的位置存入队列,计数,输出
#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#pragma warning(disable:4996)
using namespace std;
//char a[105][105];
int main()
{
int n, m;
while (cin >> n >> m && n != && m != )
{
char a[][];
memset(a,,sizeof(a));
queue<int>p;
int t1, t2;
for (int i = ; i < m; i++)
for (int j = ; j < n; j++)
{
cin >> a[i][j];
if (a[i][j] == '@')
{
t1 = i;
t2 = j;
p.push(t1);
p.push(t2); }
}
int num = , x, y;
while (p.empty() == false)
{
x = p.front();
p.pop();
y = p.front();
p.pop();
num++;
//cout << num << endl;
if ( <= x - < m && <= y < n && a[x - ][y] == '.')//上
{
p.push(x - );
p.push(y);
a[x - ][y] = '#';
}
if ( <= x + < m && <= y < n && a[x + ][y] == '.')//下
{
p.push(x + );
p.push(y);
a[x + ][y] = '#';
}
if ( <= x < m && <= y - < n && a[x][y - ] == '.')//左
{
p.push(x);
p.push(y - );
a[x][y - ] = '#';
}
if ( <= x < m && <= y + < n && a[x][y + ] == '.')//右
{
p.push(x);
p.push(y + );
a[x][y + ] = '#';
}
}
cout << num << endl;
}
return ;
}

呜呜呜`~实在不知道哪错了

下面是一种采用标记方法的做法

AC:对走过的点进行标记,用visit数组储存访问状态,结构体储存位置存入队列(结构体在bfs中非常实用),bfs基础写法,,,

#include "pch.h"
#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#pragma warning(disable:4996)
using namespace std;
int bfs(int x, int y);
struct point
{
int x, y;
}s, s1;
char a[][];
int visit[][], n, m;
queue<point>p;
int direct[][] = { {-,},{,},{,-},{,} }; bool check(int x, int y)
{
if ( <= x && x < m && <= y && y < n && visit[x][y]== && a[x][y] == '.')
return true;
else
return false;
}
int bfs(int x, int y)
{
visit[x][y] = ;
int sum = ;
while (!p.empty())
{
s1 = p.front();
int t1 = s1.x;
int t2 = s1.y;
sum++;
p.pop();
for (int i = ; i < ; i++)
{
s1.x = t1 + direct[i][];
s1.y = t2 + direct[i][]; if (check(s1.x, s1.y))
{
visit[s1.x][s1.y] = ;
p.push(s1); } }
}
return sum;
}
int main()
{
while (cin >> n >> m && n != && m != )
{
memset(visit, , sizeof(visit));
memset(a, , sizeof(a));
int num = ;
for (int i = ; i < m; i++)
for (int j = ; j < n; j++)
{
cin >> a[i][j];
if (a[i][j] == '@')
{
s.x = i;
s.y = j;
p.push(s); }
}
num = bfs(s.x, s.y);
cout << num << endl; }
return ;
}

目前还没想明白哪不一样,哎,呜呜呜

B - Red and Black 问题思考的更多相关文章

  1. css中的border还可以这样玩

    在看这篇文章之前你可能会觉得border只是简单的绘制边框,看了这篇文章,我相信你也会跟我一样说一句"我靠,原来css中的border还可以这样玩".这篇文章主要是很早以前看了别人 ...

  2. 关于 CSS 反射倒影的研究思考

    原文地址:https://css-tricks.com/state-css-reflections 译者:nzbin 友情提示:由于演示 demo 的兼容性,推荐火狐浏览.该文章篇幅较长,内容庞杂,有 ...

  3. Atitit 设计模式的本质思考】

    Atitit 设计模式的本质思考] 1. 世界就是有模式构建的1 1.1. 多次模式与偶然模式1 1.2. 模式就是在一种场合下对某个问题的一个解决方案."1 1.3. 模式需要三样东西.  ...

  4. animation-fill-mode的一些思考

    animation-fill-mode是css3动画的一个属性,它能够控制元素在动画执行前与动画完成后的样式.一个带有延迟,并且按正常方向执行的动画(正常方向是指从0%运行到100%),执行一次的过程 ...

  5. React.js入门笔记(续):用React的方式来思考

    本文主要内容来自React官方文档中的"Thinking React"部分,总结算是又一篇笔记.主要介绍使用React开发组件的官方思路.代码内容经笔者改写为较熟悉的ES5语法. ...

  6. 安装python爬虫scrapy踩过的那些坑和编程外的思考

    这些天应朋友的要求抓取某个论坛帖子的信息,网上搜索了一下开源的爬虫资料,看了许多对于开源爬虫的比较发现开源爬虫scrapy比较好用.但是以前一直用的java和php,对python不熟悉,于是花一天时 ...

  7. CSS画三角形引发的一些思考

      今天刷知乎时看到了一个问题,有谁能详细讲一下css如何画出一个三角形?怎么想都想不懂? - 知乎.很巧,刚入前端坑的我前不久也遇到过这个问题,今天再来谈一谈这个问题则是因为知乎的一些答案引发了我的 ...

  8. Lucene.net(4.8.0) 学习问题记录五: JIEba分词和Lucene的结合,以及对分词器的思考

    前言:目前自己在做使用Lucene.net和PanGu分词实现全文检索的工作,不过自己是把别人做好的项目进行迁移.因为项目整体要迁移到ASP.NET Core 2.0版本,而Lucene使用的版本是3 ...

  9. Flutter 即学即用系列博客——07 RenderFlex overflowed 引发的思考

    背景 在进行 Flutter UI 开发的时候,控制台报出了下面错误: flutter: ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY >╞════════ ...

随机推荐

  1. MySQL InnoDB引擎B+树索引简单整理说明

    本文出处:http://www.cnblogs.com/wy123/p/7211742.html (保留出处并非什么原创作品权利,本人拙作还远远达不到,仅仅是为了链接到原文,因为后续对可能存在的一些错 ...

  2. 解决Windows 10 1803 April 2018 Updatete不能网络共享的问题

    Windows 10升级到1803后便不能网络共享了,现在我用的是Widnows 10 1809 Oct 2018 Update依然存在这个问题. 为了能够共享文件和文件夹需要去windows ser ...

  3. IO高级应用关于字符码表

    ASCII码表: 计算机里只有数字,我在计算机软件里的一切都是用数字来表示,屏幕上显示的一个个字符也不例外.计算机诞生在美国,最开始所用到字符就是我们现在键盘上的一些符号和少数几个特殊的符号,每一个字 ...

  4. as3.0 嵌入字体的用法

    var txt:TextField = new TextField();//创建文本 txt.embedFonts=true;//确定嵌入字体 var font:Font=new MyFont();/ ...

  5. HDU-1260.Tickets(简单线性DP)

    本题大意:排队排票,每个人只能自己单独购买或者和后面的人一起购买,给出k个人单独购买和合买所花费的时间,让你计算出k个人总共花费的时间,然后再稍作处理就可得到答案,具体格式看题意. 本题思路:简单dp ...

  6. js--随机产生100个从0 ~ 1000之间不重复的整数(me)

    <style>       div{text-indent:40px;} </style> <script> window.onload=function(){ v ...

  7. mysql空值排序

    SELECT * FROM lzh_topic_channel_product ORDER BY order_id is null , order_id  其中的ORDER BY order_id i ...

  8. .net 资源释放(托管资源和非托管资源)

    1.托管资源 像int.float.DateTime等都是托管资源:net中80%的资源都是托管资源: 托管资源的回收通过GC(垃圾回收器)自动释放分配给该对象的内存,但无法预测进行垃圾回收的时间,我 ...

  9. JavaScript 排序算法

    排序也是在程序中经常用到的算法.无论使用冒泡排序还是快速排序,排序的核心是比较两个元素的大小.如果是数字,我们可以直接比较,但如果是字符串或者两个对象呢?直接比较数学上的大小是没有意义的,因此,比较的 ...

  10. xbee/xbeeRPOS1、xbee/xbeePROS2C802.15.4/Digimesh功能方法

    Digi XBee 802.15.4的第一个版本也称为S1,是基于Freescale的无线收发器片子设计的.最新的802.15.4模块(内部称号S1B)采用和Digi ZigBee模块相同SOC芯片设 ...