zoukankan      html  css  js  c++  java
  • Red and Black (BFS)poj 1979

                                      Red and Black
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 26181   Accepted: 14213

    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




    题意为:求@可以到达.(点)的最多个数, 别忘了@本身也算一个哦~!#代表不可以穿越!

    大体就是这么个意思,不多说,直接上代码.......

    #include <iostream>
    #include <stdio.h>
    #include <queue>
    #include <string.h>
    #define N 30
    using namespace std;
    int ans = 0, m, n;//ans用于计数的!
    int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};//一点小技巧
    int vis[N][N], map[N][N];//vis数组是用于标记是否访问过~
    void bfs(int x, int y)
    {
        int i,ty,tx,ttx,tty;
        queue<pair<int, int> > Q;//不会用queue的可以去百度看看  很好用滴
        Q.push(make_pair(x, y));
        vis[x][y] = 1;
        while(!Q.empty())
        {
            tx = Q.front().first, ty = Q.front().second;
            Q.pop();//别忘了一直要删除队头
            for(i = 0; i < 4; i++)
            {
                ttx = tx + dx[i], tty = ty + dy[i];
                if(ttx >= 0 && ttx < m && tty >= 0 && tty < n)
                    if(!vis[ttx][tty] && !map[ttx][tty])
                    {
                        vis[ttx][tty] = 1;
                        ans++;
                        Q.push(make_pair(ttx, tty));
                    }
            }
        }
    }
    int main()
    {
        int i, j, x, y;
        while(~scanf("%d%d", &n, &m))
        {
            ans = 1;
            memset(vis, 0, sizeof(vis));//比for循环清零好使
            getchar();//有些人输入老是出问题,看看是不是这的原因
            if(n == 0 && m == 0)
                break;
            char s[N];
            for(i = 0; i < m; i++)
            {
                gets(s);
                for(j = 0; j < n; j++)
                {
                    if(s[j] == '.')
                        map[i][j] = 0;
                    else if(s[j] == '#')
                        map[i][j] = 1;
                    else
                        x = i, y = j;//@的坐标
                }
            }
            bfs(x, y);
            printf("%d
    ", ans);
        }
        return 0;
    }
  • 相关阅读:
    C++学习之路(四):线程安全的单例模式
    C++学习之路(三):volatile关键字
    C++学习之路(五):复制构造函数与赋值运算符重载
    类对象作为函数参数进行值传递
    System V共享内存介绍
    关于迭代器失效
    C++学习之路(二):引用
    C++学习之路(一):const与define,结构体对齐,new/delete
    epoll内核源码分析
    Redux中间件之redux-thunk使用详解
  • 原文地址:https://www.cnblogs.com/yu0111/p/4727675.html
Copyright © 2011-2022 走看看