zoukankan      html  css  js  c++  java
  • ACM Red and Black

    有一个矩形的房间,覆盖着方砖。 每个瓷砖都是红色或黑色。 一个男人站在黑色的瓷砖上,他可以移动到四个相邻的瓷砖之一。  但他不能在红砖上移动,他只能在黑砖上移动。

    编写一个程序来计算他可以通过重复上述移动来达到的黑色瓦片的数量。

    Input

    输入由多个数据集组成。一个数据集以一条包含两个正整数W和H的线开始;W和H分别是x和y方向上的方块数。W和H不超过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

    对于每一个数据集,你的程序应该输出一行,其中包含他可以从最初的瓷砖(包括它自己)到达瓷砖的数量。

    Sample Input

    6 9
    ....#.
    .....#
    ......
    ......
    ......
    ......
    ......
    #@...#
    .#..#.
    11 9
    .#.........
    .#.#######.
    .#.#.....#.
    .#.#.###.#.
    .#.#..@#.#.
    .#.#####.#.
    .#.......#.
    .#########.
    ...........
    11 6
    ..#..#..#..
    ..#..#..#..
    ..#..#..###
    ..#..#..#@.
    ..#..#..#..
    ..#..#..#..
    7 7
    ..#.#..
    ..#.#..
    ###.###
    ...@...
    ###.###
    ..#.#..
    ..#.#..
    0 0

    Sample Output

    45
    59
    6
    13
     1 #include<bits/stdc++.h>   //c++库
     2 using namespace std;
     3 const char visited = '!';       //0x21
     4 const char red = '#';        //0X23
     5 const char black = '.';        //0X2E
     6 const char init = '@';        //0X40
     7 int w,h,bx,by,sum = 0;
     8 char tiles[25][25];
     9 void find()   /*找起始位置*/
    10 {
    11     bool geted = false;
    12     for(int i = 0; i < h; i++)
    13     {
    14         if(geted)
    15             break;
    16         for(int j =0; j < w; j++)
    17         {
    18             if(tiles[i][j]=='@')
    19             {
    20                 bx = j;  /*记录起始位置的坐标*/ 
    21                 by = i; 
    22                 geted = true;
    23             }
    24         }
    25     }
    26 }
    27 void dfs(int row,int col)  /**/ 
    28 {
    29     if(tiles[row][col] < black|| row < 0||col < 0||row >= h||col >= w)
    30         return ;
    31     sum++;
    32     tiles[row][col] = visited;
    33     dfs(row,col-1);
    34     dfs(row-1,col);
    35     dfs(row,col+1);
    36     dfs(row+1,col);    
    37 }
    38 
    39 int main()
    40 {
    41     while(cin>>w>>h,w||h)
    42     {
    43         for(int i = 0; i < h; i++)
    44             scanf("%s",tiles[i]);   //puts(tiles[i]);测试 
    45         find();        
    46         sum = 0;
    47         dfs(by,bx);    
    48         cout<<sum<<endl;
    49     }
    50 
    51  return 0;
    52 }

    dfs函数中另一种实现方式,改变搜索方式,用dy dx数组先定义好方向。

     1 #include<iostream>
     2 #include<cstdio>
     3 using namespace std;
     4 const char visited = '!';       //0x21
     5 const char red = '#';        //0X23
     6 const char black = '.';        //0X2E
     7 const char init = '@';        //0X40
     8 int w,h,sx,sy,sum = 0;
     9 char tiles[25][25];
    10 int dy[] = {1,-1,0,0};
    11 int dx[] = {0,0,1,-1}; 
    12 void dfs(int row,int col)
    13 {
    14     if(tiles[row][col] < black) return;
    15     sum++;
    16     tiles[row][col] = visited;
    17     for(int i = 0; i < 4; i++)
    18     {
    19         int nr = row + dy[i];
    20         int nc = col + dx[i];
    21         if(nr < 0||nr >= h || nc < 0|| nc >= w) continue;
    22         dfs(nr,nc);
    23     }
    24 }
    25 void find()
    26 {
    27     for(int i = 0; i < h; i++)
    28         for(int j = 0; j < w; j++)
    29         {
    30             if(tiles[i][j]=='@')
    31             {
    32                 sx = j;
    33                 sy = i;
    34             }
    35         }
    36 }
    37 int main()
    38 {
    39     while(cin>>w>>h,w||h)
    40     {
    41         for(int i = 0; i < h; i++)
    42             scanf("%s",tiles[i]);
    43         find();
    44         sum = 0;
    45         dfs(sy,sx);
    46         cout<<sum<<endl;
    47     }
    48 
    49  return 0;
    50 }
  • 相关阅读:
    14_java之变量|参数|返回值|修饰符
    NYOJ 202 红黑树 (二叉树)
    NYOJ 138 找球号(二) (哈希)
    NYOJ 136 等式 (哈希)
    NYOJ 133 子序列 (离散化)
    NYOJ 129 树的判定 (并查集)
    NYOJ 117 求逆序数 (树状数组)
    NYOJ 93 汉诺塔 (数学)
    HDU 2050 折线分割平面 (数学)
    天梯赛L2-008 最长对称子串 (字符串处理)
  • 原文地址:https://www.cnblogs.com/jj81/p/7425260.html
Copyright © 2011-2022 走看看