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 < ...
随机推荐
- 洛谷3372线段树模板题 对区间+k或者查询区间和
#include<bits/stdc++.h> using namespace std; typedef unsigned int ui; typedef long long ll; ty ...
- 如何定时备份Mysql数据库
1.创建备份数据库存储目录 cd data/db mkdir backup #创建存储目录 2.添加备份脚本 vim backupdb.sh #创建脚本文件 脚本内容如下: #!/bin/sh db_ ...
- Spring - 数据库开发概述
Spring 数据库开发 Spring 的 JDBC 模块负责数据库资源管理和镨误处理,大大简化了开发人员对数据库的操作,使得开发人员可以从繁琐的数据库操作中解脱出来,从而将更多的精力投入到编写业 ...
- SQL实战(六)
一. 题目描述 查找排除当前最大.最小salary之后的员工的平均工资avg_salary.CREATE TABLE `salaries` ( `emp_no` int(11) NOT NULL,`s ...
- 基于 HTML5 WebGL 的楼宇智能化集成系统(一)
前言 随着现代通信技术.计算机技术.控制技术的飞速发展,智能建筑已经成为现代建筑发展的主流.智能建筑是以建筑物为平台,兼备信息设施系统.信息化应用系统.建筑设备管理系统.公共安全系统等.集 ...
- @RequestBody和@RequestParam的使用详解
此次分享转载至:https://blog.csdn.net/justry_deng/article/details/80972817 这边文章讲解的比较透彻,主要是在springboot项目中进行使用 ...
- C++STL(一)——string类
STL--string类 初始化 string的赋值 string的连接 string的性质描述 遍历 字符指针和string的转化 查找.替换.交换 字符串的拼接 区间删除. 插入 大小写转换 比较 ...
- [转载]Docker容器无法被stop or kill问题
来源: 问题过程 某环境一个mysql容器无法被stop or kill or rm sudo docker ps | grep mysql 查看该容器 7844250860f8 mysql:5.7. ...
- C++ 模板和 C# 泛型的区别
C# Generics and C++ templates are both language features that provide support for parameterized type ...
- Java Object类学习总结
这篇博文发出来总有点问题,转为图片了,谢谢看官支持.