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.

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的更多相关文章

  1. 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 ...

  2. HDU1312——Red and Black(DFS)

    Red and Black Problem DescriptionThere is a rectangular room, covered with square tiles. Each tile i ...

  3. 数据结构——HDU1312:Red and Black(DFS)

    题目描述 There is a rectangular room, covered with square tiles. Each tile is colored either red or blac ...

  4. HDU 1312 Red and Black (DFS)

    Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...

  5. 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 ...

  6. HDOJ1312 Red and black(DFS深度优先搜索)

    There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A ...

  7. POJ 1979 Red and Black dfs 难度:0

    http://poj.org/problem?id=1979 #include <cstdio> #include <cstring> using namespace std; ...

  8. poj 1979 Red and Black(dfs)

    题目链接:http://poj.org/problem?id=1979 思路分析:使用DFS解决,与迷宫问题相似:迷宫由于搜索方向只往左或右一个方向,往上或下一个方向,不会出现重复搜索: 在该问题中往 ...

  9. poj1979 Red And Black(DFS)

    题目链接 http://poj.org/problem?id=1979 思路 floodfill问题,使用dfs解决 代码 #include <iostream> #include < ...

随机推荐

  1. hdu2203kmp匹配

    拼接字符串即可解决移位的问题: 代码如下: #include<bits/stdc++.h> using namespace std; typedef unsigned int ui; ty ...

  2. 【2019多校第一场补题 / HDU6582】2019多校第一场E题1005Path——最短路径+网络流

    HDU6582链接 题意 在一张有向图中,有一个起点和一个终点,你需要删去部分路径,使得起点到终点的最短距离增加(并不要求需要使得距离变成最大值),且删除的路径长度最短.求删去的路径总长为多少 分析 ...

  3. shell编程之脚本参数$@,$*,$#,$$,$?的含义

    #首先按顺序解释各个参数的含义 1.$0  表示脚本的文件名, 具体的路径信息和执行命令时的相对位置有关,例如 sakura@mi-OptiPlex-7050:~/sh$ sh args.sh arg ...

  4. mysql系列--sql实现原理

    count(*) MyISAM 引擎把⼀个表的总⾏数存在了磁盘上,因此执⾏ count(*) 的时候会直接返回这个数,效率很⾼:但是加了条件则不能快速返回⽽ InnoDB 引擎就麻烦了,它执⾏ cou ...

  5. matplotlib.pyplot.text

    matplotlib.pyplot.text(x, y, s, fontdict=None, withdash=<deprecated parameter>, **kwargs)[sour ...

  6. Transformers 简介(上)

    作者|huggingface 编译|VK 来源|Github Transformers是TensorFlow 2.0和PyTorch的最新自然语言处理库 Transformers(以前称为pytorc ...

  7. 使用TensorFlow v2库实现线性回归

    使用TensorFlow v2库实现线性回归 此示例使用简单方法来更好地理解训练过程背后的所有机制 from __future__ import absolute_import, division, ...

  8. 用卷积神经网络和自注意力机制实现QANet(问答网络)

    欢迎大家关注我们的网站和系列教程:http://www.tensorflownews.com/ ,学习更多的机器学习.深度学习的知识! 在这篇文章中,我们将解决自然语言处理(具体是指问答)中最具挑战性 ...

  9. 关于用命令行和idea对项目打jar包

    前提说一下,我们一般是对编译后的项目进行打包,不然打包后还得自己去重新编译class文件. 假如这是你的一个项目目录: 我们要写一个简单的计算器工具类项目,然后对他进行打包, idea里面out文件夹 ...

  10. Oracle 11g服务端的安装和配置

    1.双击Oracle11g_database安装目录下的Setup.exe. 2.选择“基本安装”,设置“安装位置”,填写“数据库名”和“口令”,点击“下一步”. 3.点击“下一步”. 4.一般会出现 ...