您的位置:首页 > 其它

red and black 深度优先搜素(算法)

2016-05-21 08:39 344 查看
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
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0


Sample Output

45
59
6
13
在线翻译:
问题描述
有一个长方形的房间,满是方形的瓷砖。每一瓦都是红色或黑色的。一个人正站在一个黑色的瓷砖上。从一个瓷砖,他可以移动到四个相邻的瓷砖之一。但他不能在红色的瓷砖,他只能移动黑色瓷砖。
写一个程序来计算他可以通过重复上面描述的移动的黑色瓷砖的数量。
输入
输入由多个数据集组成。数据集从一行包含两个正整数的行开始,而在两个方向上,分别是瓷砖的数量和。没有超过20个。
数据集上有更多的行,其中每一个都包含了字符。每个字符表示一个瓷砖的颜色如下。
“-一个黑色的瓦
“# -红瓦
在一个黑色的瓷砖上,一个男人在一个数据集里出现了一次
输出
对于每一个数据集,您的程序应该输出一行,其中包含的数量,他可以从最初的瓷砖(包括本身)。

解题思路:搜索(深度优先搜素法)
1.循环+条件判断;
2.函数的调用+递归;
AC代码:

#include <stdio.h>
#include <string.h>
int n,m,cnt,x,y;
char s[30][30];
int to[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};       //根据题意定义二维数组, 查询四周位置;
void dfs(int i,int j)
{
cnt++;                                       //记录符合条件的个数;
s[i][j] = '#';                               //首先确定型号,便于下文的查询;
for(int k = 0; k<4; k++)
{
x = i+to[k][0];
y = j+to[k][1];                          //四周位置定位;
if(x<n && y<m && x>=0 && y>=0 && s[x][y] == '.')
dfs(x,y);                             //函数递归,直到查询到结果停止;
}
return;                                       //void无输出;
}
int main()
{
int i,j,fi,fj;
while(~scanf("%d%d%*c",&m,&n))               //%*c输出空格,消除影响;
{
if(m == 0 && n == 0)                     //特殊情况;
break;
for(i = 0; i<n; i++)
{
for(j = 0; j<m; j++)
{
scanf("%c",&s[i][j]);
if(s[i][j] == '@')              //开始条件;
{
fi = i;
fj = j;
}
}
getchar();                          //吸收还行空格;
}
cnt = 0;                                //新的样例,重新归零;
dfs(fi,fj);                             //直接调用函数;
printf("%d\n",cnt);
}
return 0;
}
</pre><pre name="code" class="html"><pre name="code" class="html">
#include<cstdio>
#include<cstdlib>
#include<iostream>                   //c++数据的输出和输入;
#include<cstring>
using namespace std;
int m,n,cnt;
char s[30][30];
int dfs(int i, int j)
{
//cnt++;
if(i<1 || i>n || j<1 || j>m)
return 0;
if(s[i][j]!='#')
{
s[i][j]='#';
return 1+dfs(i-1,j)+dfs(i+1,j)+dfs(i,j-1)+dfs(i,j+1);     //函数递归,累加返回值;
}
else
return 0;
}
int main ()
//(int agrc,char *argv[])
{
while(cin>>m>>n)
{
if(m==0&&n==0)
break;
int i,j;
for(i=1; i<=n; i++)
for(j=1; j<=m; j++)
cin>>s[i][j];
for(i=1; i<=n; i++)
for(j=1; j<=m; j++)
if(s[i][j]=='@')          //确定查询的起始位置;
cout<<dfs(i,j)<<endl;
}
return 0;
//return EXIT_SUCCESS;
}
总结: 明白题意;注意细节;



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: