DFS深搜-Red and Black

深搜,从一点向各处搜找到所有能走的地方。

Problem Description

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.

Input

The 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)

Output

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

Sample Input

6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#[email protected]#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
[email protected]
###.###
..#.#..
..#.#..
0 0

Sample Output

45
59
6
13

Source

Asia 2004, Ehime (Japan), Japan Domestic

代码:

#include<iostream>

using namespace std;

char map[22][22];//定义最大数组

int sum,l,h;

int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; //四个方位,上、下、左、右

bool border(int x,int y)//判断是否超范围

{

if(x<0||x>=h||y<0||y>=l) return 0;

return 1;

}

void search(int x,int y)

{

int i;

int xx,yy;

sum++;//记录长度

map[x][y]=‘#‘;//标记为已走

for(i=0;i<4;i++) //以当前位置向四个方向扩展

{

xx=x+dir[i][0];

yy=y+dir[i][1];

if(border(xx,yy)&&map[xx][yy]==‘.‘)  //满足条件就以当前位置继续扩展

search(xx,yy);

}

}

int main()

{

int i,j;

int x0,y0;

while(cin>>l>>h)

{

sum=0;

if(l==0&&h==0)break;

for(i=0;i<h;i++)

{

for(j=0;j<l;j++)

{

cin>>map[i][j];

if(map[i][j]==‘@‘)//记录当前位置

{

x0=i;

y0=j;

}

}

}

search(x0,y0);//调用当前位置

cout<<sum<<endl;

}

return 0;

}

DFS深搜-Red and Black

时间: 2024-10-10 13:11:08

DFS深搜-Red and Black的相关文章

CodeM美团点评编程大赛初赛B轮 黑白树【DFS深搜+暴力】

[编程题] 黑白树 时间限制:1秒 空间限制:32768K 一棵n个点的有根树,1号点为根,相邻的两个节点之间的距离为1.树上每个节点i对应一个值k[i].每个点都有一个颜色,初始的时候所有点都是白色的. 你需要通过一系列操作使得最终每个点变成黑色.每次操作需要选择一个节点i,i必须是白色的,然后i到根的链上(包括节点i与根)所有与节点i距离小于k[i]的点都会变黑,已经是黑的点保持为黑.问最少使用几次操作能把整棵树变黑. 输入描述: 第一行一个整数n (1 ≤ n ≤ 10^5) 接下来n-1

A Knight&#39;s Journey(DFS)深搜

A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33634 Accepted: 11450 Description Background The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey around

hdu1010-Tempter of the Bone DFS深搜入门题+奇偶剪枝

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 69699    Accepted Submission(s): 19176 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

dfs深搜

dfs深搜 什么是深搜? 百度百科:按照一定的顺序.规则,不断去试探,直到找到问题的解,试完了也没有找到解,那就是无解,试探时一定要试探完所有的情况(实际上就是穷举) 个人理解:暴力穷举每一种状态 它有什么好处? 容易理解 骗分利器 好写 它有什么弊端? 慢.毕竟是穷举每一种状态 一搜到底.对于递归次数非常多的话,dfs会非常浪费时间,甚至有可能会爆栈 如何实现? 算法流程图如下: #include <iostream> #include <cstdio> void dfs(int

【DFS深搜初步】HDOJ-2952 Counting Sheep、NYOJ-27 水池数目

[题目链接:HDOJ-2952] Counting Sheep Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2476    Accepted Submission(s): 1621 Problem Description A while ago I had trouble sleeping. I used to lie awake,

HDU 1045 Fire Net【DFS深搜】

Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9880    Accepted Submission(s): 5739 Problem Description Suppose that we have a square city with straight streets. A map of a city is a

HDU-1016 Prime Ring Problem(DFS深搜+打表)

题目回顾(HDU-1016): Prime Ring Problem Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime.Note: the number

HDU_1241 Oil Deposits(DFS深搜)

Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square pl

POJ-1190-生日蛋糕-DFS(深搜)-枚举-多重剪枝

题目链接: 这个题目很好,有难度:可以好好的多做做: #include<iostream> #include<string> #include<cstdio> #include<cstring> #include<queue> #include<map> #include<cmath> #include<stack> #include<set> #include<vector> #inc