码迷,mamicode.com
首页 > 其他好文 > 详细

红与黑

时间:2017-06-05 00:23:35      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:mem   输出   front   题解   限制   时间   ==   clu   ++   

传送门

2806 红与黑

 时间限制: 1 s
 空间限制: 64000 KB
 题目等级 : 白银 Silver
 
 
题目描述 Description

有一个矩形房间,覆盖正方形瓷砖。每块瓷砖涂成了红色或黑色。一名男子站在黑色的瓷砖上,由此出发,可以移到四个相邻瓷砖之一,但他不能移动到红砖上,只能移动到黑砖上。编写一个程序,计算他通过重复上述移动所能经过的黑砖数。

 

输入描述 Input Description

输入包含多个数据集。一个数据集开头行包含两个正整数W和H,W和H分别表示矩形房间的列数和行数,且都不超过20.
每个数据集有H行,其中每行包含W个字符。每个字符的含义如下所示:
‘.‘——黑砖
‘#‘——红砖
‘@‘——男子(每个数据集仅出现一次)
两个0表示输入结束。

输出描述 Output Description

对每个数据集,程序应该输出一行,包含男子从初始瓷砖出发可到达的瓷砖数。

样例输入 Sample Input

6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0

样例输出 Sample Output

45
59
6
13

数据范围及提示 Data Size & Hint

【思路】

广搜

【code】

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> 
using namespace std;
char a[21][21];
int boo[21][21];
bool vis[22][22];
int dx[4]={0,1,-1,0},
    dy[4]={1,0,0,-1};
int h,w,ans,x,y; 
bool ok(int x,int y)
{
    if(x>=1&&x<=h&&y>=1&&y<=w)return 1;
    return 0;
}
void bfs(int x,int y)
{
    queue <int> qx;
    queue <int> qy;
    qx.push(x);qy.push(y);
    while(!qx.empty())
    {
        int nowx=qx.front();qx.pop();
        int nowy=qy.front();qy.pop();
        ans++;vis[nowx][nowy]=1;
        for(int i=0;i<4;i++)
        {
            int xx=nowx+dx[i],yy=nowy+dy[i];
            if(boo[xx][yy]&&!vis[xx][yy]&&ok(xx,yy))
            {
                qx.push(xx);qy.push(yy);  
                vis[xx][yy]=1;
            }
        }
    }  
}
int main()
{
    while(scanf("%d%d",&w,&h)&&w&&h)
    {
        ans=0;
        memset(boo,0,sizeof(boo));
        memset(vis,0,sizeof(vis));
        for(int i=0;i<h;i++)
        {
            scanf("%s",a[i]);
            for(int j=0;j<w;j++)
            {
                if(a[i][j]==.)
                boo[i+1][j+1]=1;
                if(a[i][j]==@)
                {
                    boo[i+1][j+1]=1;
                    x=i+1;y=j+1;
                }
            }
        }
        bfs(x,y);
        printf("%d\n",ans);
    }
    return 0;
}

 

红与黑

标签:mem   输出   front   题解   限制   时间   ==   clu   ++   

原文地址:http://www.cnblogs.com/zzyh/p/6942179.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!