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. 说说自己为什么用Mac不用Win系统?

    原本Mac和Win系统各有优劣,但偏偏最近有人误导身边的朋友说"学编程肯定是Windows系统呀,Mac不行的",又不给出有说服力的理由,于是我心有愤懑,正好趁机总结一下自己对于两 ...

  2. django中 对Mysql数据库的建表

    Django操作Mysql数据库: 1.1 在settings中,配置数据库相关参数,所以无需修改,这里我们看一下: DATABASES = { 'default': { # 这里可以指定使用的数据库 ...

  3. [noip2016]愤怒的小鸟<状压dp+暴搜>

    题目链接:https://vijos.org/p/2008 现在回过头去看去年的考试题,发现都不是太难,至少每道题都有头绪了... 这道题的数据范围是18,这么小,直接暴力呗,跑个暴搜就完了,时间也就 ...

  4. Linux 脏数据回刷参数与调优

    简介 我们知道,Linux用cache/buffer缓存数据,且有个回刷任务在适当时候把脏数据回刷到存储介质中.什么是适当的时候?换句话说,什么时候触发回刷?是脏数据达到多少阈值还是定时触发,或者两者 ...

  5. tensorflow编程学习路线及笔记

    话不多说,直接上图! 关于人工智能算法学习思路,欢迎浏览我的另一篇随笔:如果你想开始学习算法,不妨先了解人工智能有哪些方向? 关于python编程学习路线及笔记,欢迎浏览我的另一篇随笔:python编 ...

  6. jmeter发送Query String Parameters格式参数报错

    当发起一次GET请求时,参数会以url string的形式进行传递.即?后的字符串则为其请求参数,并以&作为分隔符 当参数为json格式时,这时需要勾选编码,否则会报错

  7. JavaScript基本数据类型及其转换规则

    ECMAScript 数据类型 ECMAScript中有五种基本数据类型:Undefined, Null, Boolean, Number, String 一种复杂数据类型:Object 数据类型检测 ...

  8. git rebase解决合并冲突

    git rebase解决合并冲突   记录合并冲突解决方法,使用的git rebase,感觉很好用 1.git rebase 文档 https://git-scm.com/docs/git-rebas ...

  9. css | js 实现扩展卡片小demo

    1.代码如下 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  10. go 结构开发规范

    机构规范: // 当前程序的包名 package main //导入其他的包 import "fmt" //常量的定义 const PI=3.14 //全局变量的声明和赋值 var ...