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 >╞════════ ...
随机推荐
- ArcGIS自定义工具箱-显示地图文档结构
ArcGIS自定义工具箱-显示地图文档结构 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 目的:显示地图文档mxd的数据组织结构,数据框,图层,表 使用方法: 地图 ...
- python学习Day4 流程控制(if分支,while循环,for循环)
复习 1.变量名命名规范 -- 1.只能由数字.字母 及 _ 组成 -- 2.不能以数字开头 -- 3.不能与系统关键字重名 -- 4._开头有特殊含义 -- 5.__开头__结尾的变量,魔法变量 - ...
- BuildTool
(一)BuildTool是什么 BuildTool 构建工具 ,是一个把源代码生成可执行应用程序的过程自动化的程序(例如Android app生成apk).构建包括编译.连接跟把代码打包成可用的或可 ...
- CDH hue下定时执行hive脚步
今天在看oozie时发现能在hue中执行hive 脚本,主要是hue 和 oozie结合使用,下面介绍下怎么使用的,挺恶心的,哈哈(在这里就不哔哔了) 提交oozie定时作业 1.进入hue界 ...
- 大数据入门到精通13--为后续和MySQL数据库准备
We will be using the sakila database extensively inside the rest of the course and it would be great ...
- Mysql 单表查询 子查询 关联查询
数据准备: ## 学院表create table department( d_id int primary key auto_increment, d_name varchar(20) not nul ...
- tomcat/Java指定加载jar包的路径
背景:部署的web站点,应用默认加载工程的/webapps/工程名/WEB-INF/lib下的jar包 但是我需要提供一个和web工程没关系的的jar包管理目录 解决方法: 执行java方法时 ...
- Error running Tomcat8: Address localhost:xxxx is already in use
参考自: https://blog.csdn.net/huazhongkejidaxuezpp/article/details/41813683 第一步,命令提示符号,执行命令:netstat -an ...
- Heartbleed心脏出血漏洞原理分析
Heartbleed心脏出血漏洞原理分析 2017年01月14日 18:14:25 阅读数:2718 1. 概述 OpenSSL在实现TLS和DTLS的心跳处理逻辑时,存在编码缺陷.OpenSS ...
- Django之URL(路由系统)用法
路由系统 路由系统概念 简而言之,路由系统就是路径和视图函数的一个对应关系.django的路由系统作用就是使views里面处理数据的函数与请求的url建立映射关系.使请求到来之后,根据urls.py里 ...