Red and Black 模板题 /// BFS oj22063
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.
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and Hare 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.
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).
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
45
59
6
13
#include <bits/stdc++.h> using namespace std; int main() {int m[4][2];m[0][0]=-1; m[0][1]=0;m[1][0]=1; m[1][1]=0;m[2][0]=0; m[2][1]=-1;m[3][0]=0; m[3][1]=1; /// 上下左右四种位移int h,w,flag[25][25];while(~scanf("%d%d",&h,&w)&&h&&w){int a,b,sum=1; char ch[25][25];for(int i=1;i<=w;i++){getchar();for(int j=1;j<=h;j++){scanf("%c",&ch[i][j]);if(ch[i][j]=='@') a=i,b=j; /// 记录人的位置,即起点 }}memset(flag,0,sizeof(flag));flag[a][b]=1;queue <int> x,y;x.push(a),y.push(b);while(!x.empty()&&!y.empty()){for(int i=0;i<4;i++){a=x.front()+m[i][0];b=y.front()+m[i][1];if(a>0&&a<=w&&b>0&&b<=h //如果点在范围内,且&&ch[a][b]!='#'&&!flag[a][b])//位于没走过的黑瓷砖x.push(a), y.push(b), sum++; //放入队列 否则忽略flag[a][b]=1;}x.pop(), y.pop();}printf("%d\n",sum);}return 0; }
View Code
转载于:https://www.cnblogs.com/zquzjx/p/8409037.html
Red and Black 模板题 /// BFS oj22063相关推荐
- Pathfinding 模板题 /// BFS oj21413
题目大意: Description Bessie is stranded on a deserted arctic island and wants to determine all the path ...
- poj2449 K短路模板题
昨晚看WC论文发现自己连K短路的经典A*算法还不会,补了一波,模板题输出-1后没return继续跑wa了一早上...... 算法流程: ①在反向图中求出t到每个点的最短路 ②从原点bfs,估价f=d+ ...
- 个人算法题精简导航整理(精炼汇总,含知识点、模板题、题单)
文章目录 前言 导航 注意事项 技巧类 自定义Pair 排序 N维数组转一维 位运算 状态压缩 算法基础 枚举 √ 指数型枚举 排列型枚举 组合型枚举 左右区间枚举 模拟 √ 日期天数问题:平年闰年情 ...
- 图论-有向图的连通性模板题(hdu1296)(hdu1827)
1.强连通分量: 强连通分量可以理解为边数最少的情况下是一个环. 这里写了一个模板题用的是tarjan算法,当然还有其他算法. tarjan算法的关键其实还是对于num数组和low数组的使用 然后可以 ...
- P1339 热浪 最短路径模板题
这么naive的题面一看就是最短路模板题~~~ ok.首先是floyd算法,tts,记得把k放在最外面就行了. 1 #include <cstdio> 2 #include <cst ...
- HDU2255 奔小康赚大钱(km模板题)
Description 传说在遥远的地方有一个非常富裕的村落,有一天,村长决定进行制度改革:重新分配房子. 这可是一件大事,关系到人民的住房问题啊.村里共有n间房间,刚好有n家老百姓, ...
- HDU1166 敌兵布阵(树状数组模板题)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- 数单词 (AC自动机模板题)
数单词 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 为了能够顺利通过英语四六级考试,现在大家每天早上都会早起读英语. LYH本来以为自己在6月份的考试中可以通过六级, ...
- CSP认证201509-4 高速公路[C++题解]:强连通分量、tarjan算法模板题
题目分析 来源:acwing 分析: 所求即为强连通分量的个数,然后计算每个强连通分量中点的个数,相加即可. 所谓强连通分量,它是一个子图,其中任意两点可以相互到达,并且再加一个点,就不能满足任意两点 ...
最新文章
- 谷歌BERT预训练源码解析(一):训练数据生成
- php中的全局变量$_POST收集表单数据
- 内核开发常备手册翻译(一)
- client-go入门之3:解析 yaml 文件并创建 k8s 资源对象
- 红帽目录服务的数据导入导出
- [最新答案V0.4版]微软等数据结构+算法面试100题[第41-60题答案]
- hash地址_深入浅出一致性Hash原理
- 安装虚拟环境virtualenv 适用于windows操作系统
- 在C#中用RX库和await来实现直观的状态机
- 小米闪购首页实战(HTML+CSS+JS)
- matlab的imnoise
- 2021年道路运输企业安全生产管理人员考试内容及道路运输企业安全生产管理人员考试报名
- DBSCAN聚类算法原理总结
- 计算机网络:计算路由表下一跳
- [美容美发培训学校网站模板]织梦模版+响应式形象设计艺术教育学校网站dedecms模板+手机自适应
- ubuntu16 解决机箱前置耳机无声
- MacBook软件集合
- 自媒体人都在用的免费音效素材网站
- 管道和FIFO概念以及相关函数(pipe、mkfifo)介绍
- 厦大2021届大一小学期C语言作业1 数组+字符串+指针+位操作
热门文章
- 认识LTE(八):LTE中的反馈:闭环空分复用(TM4)
- 深度学习中Map的概念
- 计算机技术中常用的术语CAE是指_,计算机技术中常用的术语CAE是指______。A:计算机辅助设计B:计算机辅助制造C:计算机辅助工程D:计...
- 解决阿里云windows服务器无法连接远程桌面
- unshift() :将一个或多个元素添加到数组的开头
- SQL SERVER中LEAD和LAG函数
- casewhen多条件查询_Mybatis(三)- MyBatis 多条件查询和模糊查询实例
- 基于Mui与H5+开发webapp的Android原生工程打包步骤(使用新版本5+SDK与Android studio)(部分内容转自dcloud官网)...
- 伪静态、静态、动态url
- java 获取Authorization信息