B - Red and Black 问题思考
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
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
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 问题思考的更多相关文章
- css中的border还可以这样玩
在看这篇文章之前你可能会觉得border只是简单的绘制边框,看了这篇文章,我相信你也会跟我一样说一句"我靠,原来css中的border还可以这样玩".这篇文章主要是很早以前看了别人 ...
- 关于 CSS 反射倒影的研究思考
原文地址:https://css-tricks.com/state-css-reflections 译者:nzbin 友情提示:由于演示 demo 的兼容性,推荐火狐浏览.该文章篇幅较长,内容庞杂,有 ...
- Atitit 设计模式的本质思考】
Atitit 设计模式的本质思考] 1. 世界就是有模式构建的1 1.1. 多次模式与偶然模式1 1.2. 模式就是在一种场合下对某个问题的一个解决方案."1 1.3. 模式需要三样东西. ...
- animation-fill-mode的一些思考
animation-fill-mode是css3动画的一个属性,它能够控制元素在动画执行前与动画完成后的样式.一个带有延迟,并且按正常方向执行的动画(正常方向是指从0%运行到100%),执行一次的过程 ...
- React.js入门笔记(续):用React的方式来思考
本文主要内容来自React官方文档中的"Thinking React"部分,总结算是又一篇笔记.主要介绍使用React开发组件的官方思路.代码内容经笔者改写为较熟悉的ES5语法. ...
- 安装python爬虫scrapy踩过的那些坑和编程外的思考
这些天应朋友的要求抓取某个论坛帖子的信息,网上搜索了一下开源的爬虫资料,看了许多对于开源爬虫的比较发现开源爬虫scrapy比较好用.但是以前一直用的java和php,对python不熟悉,于是花一天时 ...
- CSS画三角形引发的一些思考
今天刷知乎时看到了一个问题,有谁能详细讲一下css如何画出一个三角形?怎么想都想不懂? - 知乎.很巧,刚入前端坑的我前不久也遇到过这个问题,今天再来谈一谈这个问题则是因为知乎的一些答案引发了我的 ...
- Lucene.net(4.8.0) 学习问题记录五: JIEba分词和Lucene的结合,以及对分词器的思考
前言:目前自己在做使用Lucene.net和PanGu分词实现全文检索的工作,不过自己是把别人做好的项目进行迁移.因为项目整体要迁移到ASP.NET Core 2.0版本,而Lucene使用的版本是3 ...
- Flutter 即学即用系列博客——07 RenderFlex overflowed 引发的思考
背景 在进行 Flutter UI 开发的时候,控制台报出了下面错误: flutter: ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY >╞════════ ...
随机推荐
- Python单元测试unittest【转自https://www.cnblogs.com/feng0815/p/8045850.html】
[转自https://www.cnblogs.com/feng0815/p/8045850.html] Python中有一个自带的单元测试框架是unittest模块,用它来做单元测试,它里面封装好了一 ...
- P1147连续自然数和-(尺取法)
https://www.luogu.org/problemnew/show/P1147 题意:输入一个n,求连续几个数加起来等于n,输出这几个连续的数的第一个和最后一个.10<=n<=20 ...
- Linux ssh命令
SSH(远程连接工具)连接原理:ssh服务是一个守护进程(demon),系统后台监听客户端的连接,ssh服务端的进程名为sshd,负责实时监听客户端的请求(IP 22端口),包括公共秘钥等交换等信息. ...
- 监测SQLServer数据库中表的数据变化 方案
sqlDependency提供了这样一种能力:当被监测的数据库中的数据发生变化时,SqlDependency会自动触发OnChange事件来通知应用程序,从而达到让系统自动更新数据(或缓存)的目的. ...
- python爬虫小说代码,可用的
python爬虫小说代码,可用的,以笔趣阁为例子,python3.6以上,可用 作者的QQ:342290433,汉唐自远工程师 import requests import refrom lxml i ...
- IntelliJ IDEA常用快捷键(Mac)
Mac 键盘符号和修饰键说明 ⌘ ——> Command ⇧ ——> Shift ⌥ ——> Option ⌃ ——> Control ↩︎ ——> Return/Ent ...
- abp Cannot access a disposed object. A common cause of this error is disposing
框架:abp 异常信息: An unhandled exception was thrown by the application.System.ObjectDisposedException: Ca ...
- Linux驱动之poll机制的理解与简单使用
之前在Linux驱动之按键驱动编写(中断方式)中编写的驱动程序,如果没有按键按下.read函数是永远没有返回值的,现在想要做到即使没有按键按下,在一定时间之后也会有返回值.要做到这种功能,可以使用po ...
- 通过类名或者jar名查询所在jar包
一.问题 例如我想查看一下FilterSecurityInterceptor的源码,但是我不知道它在maven依赖中的哪个jar包中 二.解决方案 http://www.findmaven.net/ ...
- 洛谷4556 [Vani有约会]雨天的尾巴
原题链接 每个点开一个权值线段树,然后用树上差分的方法修改,最后自底向上暴力线段树合并即可. 不过空间较大,会\(MLE\),写个内存池就可以了. #include<cstdio> #in ...