zoukankan      html  css  js  c++  java
  • hdu 1312 Red and Black

    Red and Black

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


    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
    ....#.
    .....#
    ......
    ......
    ......
    ......
    ......
    #@...#
    .#..#.
    0 0
     
     
    Sample Output
    45
     
     
    Source
     
     
    PS:简单题
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstdlib>
     5 #include<cstring>
     6 #include<queue>
     7 using namespace std;
     8 int n,m;
     9 int xi,yj;
    10 char map[25][25];
    11 int vis[25][25];
    12 int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//上下左右
    13 struct node{
    14     int x;
    15     int y;
    16 };
    17 
    18 bool check(int x,int y)
    19 {
    20     if(x>=1&&x<=n && y>=1&&y<=m && map[x][y]=='.' && !vis[x][y])
    21     return true;
    22     return false;
    23 }
    24 
    25 int bfs()
    26 {
    27     memset(vis,0,sizeof(vis));
    28     int i,sum=1;
    29     node p,q;
    30     queue<struct node>Q;
    31     p.x=xi;
    32     p.y=yj;
    33     Q.push(p);
    34     while(!Q.empty())
    35     {
    36         p=Q.front();
    37         Q.pop();
    38 
    39         for(i=0;i<4;i++)
    40         {
    41             q.x=p.x+dir[i][0];
    42             q.y=p.y+dir[i][1];
    43             if(check(q.x,q.y))
    44             {
    45                 vis[q.x][q.y]=1;
    46                 sum++;
    47                 Q.push(q);
    48             }
    49         }
    50     }
    51     return sum;
    52 }
    53 
    54 
    55 int main()
    56 {
    57     int i,j;
    58     int count;
    59     //freopen("in.txt","r",stdin);
    60     while(~scanf("%d%d",&m,&n))
    61     {
    62 
    63         if(!n&&!m)
    64         break;
    65         for(i=1;i<=n;i++)
    66         {
    67             getchar();
    68             for(j=1;j<=m;j++)
    69             {
    70                 scanf("%c",&map[i][j]);
    71                 if(map[i][j]=='@')
    72                 {
    73                     xi=i;
    74                     yj=j;
    75                 }
    76             }
    77         }
    78         count=bfs();
    79         printf("%d
    ",count);
    80     }
    81     return 0;
    82 }
    View Code
  • 相关阅读:
    oracle添加字段,备注
    oracle对日期date类型操作的函数
    查询效率例子收藏
    webuploader.min.js 简单例子
    select 数字/字符串/count(参数)/sum(数字) from table
    oracle常用分析函数 over(partition by xxx order by xxx)
    LigerUi遮罩的两个方法
    LigerUI子父窗口之间传参问题
    LigerUi自动检索输入
    LigerUi折叠与展开
  • 原文地址:https://www.cnblogs.com/xuesen1995/p/4126292.html
Copyright © 2011-2022 走看看