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

POJ 1979 Red and Black dfs 难度:0

时间:2015-06-05 22:34:19      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

http://poj.org/problem?id=1979

#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 21;
bool vis[maxn][maxn];
char maz[maxn][maxn];
int n,m;
const int dx[4] = {1,-1,0,0};
const int dy[4] = {0,0,1,-1};
int ans;

bool in(int x,int y)
{
    return x >= 0 && x < n  && y >= 0 && y < m;
}

void dfs(int x,int y)
{
    for(int i = 0;i < 4;i++)
    {
        int tx = x + dx[i],ty = y + dy[i];
        if(in(tx,ty) && !vis[tx][ty] && maz[tx][ty] != ‘#‘)
        {
            ans++;
            vis[tx][ty] = true;
            dfs(tx,ty);
        }
    }
}

int main(){
    while(scanf("%d%d",&m,&n) == 2 && (m || n))
    {
        ans = 0;
        memset(vis,0,sizeof vis);
        for(int i = 0;i < n; i++)
        {
            scanf("%s",maz[i]);
        }
        for(int x = 0;x < n; x++)
        {
            for(int y = 0;y < m;y++)
            {
                if(!vis[x][y] && maz[x][y] == ‘@‘)
                {
                    ans++;
                    vis[x][y] = true;
                    dfs(x,y);
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

POJ 1979 Red and Black dfs 难度:0

标签:

原文地址:http://www.cnblogs.com/xuesu/p/4555601.html

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