• B


    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

    #include<iostream>
    #include<queue>
    #include<cstring>
    using namespace std;
    char arr[22][22];
    int sa,ea;
    int n,m;
    int v[22][22]={0};
    struct stu{
        int a,b;
    //    int sum;
    };
    
    int a[4]={0,1,0,-1};
    int b[4]={1,0,-1,0};
    
    void bfs()
    {
        int ans=0;
    //    priority_queue<stu >que;
        queue<stu>que;
        stu q1;
        q1.a=sa;
        q1.b=ea;
    //    q1.sum=1;
        v[sa][ea]=1;
        que.push(q1);
        
        while(que.size()){
            stu h;
    //        h=que.top();
            h=que.front();
            que.pop();
            stu d;
            for(int i=0;i<4;i++){
                d.a=h.a+a[i];
                d.b=h.b+b[i];
                if(d.a>=0 && d.b>=0 && d.a<m && d.b<n&& v[d.a][d.b]!=1 && arr[d.a][d.b]!='#'){
                    v[d.a][d.b]=1;
    //                d.sum=h.sum+1;
    //                cout<<d.sum<<endl;
                    que.push(d);
    //                ans=max(ans,d.sum);
                    ans++;//只要满足条件 就加一
                }
            }
        }
        cout<<ans+1<<endl;
    }
    
    int main(){
    
        while(cin>>n>>m)
        {
            memset(v,0,sizeof(v));
            
            if(n==0&&m==0)
                break;
                
            for(int i=0;i<m;i++){
                scanf("%s",&arr[i]);
            }
            
            for(int i=0;i<m;i++){
                for(int j=0;j<n;j++){
                    if(arr[i][j]=='@'){
                        sa=i;
                        ea=j;
                    }
                }
            }
            
            bfs();
        }
        return 0;
    }
  • 相关阅读:
    iOS中block实现的探究
    [ES6] 19. for ... of
    [ES6] 18. Map
    [ES6] 17. Set
    [ES6] 16. Object Enhancements
    [MEAN Stack] First API -- 5. Using $resource to setup REST app
    [AngularJS] Provider
    [MEAN Stack] First API -- 4. Organize app structure
    [AngularJS] ngCloak
    [Angular-Scaled Web] 9. Control your promises with $q
  • 原文地址:https://www.cnblogs.com/Accepting/p/11234744.html
Copyright © 2020-2023  润新知