zoukankan      html  css  js  c++  java
  • [POJ] 1979 Red and Black

    Red and Black
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 21102   Accepted: 11267

    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
    

    Source

     
    题解:广度优先搜索,从@处出发统计能遍历的black tile的个数,@也算一个。
     
    代码:
     1 #include<stdio.h>
     2 #include<stdbool.h>
     3 #include<string.h>
     4 int i,j,n,m,sum,head,tail,sum,x,y,
     5     q[100000][2],
     6     dx[4]={0,0,1,-1},
     7     dy[4]={1,-1,0,0};
     8 
     9 char a[100][100];
    10 
    11 bool can[100][100];
    12 
    13 int 
    14 pre()
    15 {
    16     memset(q,0,sizeof(q));
    17     memset(can,false,sizeof(can));
    18     sum=1;
    19     return 0;
    20 }
    21 
    22 int
    23 init()
    24 {
    25     for(i=1;i<=n;i++)
    26     {
    27         scanf("%s",&a[i]);
    28         for(j=0;j<m;j++)
    29         {
    30             if(a[i][j]=='.')
    31             can[i][j]=true;
    32             
    33             if(a[i][j]=='@')
    34             {
    35                 x=i;y=j;
    36                 can[i][j]=true;
    37             }
    38         }
    39     }
    40     
    41     return 0;
    42 }
    43 
    44 int 
    45 bfs()
    46 {
    47     int i,xi,yi,xx,yy;
    48     
    49     head=0;tail=1;
    50     can[x][y]=false;
    51     q[1][0]=x;q[1][1]=y;
    52     
    53     while(head!=tail)
    54     {
    55         head=head%100000+1;
    56         xi=q[head][0];
    57         yi=q[head][1];
    58         
    59         for(i=0;i<4;i++)
    60         {
    61             xx=xi+dx[i];
    62             yy=yi+dy[i];
    63             if(can[xx][yy])
    64             {
    65                 tail=tail%100000+1;
    66                 q[tail][0]=xx;
    67                 q[tail][1]=yy;
    68                 can[xx][yy]=false;
    69                 sum++;
    70             }
    71         }
    72     }
    73     printf("%d
    ",sum);
    74     return 0;
    75     
    76     
    77 }
    78              
    79 int 
    80 main()
    81 {
    82     while(true)
    83     {
    84         pre();
    85         scanf("%d%d
    ",&m,&n);
    86         if(m==0&&n==0) break;
    87         init();
    88         bfs();
    89     }
    90     
    91    
    92     return 0;
    93 }
  • 相关阅读:
    windows10关闭更新,windowsUpdate禁用无效 windows无限重启 一分钟无限重启 win10无法连接到SENS服务
    Visual Studio项目/解决方案重命名
    关于Geometry中的一些简单形状
    无法打开http://localhost:6080/arcgis/manager/
    centOS无法联网
    FTP服务器搭建
    iFrame中dateGrid中数据不显示
    关于python的基础知识
    python中int str bool list dict数据操作方法汇总
    关于int str bool的讨论
  • 原文地址:https://www.cnblogs.com/sxiszero/p/3643980.html
Copyright © 2011-2022 走看看