Red and Black
Red and Black
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 26011 Accepted Submission(s): 15719
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
..#.#..
..#.#..
.
…@…
.
..#.#..
..#.#..
0 0
Sample Output
45
59
6
13
1.2题目描述:
有一个长方形的房间,房间里的地面上布满了正方形的瓷砖,瓷砖要么是红色,要么是黑色。一男子站在其中一块黑色的瓷砖上。男子可以向他四周的瓷砖上移动,但不能移动到红色的瓷砖上,只能在黑色的瓷砖上移动。
本题的目的就是要编写程序,计算他在这个房间里可以到达的黑色瓷砖的数量。
1.3输入描述
输入文件中包含多个测试数据。每个测试数据的第1 行为两个整数W和H,分别表示长方形房间里x方向和y方向上瓷砖的数目。W和H的值不超过20。接下来有H 行,每行有W个字符,每个字符代表了瓷砖的颜色,这些字符的取值及含义为:
1) ‘.’ - 黑色的瓷砖;
2) ‘#’ - 红色的瓷砖;
3) ‘@’ - 表示该位置为黑色瓷砖,且一名男子站在上面,注意每个测试数据中只有一个
‘@’符号。
输入文件中最后一行为两个0,代表输入文件结束。
1.4分析
经典的搜索题目,深度优先搜索,其描述如下: void dfs()
{
for(所有的邻接节点)
{
if(节点没有被遍历)
{
标记此节点;
dfs(此节点);
}
}
}
1.5代码如下:
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
int visited[30][30];
char map[30][30];
int count,n,m,p,q;
int xy[4][2]={{1,0},{0,1},{-1,0},{0,-1}};//方向数组
void dfs(int x,int y)
{ int i,j;
for(i=0;i<=3;i++)
{
int xx=x+xy[i][0];
int yy=y+xy[i][1];//四个方向遍历
if(xx>=0&&xx<n&&yy>=0&&yy<m&&visited[xx][yy]==0&&map[xx][yy]=='.')//满足条件 递归
{
visited[xx][yy]=1;
count++;
dfs(xx,yy);
}
}
}
int main()
{
//freopen("input.txt","r",stdin);
int i,j;
while(scanf("%d %d",&m,&n)!=EOF&&m&&n)//注意是输入 列 m 行 n
{
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
cin>>map[i][j];
if(map[i][j]=='@')
{
p=i;
q=j;//p,q就是单纯的记住@出现对应的坐标
}
visited[i][j]=0;//初始化
}
visited[p][q]=1;//把当前出现@的位置标记住
count=1;
dfs(p,q);//从@的位置开始递归 找"."
printf("%d\n",count);
}
return 0;
}
注意 这里的 i,j 不能定义成外部变量(全局变量)! 真的是~·`
推荐阅读
-
Win7下硬盘安装 Red Hat Enterprise Linux 6.0 ES图文方法
-
映众GTX1080 Black冰龙黑金版显卡天梯图评测和拆解
-
Red Hat Linux,Apache2.0+Weblogic9.2负载均衡集群安装配置
-
Red Hat Linux上使用BIND建立DNS服务器
-
赛睿Sensei 310 Glossy Red火星红开箱图赏:鲜红夺目
-
经过了近两年的跳票后 RED Hydrogen One钛合金版终于开始出货
-
西数发布WD Black SN750 SSD:游戏模式一键加速
-
Red Hat宣称云计算比微软强大 无视VMware
-
windows系统安装虚拟机VMware12,然后在虚拟机中安装Red Hat Enterprise Linux6操作系统
-
[转]redhat7(centos7) not registered to Red Hat Subscription Management