ACM PKU 1979 Red and Black

题目链接:http://poj.org/problem?id=1979

突然发现今天刷水题的速度上来了,而且是刷一个AC一个,哈哈

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int maxn=20;
int W,H,ans,sx,sy;
int r[4]= {0,-1,0,1},c[4]= {1,0,-1,0};
char C[maxn+10][maxn+10];

bool charge(int x,int y)
{
    if(x<0||y<0||x>=H||y>=W||C[x][y]=='#')return false;
    return true;
}

void dfs(int x,int y)
{
    C[x][y]='#';
    ans++;
    for(int i=0; i<4; i++)
    {
        int a=x+r[i];
        int b=y+c[i];
        if(charge(a,b))   dfs(a,b);
    }
}

int main()
{
//    freopen("in.txt","r",stdin);
    while(scanf("%d%d",&W,&H)&&(W||H))
    {
        ans=0;
        getchar();
        for(int i=0; i<H; i++)
        {
            for(int j=0; j<W; j++)
            {
                scanf("%c",&C[i][j]);
                if(C[i][j]=='@')
                {
                    sx=i,sy=j;
                }
            }
            getchar();
        }
        dfs(sx,sy);
        printf("%d\n",ans);
    }
    return 0;
}

posted on 2011-09-22 20:55  _Clarence  阅读(131)  评论(0编辑  收藏  举报

导航