ZOJ2165 Red and Black(dfs)

Red and Black
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

翻译:
题目编号:ZOJ2165

详见:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2165

1.2题目描述:

有一个长方形的房间,房间里的地面上布满了正方形的瓷砖,瓷砖要么是红色,要么是黑色。一男子站在其中一块黑色的瓷砖上。男子可以向他四周的瓷砖上移动,但不能移动到红色的瓷砖上,只能在黑色的瓷砖上移动。

本题的目的就是要编写程序,计算他在这个房间里可以到达的黑色瓷砖的数量。

1.3输入描述

输入文件中包含多个测试数据。每个测试数据的第1 行为两个整数W和H,分别表示长方形房间里x方向和y方向上瓷砖的数目。W和H的值不超过20。接下来有H 行,每行有W个字符,每个字符代表了瓷砖的颜色,这些字符的取值及含义为:

1) ‘.’ - 黑色的瓷砖;

2) ‘#’ - 红色的瓷砖;

3) ‘@’ - 表示该位置为黑色瓷砖,且一名男子站在上面,注意每个测试数据中只有一个

‘@’符号。

输入文件中最后一行为两个0,代表输入文件结束。

1.4 输出描述

对输入文件中每个测试数据,输出占一行,为该男子从初始位置出发可以到达的黑色瓷砖的数目(包括他初始时所处的黑色瓷砖)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
#include<cstdlib>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
const int maxn = 21;
bool vis[maxn][maxn]; //标记是否走过
int m, n;
char oil[maxn][maxn];
int dir[4][2] = {1,0, 0,1, 0,-1, -1,0}; //记录该点周围的方向
void dfs(int x, int y)
{
    vis[x][y] = 0;
    int i;
    int k, g;
    for(i=0; i<4; i++){
        k = x + dir[i][0];
        g = y + dir[i][1];
        //printf("%c    %d   %d      %d     \n",oil[k][g],vis[k][g],k,g);
        if(oil[k][g]=='.' && vis[k][g]!=0 && k>=0 && k< n && g>=0 && g<m){
             //   cout<<k<<"   "<<g<<endl;
            dfs(k, g);

        }
    }
}
int main()
{
    int i, j, sum, e, t;
    while(scanf("%d%d", &m, &n)!=EOF&& m+n)
    {
        sum = 0;
        memset(vis, 1, sizeof(vis));
        memset(oil, 0, sizeof(oil));
        for(i=0; i<n; i++){
            for(j=0; j<m; j++){
                cin >> oil[i][j];
                if(oil[i][j]=='@'){
                    t=i;
                    e=j;
                }
            }
        }
        dfs(t, e);
        for(i=0; i<n; i++){
            for(j=0; j<m; j++){
                if(vis[i][j]==0){
                    sum++;
                }
            }
        }
        cout << sum << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值