I - Red and Black DFS
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
InputThe 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)
OutputFor 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
#include<iostream>
#include<cstring>
using namespace std;
int w,h,ans;
char arr[][];
int mark[][];
int d[][]={{,},{,},{,-},{-,}};
void dfs(int x,int y)
{
mark[x][y]=;
for(int i=;i<;i++){
int dx=x+d[i][];
int dy=y+d[i][];
if(dx>=&&dy>=&&dx<h&&dy<w&&mark[dx][dy]==&&arr[dx][dy]=='.'){
ans++;
mark[dx][dy]=;
dfs(dx,dy);
}
}
} int main()
{
while(cin>>w>>h){
memset(mark,,sizeof(mark));
ans=;
if(w==&&h==)
break;
for(int i=;i<h;i++){
scanf("%s",&arr[i]);
}
for(int i=;i<h;i++)
for(int j=;j<w;j++){
if(arr[i][j]=='@'){
// mark[i][j]=1;
dfs(i,j);
}
}
cout<<ans<<endl; }
return ;
}
BFS也可以写就是只要是x周围存在"."就加1,
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
char arr[][];
int sa,ea;
int n,m;
int v[][]={};
struct stu{
int a,b;
// int sum;
}; int a[]={,,,-};
int b[]={,,-,}; void bfs()
{
int ans=;
// priority_queue<stu >que;
queue<stu>que;
stu q1;
q1.a=sa;
q1.b=ea;
// q1.sum=1;
v[sa][ea]=;
que.push(q1); while(que.size()){
stu h;
// h=que.top();
h=que.front();
que.pop();
stu d;
for(int i=;i<;i++){
d.a=h.a+a[i];
d.b=h.b+b[i];
if(d.a>= && d.b>= && d.a<m && d.b<n&& v[d.a][d.b]!= && arr[d.a][d.b]!='#'){
v[d.a][d.b]=;
// d.sum=h.sum+1;
// cout<<d.sum<<endl;
que.push(d);
// ans=max(ans,d.sum);
ans++;//只要加入到队列中就加一,因为加入到队列的一定是'.'
}
}
}
cout<<ans+<<endl;
} int main(){ while(cin>>n>>m)
{
memset(v,,sizeof(v)); if(n==&&m==)
break; for(int i=;i<m;i++){
scanf("%s",&arr[i]);
} for(int i=;i<m;i++){
for(int j=;j<n;j++){
if(arr[i][j]=='@'){
sa=i;
ea=j;
}
}
} bfs();
}
return ;
}
I - Red and Black DFS的更多相关文章
- HDU 1312 Red and Black DFS(深度优先搜索) 和 BFS(广度优先搜索)
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- HDU1312——Red and Black(DFS)
Red and Black Problem DescriptionThere is a rectangular room, covered with square tiles. Each tile i ...
- 数据结构——HDU1312:Red and Black(DFS)
题目描述 There is a rectangular room, covered with square tiles. Each tile is colored either red or blac ...
- HDU 1312 Red and Black (DFS)
Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...
- HDU 1312 Red and Black(DFS,板子题,详解,零基础教你代码实现DFS)
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDOJ1312 Red and black(DFS深度优先搜索)
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A ...
- POJ 1979 Red and Black dfs 难度:0
http://poj.org/problem?id=1979 #include <cstdio> #include <cstring> using namespace std; ...
- poj 1979 Red and Black(dfs)
题目链接:http://poj.org/problem?id=1979 思路分析:使用DFS解决,与迷宫问题相似:迷宫由于搜索方向只往左或右一个方向,往上或下一个方向,不会出现重复搜索: 在该问题中往 ...
- poj1979 Red And Black(DFS)
题目链接 http://poj.org/problem?id=1979 思路 floodfill问题,使用dfs解决 代码 #include <iostream> #include < ...
随机推荐
- WordPress 迁移站点更换域名为新域名
使用 wp-cli 工具搜索替换域名的方式更换 WordPress 域名 wp-cli 是一个命令行工具,可以让我们通过命令行安装.更新 WordPress,对 WordPress 执行一些批量操作, ...
- 使用Keras进行深度学习:(一)Keras 入门
欢迎大家关注我们的网站和系列教程:http://www.tensorflownews.com/,学习更多的机器学习.深度学习的知识! Keras是Python中以CNTK.Tensorflow或者Th ...
- 旷视6号员工范浩强:高二开始实习,“兼职”读姚班,25岁在CVPR斩获第四个世界第一...
初来乍到,这个人说话容易让人觉得"狂". "我们将比赛结果提交上去,果不其然,是第一名的成绩."当他说出这句话的时候,表情没有一丝波澜,仿佛一切顺理成章. 他说 ...
- ClickHouse学习系列之一【安装说明】
背景 最近花了些时间看了下ClickHouse文档,发现它在OLAP方面表现很优异,而且相对也比较轻量和简单,所以准备入门了解下该数据库系统.按照习惯第一步先安装,本文对其用户权限管理方面进行梳理说明 ...
- ES6语法:函数新特性(一)
ES6 函数 引言: 函数在任何语言中偶读很重要,java里面的函数通常叫做方法,其实是一个东西,使用函数可以简化更多的代码,代码结构看着更加清晰.今天我们来学学ES6语法中,函数有什么变化. 虽然现 ...
- coding++:error 阿里云 Redis集群一直Waiting for the cluster to join....存在以下隐患
1):Redis集群一直Waiting for the cluster to join... 再次进行连接时首先需要以下操作 1.使用redis desktop Manager连接所有节点 调出命令窗 ...
- iOS 第三方库
网络 AFNetworking HTTP网络库 Reachability 网络监测 UI.布局 Masonry AutoLayout SnapKit AutoLayout Swift TOWebVie ...
- python opencv 图片缺陷检测(讲解直方图以及相关系数对比法)
一.利用直方图的方式进行批量的图片缺陷检测(方法简单) 二.步骤(完整代码见最后) 2.1灰度转换(将原图和要检测对比的图分开灰度化) 灰度化的作用是因为后面的直方图比较需要以像素256为基准进行相关 ...
- 基于Java的数字货币交易系统的架构设计与开发
前言 无论是股票交易系统,还是数字货币交易系统,都离不开撮合交易引擎,这是交易平台的心脏.同时,一个优秀的架构设计也会让交易平台的运维和持续开发更加容易.本文基于对开源项目的深入研究,总结了数字货币交 ...
- 面试官:JavaScript 原始数据类型 Symbol 有什么用?
以前提到 JavaScript 原始数据类型时,我们知道有Number,String,Null,Boolean,Undefined这几种.ES6 引入了新的基本数据类型Symbol和BigInt.今天 ...