zoukankan      html  css  js  c++  java
  • OpenJ_Bailian 1979

     Red and Black

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    Submit Status Practice OpenJ_Bailian 1979

    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) 
    The end of the input is indicated by a line consisting of two zeros. 

    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

    题意:

           给出一个字符矩阵表示一个房间内的情况,‘.’表示黑砖、‘#’表示红砖、‘@’表示一个人最初站立的位置。这个人不能走上红砖,问这个人能到达的地砖总数是多少。

    输入:

           多组数据,首先输入矩阵的列数和行数,然后输入矩阵。以列数与行数都为0表示输入的结束。

    输出:

           能到达的地砖总数。

    分析:

           使用深度优先搜索。

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 using namespace std;
     7 int dx[4] = {1,0,-1,0},dy[4] = {0,1,0,-1};
     8 int W,H;
     9 char G[24][24];
    10 int cnt;
    11 void dfs(int x,int y){
    12     cnt++; G[x][y] = '#';
    13     for(int i = 0 ;i < 4 ; i++){
    14         int nx = x + dx[i],ny = y + dy[i];
    15         if(nx >= 0 && nx < H && ny >= 0 && ny < W && G[nx][ny] != '#')
    16             dfs(nx,ny);
    17     }
    18 }
    19 int main(){
    20     while(scanf("%d%d",&W,&H) == 2 && W){
    21         cnt = 0;
    22         int sx,sy;
    23         getchar();
    24         for(int i = 0 ; i < H ; i++){
    25             for(int j = 0 ; j < W ; j++){
    26                 scanf("%c",&G[i][j]);
    27                 if(G[i][j] == '@'){
    28                     sx = i;
    29                     sy = j;
    30                 }
    31             }
    32             getchar();
    33         }
    34         //for(int i = 0 ;i < H ;i++){ for(int j = 0 ;j < W; j++) printf("%c",G[i][j]);printf("
    ");}
    35         dfs(sx,sy); printf("%d
    ",cnt);
    36     }
    37     return 0;
    38 }
    View Code
  • 相关阅读:
    zlog 使用手册
    Contribution and Coding Style Guide Bullet Physics Library
    Windows系统读写ext2/3/4文件系统的工具「ext2fsd」
    blender svn
    c# 各个版本
    c# Looping with the foreach statement
    C# 9.0 and .NET 5 – Modern Cross-Platform Development
    我的常用博客
    qml通过stacklayout实现页面切换
    JSON解析-android平台(转)
  • 原文地址:https://www.cnblogs.com/cyb123456/p/5778261.html
Copyright © 2011-2022 走看看