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

    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. 

    InputThe 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) 
    OutputFor 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

    思路:BFS
    AC Code:
    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<utility>
    #include<cstring>
    using namespace std;
    typedef pair<int,int> P;
    char maps[25][25];
    int vis[25][25];
    int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
    int W,H,cnt;
    void bfs(P p){
        queue<P> q;q.push(p);
        while(!q.empty() ){
            p=q.front() ;q.pop() ;
            int x=p.first,y=p.second;
            for(int i=0;i<4;i++)
            {
                int nx=x+dx[i],ny=y+dy[i];
                if(nx>=0&&nx<H&&ny>=0&&ny<W&&maps[nx][ny]!='#'&&vis[nx][ny]==0){
                    cnt++;
                    vis[nx][ny]=1;
                    q.push(P(nx,ny));  
                }
            }
        }     
        
    }
    int main(){
        while(~scanf("%d%d",&W,&H)){
            if(W==0&&H==0) break;
            int x,y;
            getchar();
            memset(vis,0,sizeof(vis));
            for(int i=0;i<H;i++){
                for(int j=0;j<W;j++){
                    scanf("%c",&maps[i][j]);
                    if(maps[i][j]=='@') { x=i; y=j; }    
                }
                getchar();
            }
            cnt=1;vis[x][y]=1;
            bfs(P(x,y));
            printf("%d
    ",cnt);
        }
    }
      人生不如意的时候,是上帝给的长假,这个时候应该好好享受假期。
      突然有一天假期结束,时来运转,人生才是真正开始了。
  • 相关阅读:
    HTML DOM 12 表格排序
    HTML DOM 10 常用场景
    HTML DOM 10 插入节点
    HTML DOM 09 替换节点
    HTML DOM 08 删除节点
    HTML DOM 07 创建节点
    022 注释
    024 数字类型
    005 基于面向对象设计一个简单的游戏
    021 花式赋值
  • 原文地址:https://www.cnblogs.com/astonc/p/9900578.html
Copyright © 2011-2022 走看看