Hdu 1312 Red and Black

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312

//极其简单的dfs

#include<iostream>
using namespace std;

int w,h;
int dir[4][2]={1,0,-1,0,0,1,0,-1};
char map[22][22];
int ans;
int sx,sy,px,py;             //x指行,y指列

void dfs(int x,int y){
    
    ans++;
    map[x][y]='#';
    for(int i=0;i<4;i++)
    {
        px=x+dir[i][0];
        py=y+dir[i][1];
        if(map[px][py]=='.')
            dfs(px,py);
    }

}
int main (){

    while(cin>>w>>h){

        if(!(w|h))
            break;
        ans=0;

        memset(map,'#',sizeof(map));
        for(int i=1;i<=h;i++)                   //w指的是宽度,h指的是高度
            for(int j=1;j<=w;j++)
            {
                cin>>map[i][j];
                if(map[i][j]=='@')
                {
                    sx=i;
                    sy=j;
                }
            }

        //cout<<sx<<sy;
        dfs(sx,sy);
        cout<<ans<<endl;
    }
    return 0;
}

 

posted @ 2014-02-01 16:37  neverchanje  阅读(170)  评论(0编辑  收藏  举报