zoukankan      html  css  js  c++  java
  • 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) 
    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

    百度翻译:有一个长方形的房间,上面铺着方砖。每个瓷砖都是红色或黑色的。一个男人站在一块黑瓷砖上。从一个瓷砖,他可以移动到四个相邻的瓷砖之一。但他不能在红瓷砖上移动,只能在黑瓷砖上移动。

    写一个程序来计算黑瓷砖的数量,他可以通过重复上面描述的动作来达到。

    思路:先找到人的坐标,如何搜索。注意输入时先输入的是列数,再输入的是行数。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <fstream>
     4 #include <algorithm>
     5 #include <cmath>
     6 #include <deque>
     7 #include <vector>
     8 #include <queue>
     9 #include <string>
    10 #include <cstring>
    11 #include <map>
    12 #include <stack>
    13 #include <set>
    14 #include <sstream>
    15 #define mod 1000000007
    16 #define eps 1e-6
    17 #define ll long long
    18 #define INF 0x3f3f3f3f
    19 using namespace std;
    20 
    21 int m,n;
    22 int ans;//计数器
    23 int fx[4]={1,-1,0,0},fy[4]={0,0,-1,1};
    24 string cz[25];
    25 
    26 void dfs(int i,int j)
    27 {
    28     ans++;
    29     cz[i][j]='#';
    30     for(int k=0;k<4;k++)
    31     {
    32         int x=i+fx[k];
    33         int y=j+fy[k];
    34         if(x>=0&&x<n&&y>=0&&y<m&&cz[x][y]=='.')
    35         {
    36             dfs(x,y);
    37         }
    38     }
    39 }
    40 
    41 int main()
    42 {
    43     while(cin>>m>>n,m!=0,n!=0)
    44     {
    45         int x,y;
    46         for(int i=0;i<n;i++)
    47         {
    48             cin>>cz[i];
    49             for(int j=0;j<m;j++)
    50             {
    51                 if(cz[i][j]=='@')//找人
    52                 {
    53                     x=i;
    54                     y=j;
    55                 }
    56             }
    57         }
    58         ans=0;
    59         dfs(x,y);
    60         printf("%d
    ",ans);
    61     }
    62 }
  • 相关阅读:
    Vue中v-on的指令以及一些其他指令
    Vue模板语法中数据绑定
    Vue介绍以及模板语法-插值
    JQ中的Ajax的封装
    跨域问题的解决?
    Ajax的快速入门
    如何使用npm的部分用法以及npm被墙的解决方法
    使用Node.js简单创建一个服务器
    机智云连接ESP8266--远程控制点亮RGB灯
    1、索引的概念和分类
  • 原文地址:https://www.cnblogs.com/mzchuan/p/11174135.html
Copyright © 2011-2022 走看看