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深度学习与实践(15)
import tensorflow as tf import tensorflow.examples.tutorials.mnist.input_data as input_data mnist = ...
- 接入层高性能缓存技术nginx+redis利器OpenResty
一. OpenRestyOpenResty是一个基于 Nginx与 Lua的高性能 Web平台,其内部集成了大量精良的 Lua库.第三方模块以及大多数的依赖项.用于方便地搭建能够处理超高并发.扩展性极 ...
- Flask最强攻略 - 跟DragonFire学Flask - 第二篇 Flask 中的 Render Redirect HttpResponse
1.Flask中的HTTPResponse 在Flask 中的HttpResponse 在我们看来其实就是直接返回字符串 2.Flask中的Redirect 每当访问"/redi" ...
- extern和include的作用
首先要搞清楚的是.h头文件中都是一些声明性的语句,是不分配内存的,所以头文件中有对函数的声明,有define语句,有没有实例化的结构体定义,但是没有对变量的定义(比如 int a),有的只是对外变量的 ...
- FortiGate抓包 Sniffer
1.图形界面抓包 系统管理--网络--数据包捕获 选择添加好的数据捕获,点击"运行"开关抓包:抓取包后,可以点击"下载"将抓取的数据包保存的本地磁盘,可以用wi ...
- 项目总结19:layui实现表格渲染、表格搜索、数据获取
项目总结19:layui实现表格渲染.表格搜索.数据获取 1-参考资料:https://www.layui.com/demo/table/reload.html 2-本次总结的是layui的表格功能, ...
- xmlhttprequest 1.0和2.0的区别,from qq前端哥
阮一峰好文:http://www.ruanyifeng.com/blog/2012/09/xmlhttprequest_level_2.html
- saltstack 使用salt ‘*’ test.ping 报错Minion did not return(转)
原文地址:http://blog.51cto.com/4634721/2093019 saltstack 使用salt ‘*’ test.ping 报错Minion did not return. [ ...
- node.js中path路径模块的使用
path模块是node.js中处理路径的核心模块.可以很方便的处理关于文件路径的问题. join() 将多个参数值合并成一个路径 const path = require('path'); conso ...
- [原]CentOS 7.2 1511部署L2TP/IPsec服务器及客户端
快过年了,感觉从去年开始,我们公司就变成了“别人的公司”,基本上提前一星期就放假了,好开心.正好可以利用这一段时间,把前段时间一些疑惑的问题解决下:) 然而挡在面前的一个拦路虎是:很多时候不能愉快的G ...