E - Red and Black

        这个题也是栈和队列的应用,套模板就行了,只不过解题的时候傻逼了一下,至于具体怎么傻逼,请看后面的代码。


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
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
 

Sample Output

45
59
6
13



下面是代码:


#include <iostream>
#include <queue>
#include <memory.h>
using namespace std;
class Point             //定义一个坐标类,用于实例化一个坐标对象
{
public:
    int x;
    int y;
    Point(int px, int py):x(px),y(py){}
};
const int maxn=20+4;
int w, h, mx, my;
const int direct[4][2]={{0,1},{0,-1},{1,0},{-1,0}};     //方向数组,挺好用的
char omap[maxn][maxn];
int flag[maxn][maxn];
void Init()
{
    for(int i=1;i<=h;i++)for(int j=1;j<=w;j++)
    {
        cin>>omap[i][j];
        if(omap[i][j]=='@'){mx=i;my=j;}         //初始化拿到@的位置,记录下来
    }
    memset(flag, 0, sizeof(flag));              //清空标记数组
}
int check(int cx, int cy)
{
    return (cx<=0||cx>h||cy<=0||cy>w||omap[cx][cy]=='#'||flag[cx][cy])?false:true;      //判断函数
}
void Bfs(int bx, int by)
{
    //bfs模板,就不多说什么了
    queue<Point> myqueue;
    Point point(bx, by), next(0, 0);
    flag[bx][by]=1;
    myqueue.push(point);
    while(!myqueue.empty())
    {
        point=myqueue.front();
        myqueue.pop();
        for(int dx=0;dx<4;dx++)
        {
            next.x=point.x+direct[dx][0];
            next.y=point.y+direct[dx][1];
            if(check(next.x, next.y))
            {
                flag[next.x][next.y]=1;
                myqueue.push(next);
            }
        }
    }
}
void Print()
{
    int ans=0;
    for(int ax=1;ax<=h;ax++)
    {
        for(int ay=1;ay<=w;ay++)if(flag[ax][ay])ans++;
        //寻找标记数组里面的标记,其实这里傻逼了,为什么不直接在bfs的时候count++呢,诶,而且,用dfs效率不是更好吗?
    }
    cout<<ans<<endl;
}
int main()
{
    while(cin>>w>>h && w && h)
    {
        Init();
        Bfs(mx, my);
        Print();
    }
    return 0;
}