• HDUOJ--------(1312)Red and Black


    Red and Black

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6905    Accepted Submission(s): 4384

    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
     
    Source
     
    Recommend
    Eddy
     
     
     
     1 #include<cstdio>
     2 #include<cstdlib>
     3 #include<cstring>
     4 #include<deque>
     5 #include<iostream>
     6 #define maxn 25
     7 using namespace std;
     8 char maze[maxn][maxn];
     9 int w,h;
    10 /*建立方向树*/
    11 struct node
    12 {
    13   int x,y;
    14 }start;
    15 /*搜索方向*/
    16 int dir[4][2]=
    17 {
    18     {0,1},
    19     {0,-1},
    20     {-1,0},
    21     {1,0}
    22 };
    23 void bfs()
    24 {
    25     /*入队,出队用*/
    26     deque<node>q;
    27     /*暂存位置*/
    28     node q1,q2;
    29     q.push_back(start);
    30     while(!q.empty())
    31     {
    32      q1=q.front ();
    33      q.pop_front();
    34      for(int i=0;i<4;i++)
    35      {
    36          q2.x=q1.x+dir[i][0];
    37          q2.y=q1.y+dir[i][1];
    38          if(q2.x>h||q2.x<1||q2.y>w||q2.y<1||maze[q2.x][q2.y]=='#'||maze[q2.x][q2.y]=='p')   /*return ;*/
    39               ;       /*啥也不干,就这么一个逗号....*/
    40      else
    41      {
    42          if(maze[q2.x][q2.y]=='.')
    43              maze[q2.x][q2.y]='p';   /*就暂时用P来代表占位置吧!*/
    44         /*入队*/
    45         q.push_back (q2);
    46      }
    47      }     
    48     }
    49 }
    50 int main()
    51 {
    52     int i,j,ans;
    53     while(scanf("%d%d",&w,&h),h+w)
    54     {
    55        getchar();
    56        memset(maze,'',sizeof(maze));
    57      for(i=1;i<=h;i++)
    58      {
    59        for(j=1;j<=w;j++)
    60        {
    61            scanf("%c",&maze[i][j]);
    62            if(maze[i][j]=='@')
    63            {
    64                start.x=i;
    65                start.y=j;
    66                maze[i][j]='#';
    67            }
    68        }
    69        getchar();
    70      }
    71        bfs();
    72        ans=1;
    73       for(i=1;i<=h;i++)
    74       {
    75           for(j=1;j<=w;j++)
    76           {
    77              
    78               if(maze[i][j]=='p')
    79                   ans++;
    80           }
    81       }
    82       printf("%d
    ",ans);
    83     }
    84   return 0;
    85 }
    View Code

    bfs+匹配

  • 相关阅读:
    MinGW-编译器
    Enum , Enum Class ?
    C++编译器之间的不同性能
    交叉验证
    经验风险最小化-结构风险最小化
    图像卷积
    pytorch官网上两个例程
    ORB feature(O for orientation)
    Catalan数
    无责任共享 Coursera、Udacity 等课程视频(转载)
  • 原文地址:https://www.cnblogs.com/gongxijun/p/3362284.html
Copyright © 2020-2023  润新知