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
.#………
.#.#######.
.#.#…..#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#…….#.
.#########.
………..
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 不能定义成外部变量(全局变量)! 真的是~·`

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值